From bcc841fe359393a9d46a12b9d8cc10bd7de1d179 Mon Sep 17 00:00:00 2001 From: AgentEnder Date: Wed, 17 May 2023 16:22:48 -0400 Subject: [PATCH] fix(core): tree should not be changed after committed to disk in migrations --- packages/nx/src/adapter/ngcli-adapter.ts | 12 ++++++++++-- packages/nx/src/command-line/generate/generate.ts | 6 +++++- .../init/implementation/dot-nx/add-nx-scripts.ts | 2 +- packages/nx/src/command-line/migrate/migrate.ts | 6 +++++- packages/nx/src/command-line/new/new.ts | 2 +- packages/nx/src/generators/tree.ts | 10 +++++++--- .../update-16-0-0/update-depends-on-to-tokens.ts | 2 +- 7 files changed, 30 insertions(+), 10 deletions(-) diff --git a/packages/nx/src/adapter/ngcli-adapter.ts b/packages/nx/src/adapter/ngcli-adapter.ts index 1f3bb6398e0dc9..8c8ba0e8d6697c 100644 --- a/packages/nx/src/adapter/ngcli-adapter.ts +++ b/packages/nx/src/adapter/ngcli-adapter.ts @@ -656,7 +656,11 @@ export async function generate( const logger = getLogger(verbose); const fsHost = new NxScopeHostUsedForWrappedSchematics( root, - new FsTree(root, verbose) + new FsTree( + root, + verbose, + `ng-cli generator: ${opts.collectionName}:${opts.generatorName}` + ) ); const workflow = createWorkflow(fsHost, root, opts); const collection = getCollection(workflow, opts.collectionName); @@ -745,7 +749,11 @@ export async function runMigration( const logger = getLogger(isVerbose); const fsHost = new NxScopeHostUsedForWrappedSchematics( root, - new FsTree(root, isVerbose) + new FsTree( + root, + isVerbose, + `ng-cli migration: ${packageName}:${migrationName}` + ) ); const workflow = createWorkflow(fsHost, root, {}); const collection = resolveMigrationsCollection(packageName); diff --git a/packages/nx/src/command-line/generate/generate.ts b/packages/nx/src/command-line/generate/generate.ts index 446c795533b494..c2800520018a0a 100644 --- a/packages/nx/src/command-line/generate/generate.ts +++ b/packages/nx/src/command-line/generate/generate.ts @@ -364,7 +364,11 @@ export async function generate(cwd: string, args: { [k: string]: any }) { ); if (ws.isNxGenerator(opts.collectionName, normalizedGeneratorName)) { - const host = new FsTree(workspaceRoot, verbose); + const host = new FsTree( + workspaceRoot, + verbose, + `generating (${opts.collectionName}:${normalizedGeneratorName})` + ); const implementation = implementationFactory(); // @todo(v17): Remove this, isStandalonePreset property is defunct. diff --git a/packages/nx/src/command-line/init/implementation/dot-nx/add-nx-scripts.ts b/packages/nx/src/command-line/init/implementation/dot-nx/add-nx-scripts.ts index 093a6b56a2c2bb..312243b5f67519 100644 --- a/packages/nx/src/command-line/init/implementation/dot-nx/add-nx-scripts.ts +++ b/packages/nx/src/command-line/init/implementation/dot-nx/add-nx-scripts.ts @@ -33,7 +33,7 @@ path_to_root=$(dirname $BASH_SOURCE) node ${path.posix.join('$path_to_root', nxWrapperPath(path.posix))} $@`; export function generateDotNxSetup(version?: string) { - const host = new FsTree(process.cwd(), false); + const host = new FsTree(process.cwd(), false, '.nx setup'); writeMinimalNxJson(host, version); updateGitIgnore(host); host.write(nxWrapperPath(), getNxWrapperContents()); diff --git a/packages/nx/src/command-line/migrate/migrate.ts b/packages/nx/src/command-line/migrate/migrate.ts index 3bf055aff782d7..197d321bf435ad 100644 --- a/packages/nx/src/command-line/migrate/migrate.ts +++ b/packages/nx/src/command-line/migrate/migrate.ts @@ -1578,7 +1578,11 @@ async function runNxMigration( name ); const fn = require(implPath)[fnSymbol]; - const host = new FsTree(root, process.env.NX_VERBOSE_LOGGING === 'true'); + const host = new FsTree( + root, + process.env.NX_VERBOSE_LOGGING === 'true', + `migration ${collection.name}:${name}` + ); await fn(host, {}); host.lock(); const changes = host.listChanges(); diff --git a/packages/nx/src/command-line/new/new.ts b/packages/nx/src/command-line/new/new.ts index cba3a78979d4e9..ffcd7fc83ee2aa 100644 --- a/packages/nx/src/command-line/new/new.ts +++ b/packages/nx/src/command-line/new/new.ts @@ -33,7 +33,7 @@ export async function newWorkspace(cwd: string, args: { [k: string]: any }) { false ); - const host = new FsTree(cwd, false); + const host = new FsTree(cwd, false, 'nx new'); const implementation = implementationFactory(); const task = await implementation(host, combinedOpts); flushChanges(cwd, host.listChanges()); diff --git a/packages/nx/src/generators/tree.ts b/packages/nx/src/generators/tree.ts index 5c24df08819ead..3500cbfc7d8df8 100644 --- a/packages/nx/src/generators/tree.ts +++ b/packages/nx/src/generators/tree.ts @@ -138,7 +138,11 @@ export class FsTree implements Tree { */ private locked = false; - constructor(readonly root: string, private readonly isVerbose: boolean) {} + constructor( + readonly root: string, + private readonly isVerbose: boolean, + private readonly logOperationId?: string + ) {} read(filePath: string): Buffer | null; read(filePath: string, encoding: BufferEncoding): string | null; @@ -360,11 +364,11 @@ export class FsTree implements Tree { // TODO (v17): Remove condition if (gt(nxVersion, '17.0.0')) { throw new Error( - 'The tree has already been committed to disk. It can no longer be modified. Do not modify the tree during a GeneratorCallback and ensure that Promises have resolved before the generator returns or resolves.' + `The tree has already been committed to disk. It can no longer be modified. Do not modify the tree during a GeneratorCallback and ensure that Promises have resolved before the generator returns or resolves. Operation ID: \`${this.logOperationId}\`` ); } else { output.warn({ - title: 'Tree modified after commit to disk.', + title: `Tree modified after commit to disk while running \`${this.logOperationId}\`.`, bodyLines: [ 'The tree has already been committed to disk. It can no longer be modified. Do not modify the tree during a GeneratorCallback and ensure that Promises have resolved before the generator returns or resolves.', `This will be an error in version 16. Please open an issue on the Nx repo if experiencing this with a first-party plugin, or the plugin's repo if using a community plugin.`, diff --git a/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.ts b/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.ts index 30a75b27b26d7d..6fabe17983a676 100644 --- a/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.ts +++ b/packages/nx/src/migrations/update-16-0-0/update-depends-on-to-tokens.ts @@ -63,7 +63,7 @@ export default async function (tree: Tree) { } } - formatChangedFilesWithPrettierIfAvailable(tree); + await formatChangedFilesWithPrettierIfAvailable(tree); } function updateDependsOnAndInputsInsideNxJson(tree: Tree) { const nxJson = readNxJson(tree);