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): do not create new targets from target defaults when packag… #21365

Merged
merged 1 commit into from
Jan 26, 2024
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
Expand Up @@ -8,7 +8,7 @@ const {
createNodes: [, createNodesFn],
} = TargetDefaultsPlugin;

describe('nx project.json plugin', () => {
describe('target-defaults plugin', () => {
let context: CreateNodesContext;
beforeEach(() => {
context = {
Expand All @@ -23,6 +23,10 @@ describe('nx project.json plugin', () => {
};
});

afterEach(() => {
memfs.vol.reset();
});

it('should add target default info to project json projects', () => {
memfs.vol.fromJSON(
{
Expand Down Expand Up @@ -77,4 +81,156 @@ describe('nx project.json plugin', () => {
}
`);
});

it('should add target if package.json has nx but no includedScripts', () => {
memfs.vol.fromJSON(
{
'package.json': JSON.stringify({
name: 'lib-a',
scripts: {
test: 'nx affected:test',
},
nx: {},
}),
},
'/root'
);

expect(
createNodesFn('package.json', undefined, {
nxJsonConfiguration: {
targetDefaults: {
test: {
command: 'jest',
},
},
},
workspaceRoot: '/root',
})
).toMatchInlineSnapshot(`
{
"projects": {
".": {
"targets": {
"test": {
"command": "jest",
},
},
},
},
}
`);
});

it('should add target if package.json has nx and includes the script in includedScripts', () => {
memfs.vol.fromJSON(
{
'package.json': JSON.stringify({
name: 'lib-a',
scripts: {
test: 'nx affected:test',
},
nx: {
includedScripts: ['test'],
},
}),
},
'/root'
);

expect(
createNodesFn('package.json', undefined, {
nxJsonConfiguration: {
targetDefaults: {
test: {
command: 'jest',
},
},
},
workspaceRoot: '/root',
})
).toMatchInlineSnapshot(`
{
"projects": {
".": {
"targets": {
"test": {
"command": "jest",
},
},
},
},
}
`);
});

it('should not add target if package.json does not have nx', () => {
memfs.vol.fromJSON(
{
'package.json': JSON.stringify({
name: 'lib-a',
scripts: {
test: 'nx affected:test',
},
}),
},
'/root'
);

expect(
createNodesFn('package.json', undefined, {
nxJsonConfiguration: {
targetDefaults: {
test: {
command: 'jest',
},
},
},
workspaceRoot: '/root',
})
).toMatchInlineSnapshot(`{}`);
});

it('should not add target if project does not define target', () => {
memfs.vol.fromJSON(
{
'package.json': JSON.stringify({
name: 'lib-a',
scripts: {
test: 'nx affected:test',
},
nx: {
includedScripts: [],
},
}),
},
'/root'
);

expect(
createNodesFn('package.json', undefined, {
nxJsonConfiguration: {
targetDefaults: {
test: {
command: 'jest',
},
},
},
workspaceRoot: '/root',
})
).toMatchInlineSnapshot(`
{
"projects": {
".": {
"targets": {
"test": {
"command": "jest",
Symbol(ONLY_MODIFIES_EXISTING_TARGET): true,
},
},
},
},
}
`);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ export const TargetDefaultsPlugin: NxPluginV2 = {
const packageJson = readJsonOrNull<PackageJson>(
join(ctx.workspaceRoot, root, 'package.json')
);
const projectDefinedTargets = new Set(
Object.keys({
...packageJson?.scripts,
...projectJson?.targets,
})
);
const includedScripts = packageJson?.nx?.includedScripts;
const projectDefinedTargets = new Set([
...Object.keys(packageJson?.scripts ?? {}).filter((script) => {
if (includedScripts) {
return includedScripts.includes(script);
}
return true;
}),
...Object.keys(projectJson?.targets ?? {}),
]);

const executorToTargetMap = getExecutorToTargetMap(
packageJson,
Expand Down