Skip to content

Commit

Permalink
fix(misc): dot nx setup shouldn't include target defaults (#23180)
Browse files Browse the repository at this point in the history
<!-- 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 #
  • Loading branch information
AgentEnder authored May 3, 2024
1 parent 84eb280 commit 30446df
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 59 deletions.
16 changes: 8 additions & 8 deletions e2e/nx/src/nxw.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ describe('nx wrapper / .nx installation', () => {

beforeAll(() => {
runNxWrapper = newWrappedNxWorkspace();
updateJson<NxJsonConfiguration>('nx.json', (json) => {
json.targetDefaults ??= {};
json.targetDefaults.echo = { cache: true };
json.installation.plugins = {
'@nx/js': getPublishedVersion(),
};
return json;
});
});

afterAll(() => {
Expand All @@ -39,14 +47,6 @@ describe('nx wrapper / .nx installation', () => {
})
);

updateJson<NxJsonConfiguration>('nx.json', (json) => {
json.targetDefaults.echo = { cache: true };
json.installation.plugins = {
'@nx/js': getPublishedVersion(),
};
return json;
});

expect(runNxWrapper('echo a')).toContain('Hello from A');

expect(runNxWrapper('echo a')).toContain(
Expand Down
13 changes: 13 additions & 0 deletions packages/nx/src/command-line/add/add.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { nxVersion } from '../../utils/versions';
import { coreNxPluginVersions } from './add';

describe('nx core packages', () => {
it('should map nx packages to nx version', () => {
expect(coreNxPluginVersions.get('@nx/workspace')).toEqual(nxVersion);
});

it('should map nx-cloud to latest', () => {
expect(coreNxPluginVersions.get('@nrwl/nx-cloud')).toEqual('latest');
expect(coreNxPluginVersions.get('nx-cloud')).toEqual('latest');
});
});
51 changes: 19 additions & 32 deletions packages/nx/src/command-line/add/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { getPluginCapabilities } from '../../utils/plugins';
import { nxVersion } from '../../utils/versions';
import { workspaceRoot } from '../../utils/workspace-root';
import type { AddOptions } from './command-object';
import { normalizeVersionForNxJson } from '../init/implementation/dot-nx/add-nx-scripts';

export function addHandler(options: AddOptions): Promise<void> {
if (options.verbose) {
Expand Down Expand Up @@ -63,7 +64,10 @@ async function installPackage(
);
} else {
nxJson.installation.plugins ??= {};
nxJson.installation.plugins[pkgName] = version;
nxJson.installation.plugins[pkgName] = normalizeVersionForNxJson(
pkgName,
version
);
writeJsonFile('nx.json', nxJson);

try {
Expand Down Expand Up @@ -113,7 +117,7 @@ async function initializePlugin(

try {
const args = [];
if (coreNxPlugins.includes(pkgName)) {
if (coreNxPluginVersions.has(pkgName)) {
args.push(`--keepExistingVersions`);

if (
Expand Down Expand Up @@ -171,8 +175,8 @@ function parsePackageSpecifier(
const i = packageSpecifier.lastIndexOf('@');

if (i <= 0) {
if (coreNxPlugins.includes(packageSpecifier)) {
return [packageSpecifier, nxVersion];
if (coreNxPluginVersions.has(packageSpecifier)) {
return [packageSpecifier, coreNxPluginVersions.get(packageSpecifier)];
}

return [packageSpecifier, 'latest'];
Expand All @@ -184,31 +188,14 @@ function parsePackageSpecifier(
return [pkgName, version];
}

const coreNxPlugins = [
'@nx/angular',
'@nx/cypress',
'@nx/detox',
'@nx/devkit',
'@nx/esbuild',
'@nx/eslint',
'@nx/eslint-plugin',
'@nx/expo',
'@nx/express',
'@nx/jest',
'@nx/nest',
'@nx/next',
'@nx/node',
'@nx/nuxt',
'@nx/playwright',
'@nx/plugin',
'@nx/react',
'@nx/react-native',
'@nx/remix',
'@nx/rollup',
'@nx/storybook',
'@nx/vite',
'@nx/vue',
'@nx/web',
'@nx/webpack',
'@nx/workspace',
];
export const coreNxPluginVersions = (
require('../../../package.json') as typeof import('../../../package.json')
)['nx-migrations'].packageGroup.reduce(
(map, entry) => {
const packageName = typeof entry === 'string' ? entry : entry.package;
const version = typeof entry === 'string' ? nxVersion : entry.version;
return map.set(packageName, version);
},
// Package Name -> Desired Version
new Map<string, string>()
);
Original file line number Diff line number Diff line change
Expand Up @@ -46,29 +46,18 @@ export function generateDotNxSetup(version?: string) {
flushChanges(host.root, changes);
}

export function normalizeVersionForNxJson(pkg: string, version: string) {
if (!valid(version)) {
version = execSync(`npm view ${pkg}@${version} version`).toString();
}
return version.trimEnd();
}

export function writeMinimalNxJson(host: Tree, version: string) {
if (!host.exists('nx.json')) {
if (!valid(version)) {
version = execSync(`npm view nx@${version} version`).toString();
}
writeJson<NxJsonConfiguration>(host, 'nx.json', {
targetDefaults: {
build: {
cache: true,
dependsOn: ['^build'],
},
lint: {
cache: true,
},
test: {
cache: true,
},
e2e: {
cache: true,
},
},
installation: {
version: version.trimEnd(),
version: normalizeVersionForNxJson('nx', version),
},
});
}
Expand Down

0 comments on commit 30446df

Please sign in to comment.