Skip to content

Commit

Permalink
fix(core): tree should not be changed after committed to disk in migr…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
AgentEnder committed May 17, 2023
1 parent 127281b commit 7627de3
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 10 deletions.
12 changes: 10 additions & 2 deletions packages/nx/src/adapter/ngcli-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion packages/nx/src/command-line/generate/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
`Generate (${opts.collectionName}:${normalizedGeneratorName})`
);
const implementation = implementationFactory();

// @todo(v17): Remove this, isStandalonePreset property is defunct.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
6 changes: 5 additions & 1 deletion packages/nx/src/command-line/migrate/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
`${collection.name}:${name}`
);
await fn(host, {});
host.lock();
const changes = host.listChanges();
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/command-line/new/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
10 changes: 7 additions & 3 deletions packages/nx/src/generators/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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. Operation ID: \`${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.`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default async function (tree: Tree) {
}
}

formatChangedFilesWithPrettierIfAvailable(tree);
await formatChangedFilesWithPrettierIfAvailable(tree);
}
function updateDependsOnAndInputsInsideNxJson(tree: Tree) {
const nxJson = readNxJson(tree);
Expand Down

0 comments on commit 7627de3

Please sign in to comment.