Skip to content

Commit 30446df

Browse files
authored
fix(misc): dot nx setup shouldn't include target defaults (#23180)
<!-- Please make sure you have read the submission guidelines before posting an PR --> <!-- https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr --> <!-- Please make sure that your commit message follows our format --> <!-- Example: `fix(nx): must begin with lowercase` --> ## Current Behavior - `nx add @nx/gradle` installs latest version - `nx init` sets up target defaults - `nx add package@beta` will add `beta` as the version, rather than resolving it to the tag. ## Expected Behavior - `nx add @nx/gradle` installs version matching `nx` - `nx init` lets plugin handle settings - `nx add package@beta` will add the latest version matching the beta tag ## Related Issue(s) <!-- Please link the issue being fixed so it gets closed when this is merged. --> Fixes #
1 parent 84eb280 commit 30446df

File tree

4 files changed

+48
-59
lines changed

4 files changed

+48
-59
lines changed

e2e/nx/src/nxw.test.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ describe('nx wrapper / .nx installation', () => {
1818

1919
beforeAll(() => {
2020
runNxWrapper = newWrappedNxWorkspace();
21+
updateJson<NxJsonConfiguration>('nx.json', (json) => {
22+
json.targetDefaults ??= {};
23+
json.targetDefaults.echo = { cache: true };
24+
json.installation.plugins = {
25+
'@nx/js': getPublishedVersion(),
26+
};
27+
return json;
28+
});
2129
});
2230

2331
afterAll(() => {
@@ -39,14 +47,6 @@ describe('nx wrapper / .nx installation', () => {
3947
})
4048
);
4149

42-
updateJson<NxJsonConfiguration>('nx.json', (json) => {
43-
json.targetDefaults.echo = { cache: true };
44-
json.installation.plugins = {
45-
'@nx/js': getPublishedVersion(),
46-
};
47-
return json;
48-
});
49-
5050
expect(runNxWrapper('echo a')).toContain('Hello from A');
5151

5252
expect(runNxWrapper('echo a')).toContain(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { nxVersion } from '../../utils/versions';
2+
import { coreNxPluginVersions } from './add';
3+
4+
describe('nx core packages', () => {
5+
it('should map nx packages to nx version', () => {
6+
expect(coreNxPluginVersions.get('@nx/workspace')).toEqual(nxVersion);
7+
});
8+
9+
it('should map nx-cloud to latest', () => {
10+
expect(coreNxPluginVersions.get('@nrwl/nx-cloud')).toEqual('latest');
11+
expect(coreNxPluginVersions.get('nx-cloud')).toEqual('latest');
12+
});
13+
});

packages/nx/src/command-line/add/add.ts

+19-32
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { getPluginCapabilities } from '../../utils/plugins';
1414
import { nxVersion } from '../../utils/versions';
1515
import { workspaceRoot } from '../../utils/workspace-root';
1616
import type { AddOptions } from './command-object';
17+
import { normalizeVersionForNxJson } from '../init/implementation/dot-nx/add-nx-scripts';
1718

1819
export function addHandler(options: AddOptions): Promise<void> {
1920
if (options.verbose) {
@@ -63,7 +64,10 @@ async function installPackage(
6364
);
6465
} else {
6566
nxJson.installation.plugins ??= {};
66-
nxJson.installation.plugins[pkgName] = version;
67+
nxJson.installation.plugins[pkgName] = normalizeVersionForNxJson(
68+
pkgName,
69+
version
70+
);
6771
writeJsonFile('nx.json', nxJson);
6872

6973
try {
@@ -113,7 +117,7 @@ async function initializePlugin(
113117

114118
try {
115119
const args = [];
116-
if (coreNxPlugins.includes(pkgName)) {
120+
if (coreNxPluginVersions.has(pkgName)) {
117121
args.push(`--keepExistingVersions`);
118122

119123
if (
@@ -171,8 +175,8 @@ function parsePackageSpecifier(
171175
const i = packageSpecifier.lastIndexOf('@');
172176

173177
if (i <= 0) {
174-
if (coreNxPlugins.includes(packageSpecifier)) {
175-
return [packageSpecifier, nxVersion];
178+
if (coreNxPluginVersions.has(packageSpecifier)) {
179+
return [packageSpecifier, coreNxPluginVersions.get(packageSpecifier)];
176180
}
177181

178182
return [packageSpecifier, 'latest'];
@@ -184,31 +188,14 @@ function parsePackageSpecifier(
184188
return [pkgName, version];
185189
}
186190

187-
const coreNxPlugins = [
188-
'@nx/angular',
189-
'@nx/cypress',
190-
'@nx/detox',
191-
'@nx/devkit',
192-
'@nx/esbuild',
193-
'@nx/eslint',
194-
'@nx/eslint-plugin',
195-
'@nx/expo',
196-
'@nx/express',
197-
'@nx/jest',
198-
'@nx/nest',
199-
'@nx/next',
200-
'@nx/node',
201-
'@nx/nuxt',
202-
'@nx/playwright',
203-
'@nx/plugin',
204-
'@nx/react',
205-
'@nx/react-native',
206-
'@nx/remix',
207-
'@nx/rollup',
208-
'@nx/storybook',
209-
'@nx/vite',
210-
'@nx/vue',
211-
'@nx/web',
212-
'@nx/webpack',
213-
'@nx/workspace',
214-
];
191+
export const coreNxPluginVersions = (
192+
require('../../../package.json') as typeof import('../../../package.json')
193+
)['nx-migrations'].packageGroup.reduce(
194+
(map, entry) => {
195+
const packageName = typeof entry === 'string' ? entry : entry.package;
196+
const version = typeof entry === 'string' ? nxVersion : entry.version;
197+
return map.set(packageName, version);
198+
},
199+
// Package Name -> Desired Version
200+
new Map<string, string>()
201+
);

packages/nx/src/command-line/init/implementation/dot-nx/add-nx-scripts.ts

+8-19
Original file line numberDiff line numberDiff line change
@@ -46,29 +46,18 @@ export function generateDotNxSetup(version?: string) {
4646
flushChanges(host.root, changes);
4747
}
4848

49+
export function normalizeVersionForNxJson(pkg: string, version: string) {
50+
if (!valid(version)) {
51+
version = execSync(`npm view ${pkg}@${version} version`).toString();
52+
}
53+
return version.trimEnd();
54+
}
55+
4956
export function writeMinimalNxJson(host: Tree, version: string) {
5057
if (!host.exists('nx.json')) {
51-
if (!valid(version)) {
52-
version = execSync(`npm view nx@${version} version`).toString();
53-
}
5458
writeJson<NxJsonConfiguration>(host, 'nx.json', {
55-
targetDefaults: {
56-
build: {
57-
cache: true,
58-
dependsOn: ['^build'],
59-
},
60-
lint: {
61-
cache: true,
62-
},
63-
test: {
64-
cache: true,
65-
},
66-
e2e: {
67-
cache: true,
68-
},
69-
},
7059
installation: {
71-
version: version.trimEnd(),
60+
version: normalizeVersionForNxJson('nx', version),
7261
},
7362
});
7463
}

0 commit comments

Comments
 (0)