Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): fix migrations for projects in workspace.json #13226

Merged
merged 1 commit into from Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -1,4 +1,7 @@
import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace';
import {
createTreeWithEmptyV1Workspace,
createTreeWithEmptyWorkspace,
} from '../../generators/testing-utils/create-tree-with-empty-workspace';
import type { Tree } from '../../generators/tree';
import {
addProjectConfiguration,
Expand Down Expand Up @@ -294,3 +297,48 @@ describe('15.0.0 migration (migrate-to-inputs)', () => {
expect(updatedWorkspace.namedInputs).not.toBeDefined();
});
});

describe('15.0.0 migration (migrate-to-inputs) (v1)', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyV1Workspace();
});

it('should add project specific implicit dependencies to project namedInputs', async () => {
updateWorkspaceConfiguration(tree, {
version: 2,
implicitDependencies: {
'tools/scripts/build-app.js': ['app1', 'app2'],
},
});
addProjectConfiguration(tree, 'app1', {
root: 'app1',
});
addProjectConfiguration(tree, 'app2', {
root: 'app2',
});
addProjectConfiguration(tree, 'lib1', {
root: 'lib1',
});

await migrateToInputs(tree);

const updated = readWorkspaceConfiguration(tree);
expect(updated.implicitDependencies).toBeUndefined();
expect(updated.namedInputs.projectSpecificFiles).toEqual([]);
expect(updated.namedInputs.default).toContain('projectSpecificFiles');

const app1 = readProjectConfiguration(tree, 'app1');
expect(app1.namedInputs.projectSpecificFiles).toContain(
'{workspaceRoot}/tools/scripts/build-app.js'
);
const app2 = readProjectConfiguration(tree, 'app2');
expect(app2.namedInputs.projectSpecificFiles).toContain(
'{workspaceRoot}/tools/scripts/build-app.js'
);

const lib = readProjectConfiguration(tree, 'lib1');
expect(lib.namedInputs).toBeUndefined();
});
});
30 changes: 16 additions & 14 deletions packages/nx/src/migrations/update-15-0-0/migrate-to-inputs.ts
Expand Up @@ -83,21 +83,23 @@ export default async function (tree: Tree) {
projectSpecificFileset
);

if (tree.exists(join(project.root, 'project.json'))) {
try {
updateProjectConfiguration(tree, dependent, project);
} else if (tree.exists(join(project.root, 'package.json'))) {
updateJson<PackageJson>(
tree,
join(project.root, 'package.json'),
(json) => {
json.nx ??= {};
json.nx.namedInputs ??= {};
json.nx.namedInputs.projectSpecificFiles ??=
project.namedInputs.projectSpecificFiles;

return json;
}
);
} catch {
if (tree.exists(join(project.root, 'package.json'))) {
updateJson<PackageJson>(
tree,
join(project.root, 'package.json'),
(json) => {
json.nx ??= {};
json.nx.namedInputs ??= {};
json.nx.namedInputs.projectSpecificFiles ??=
project.namedInputs.projectSpecificFiles;

return json;
}
);
}
}
}
} else {
Expand Down
@@ -1,4 +1,7 @@
import { createTreeWithEmptyWorkspace } from '../../generators/testing-utils/create-tree-with-empty-workspace';
import {
createTreeWithEmptyV1Workspace,
createTreeWithEmptyWorkspace,
} from '../../generators/testing-utils/create-tree-with-empty-workspace';
import type { Tree } from '../../generators/tree';
import {
addProjectConfiguration,
Expand Down Expand Up @@ -117,3 +120,48 @@ describe('15.0.0 migration (prefix-outputs)', () => {
await prefixOutputs(tree);
});
});

describe('15.0.0 migration (prefix-outputs) (v1)', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyV1Workspace();
});

it('should prefix project outputs', async () => {
addProjectConfiguration(tree, 'proj', {
root: 'proj',
targets: {
build: {
executor: 'nx:run-commands',
outputs: [
'dist',
'dist/{projectRoot}',
'dist/{projectRoot}/**/*.js',
'proj/coverage',
'./test-results',
'{projectRoot}/build',
'{options.outputDirectory}',
],
options: {},
},
},
});

await prefixOutputs(tree);

const updated = readProjectConfiguration(tree, 'proj');

expect(updated.targets.build.outputs).toEqual([
'{workspaceRoot}/dist',
'{workspaceRoot}/dist/{projectRoot}',
'{workspaceRoot}/dist/{projectRoot}/**/*.js',
'{projectRoot}/coverage',
'{projectRoot}/test-results',
'{projectRoot}/build',
'{options.outputDirectory}',
]);

expect(() => validateOutputs(updated.targets.build.outputs)).not.toThrow();
});
});
24 changes: 13 additions & 11 deletions packages/nx/src/migrations/update-15-0-0/prefix-outputs.ts
Expand Up @@ -39,19 +39,21 @@ export default async function (tree: Tree) {
target.outputs = transformLegacyOutputs(project.root, e);
}
}
if (tree.exists(join(project.root, 'project.json'))) {
try {
updateProjectConfiguration(tree, projectName, project);
} else if (tree.exists(join(project.root, 'package.json'))) {
updateJson<PackageJson>(
tree,
join(project.root, 'package.json'),
(json) => {
json.nx ??= {};
json.nx.targets ??= project.targets;
} catch {
if (tree.exists(join(project.root, 'package.json'))) {
updateJson<PackageJson>(
tree,
join(project.root, 'package.json'),
(json) => {
json.nx ??= {};
json.nx.targets ??= project.targets;

return json;
}
);
return json;
}
);
}
}
}

Expand Down