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(misc): fix migrations updating target options to consider target defaults #18064

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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
import {
ProjectConfiguration,
ProjectGraph,
Tree,
addProjectConfiguration,
readProjectConfiguration,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import migration from './explicitly-set-projects-to-update-buildable-deps';

let projectGraph: ProjectGraph;
jest.mock('@nx/devkit', () => ({
...jest.requireActual('@nx/devkit'),
createProjectGraphAsync: () => Promise.resolve(projectGraph),
}));

describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
let tree: Tree;

beforeEach(() => {
tree = createTreeWithEmptyWorkspace({ layout: 'apps-libs' });
projectGraph = { dependencies: {}, nodes: {} };
});

it.each([
Expand All @@ -22,7 +30,7 @@ describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
])(
'should set updateBuildableProjectDepsInPackageJson option to "true" when not specified in target using "%s"',
async (executor) => {
addProjectConfiguration(tree, 'lib1', {
addProject(tree, 'lib1', {
root: 'libs/lib1',
projectType: 'library',
targets: { build: { executor, options: {} } },
Expand All @@ -37,6 +45,29 @@ describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
}
);

it.each([
'@nx/angular:ng-packagr-lite',
'@nrwl/angular:ng-packagr-lite',
'@nx/angular:package',
'@nrwl/angular:package',
])(
'should set updateBuildableProjectDepsInPackageJson option to "true" when target has no options object defined using "%s"',
async (executor) => {
addProject(tree, 'lib1', {
root: 'libs/lib1',
projectType: 'library',
targets: { build: { executor } },
});

await migration(tree);

const project = readProjectConfiguration(tree, 'lib1');
expect(
project.targets.build.options.updateBuildableProjectDepsInPackageJson
).toBe(true);
}
);

it.each([
'@nx/angular:ng-packagr-lite',
'@nrwl/angular:ng-packagr-lite',
Expand All @@ -45,7 +76,7 @@ describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
])(
'should not overwrite updateBuildableProjectDepsInPackageJson option when it is specified in target using "%s"',
async (executor) => {
addProjectConfiguration(tree, 'lib1', {
addProject(tree, 'lib1', {
root: 'libs/lib1',
projectType: 'library',
targets: {
Expand Down Expand Up @@ -76,11 +107,29 @@ describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
},
},
};
addProjectConfiguration(tree, 'lib1', originalProjectConfig);
addProject(tree, 'lib1', originalProjectConfig);

await migration(tree);

const project = readProjectConfiguration(tree, 'lib1');
expect(project.targets).toStrictEqual(originalProjectConfig.targets);
});
});

function addProject(
tree: Tree,
projectName: string,
config: ProjectConfiguration
): void {
projectGraph = {
dependencies: {},
nodes: {
[projectName]: {
data: config,
name: projectName,
type: config.projectType === 'application' ? 'app' : 'lib',
},
},
};
addProjectConfiguration(tree, projectName, config);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
createProjectGraphAsync,
formatFiles,
getProjects,
readProjectConfiguration,
Tree,
updateProjectConfiguration,
} from '@nx/devkit';
Expand All @@ -13,31 +14,37 @@ const executors = new Set([
]);

export default async function (tree: Tree) {
const projects = getProjects(tree);
// use project graph to get the expanded target configurations
const projectGraph = await createProjectGraphAsync();

for (const [projectName, project] of projects) {
if (project.projectType !== 'library') {
for (const [projectName, { data: projectData }] of Object.entries(
projectGraph.nodes
)) {
if (projectData.projectType !== 'library') {
continue;
}

let updated = false;
for (const [, target] of Object.entries(project.targets || {})) {
for (const [targetName, target] of Object.entries(
projectData.targets || {}
)) {
if (!executors.has(target.executor)) {
continue;
}

if (
target.options &&
!target.options ||
target.options.updateBuildableProjectDepsInPackageJson === undefined
) {
target.options.updateBuildableProjectDepsInPackageJson = true;
updated = true;
// read the project configuration to write the explicit project configuration
// and avoid writing the expanded target configuration
const project = readProjectConfiguration(tree, projectName);
project.targets[targetName].options ??= {};
project.targets[
targetName
].options.updateBuildableProjectDepsInPackageJson = true;
updateProjectConfiguration(tree, projectName, project);
}
}

if (updated) {
updateProjectConfiguration(tree, projectName, project);
}
}

await formatFiles(tree);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import {
ProjectConfiguration,
ProjectGraph,
Tree,
addProjectConfiguration,
readProjectConfiguration,
} from '@nx/devkit';
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
import migration from './explicitly-set-projects-to-update-buildable-deps';

let projectGraph: ProjectGraph;
jest.mock('@nx/devkit', () => ({
...jest.requireActual('@nx/devkit'),
createProjectGraphAsync: () => Promise.resolve(projectGraph),
}));

describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
let tree: Tree;

Expand All @@ -17,7 +24,7 @@ describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
it.each(['@nx/js:swc', '@nrwl/js:swc', '@nx/js:tsc', '@nrwl/js:tsc'])(
'should set updateBuildableProjectDepsInPackageJson option to "true" when not specified in target using "%s"',
async (executor) => {
addProjectConfiguration(tree, 'lib1', {
addProject(tree, 'lib1', {
root: 'libs/lib1',
projectType: 'library',
targets: { build: { executor, options: {} } },
Expand All @@ -32,10 +39,28 @@ describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
}
);

it.each(['@nx/js:swc', '@nrwl/js:swc', '@nx/js:tsc', '@nrwl/js:tsc'])(
'should set updateBuildableProjectDepsInPackageJson option to "true" when target has no options object defined using "%s"',
async (executor) => {
addProject(tree, 'lib1', {
root: 'libs/lib1',
projectType: 'library',
targets: { build: { executor } },
});

await migration(tree);

const project = readProjectConfiguration(tree, 'lib1');
expect(
project.targets.build.options.updateBuildableProjectDepsInPackageJson
).toBe(true);
}
);

it.each(['@nx/js:swc', '@nrwl/js:swc', '@nx/js:tsc', '@nrwl/js:tsc'])(
'should not overwrite updateBuildableProjectDepsInPackageJson option when it is specified in target using "%s"',
async (executor) => {
addProjectConfiguration(tree, 'lib1', {
addProject(tree, 'lib1', {
root: 'libs/lib1',
projectType: 'library',
targets: {
Expand Down Expand Up @@ -66,11 +91,29 @@ describe('explicitly-set-projects-to-update-buildable-deps migration', () => {
},
},
};
addProjectConfiguration(tree, 'lib1', originalProjectConfig);
addProject(tree, 'lib1', originalProjectConfig);

await migration(tree);

const project = readProjectConfiguration(tree, 'lib1');
expect(project.targets).toStrictEqual(originalProjectConfig.targets);
});
});

function addProject(
tree: Tree,
projectName: string,
config: ProjectConfiguration
): void {
projectGraph = {
dependencies: {},
nodes: {
[projectName]: {
data: config,
name: projectName,
type: config.projectType === 'application' ? 'app' : 'lib',
},
},
};
addProjectConfiguration(tree, projectName, config);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
createProjectGraphAsync,
formatFiles,
getProjects,
readProjectConfiguration,
Tree,
updateProjectConfiguration,
} from '@nx/devkit';
Expand All @@ -13,31 +14,37 @@ const executors = new Set([
]);

export default async function (tree: Tree) {
const projects = getProjects(tree);
// use project graph to get the expanded target configurations
const projectGraph = await createProjectGraphAsync();

for (const [projectName, project] of projects) {
if (project.projectType !== 'library') {
for (const [projectName, { data: projectData }] of Object.entries(
projectGraph.nodes
)) {
if (projectData.projectType !== 'library') {
continue;
}

let updated = false;
for (const [, target] of Object.entries(project.targets || {})) {
for (const [targetName, target] of Object.entries(
projectData.targets || {}
)) {
if (!executors.has(target.executor)) {
continue;
}

if (
target.options &&
!target.options ||
target.options.updateBuildableProjectDepsInPackageJson === undefined
) {
target.options.updateBuildableProjectDepsInPackageJson = true;
updated = true;
// read the project configuration to write the explicit project configuration
// and avoid writing the expanded target configuration
const project = readProjectConfiguration(tree, projectName);
project.targets[targetName].options ??= {};
project.targets[
targetName
].options.updateBuildableProjectDepsInPackageJson = true;
updateProjectConfiguration(tree, projectName, project);
}
}

if (updated) {
updateProjectConfiguration(tree, projectName, project);
}
}

await formatFiles(tree);
Expand Down
Loading