From 3dea7e2ffa0cc209969ddd5140248c58c380321c Mon Sep 17 00:00:00 2001 From: Simon Abbott Date: Wed, 22 Jan 2025 17:44:10 -0600 Subject: [PATCH] fix(core): don't set legacy-peer-deps by default Changing the value of `legacy-peer-deps` causes npm to install a different graph, which can break builds and the `npm ci` command. Any users who rely on `legacy-peer-deps` should already have it set in an `.npmrc` file, so this should not affect them. It does, however, fix bugs with using `nx` in repos that _don't_ have `legacy-peer-deps` enabled, which is the vast majority of them. Fixes #22066, fixes #29537 --- .../migration/adding-to-existing-project.md | 2 +- docs/shared/migration/adding-to-monorepo.md | 2 +- docs/shared/migration/migration-angular.md | 2 +- docs/shared/tutorials/angular-monorepo.md | 2 +- docs/shared/tutorials/angular-standalone.md | 2 +- docs/shared/tutorials/react-monorepo.md | 2 +- docs/shared/tutorials/react-standalone.md | 2 +- docs/shared/tutorials/typescript-packages.md | 2 +- docs/shared/tutorials/vue-standalone.md | 2 +- e2e/release/src/lock-file-updates.test.ts | 38 ++++++++++++++ e2e/utils/command-utils.ts | 4 +- .../lib/add-ngrx-to-package-json.ts | 2 - .../lib/add-ngrx-to-package-json.ts | 2 - .../ngrx/lib/add-ngrx-to-package-json.ts | 2 - .../angular/src/generators/utils/add-jest.ts | 2 - .../nx/src/command-line/migrate/migrate.ts | 4 -- packages/nx/src/utils/package-manager.ts | 5 +- .../__snapshots__/ci-workflow.spec.ts.snap | 52 +++++++++---------- 18 files changed, 76 insertions(+), 53 deletions(-) diff --git a/docs/shared/migration/adding-to-existing-project.md b/docs/shared/migration/adding-to-existing-project.md index 56829e60bbfbd..1c1e10e09a8ae 100644 --- a/docs/shared/migration/adding-to-existing-project.md +++ b/docs/shared/migration/adding-to-existing-project.md @@ -358,7 +358,7 @@ jobs: with: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - uses: nrwl/nx-set-shas@v4 # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: npx nx affected -t lint test build diff --git a/docs/shared/migration/adding-to-monorepo.md b/docs/shared/migration/adding-to-monorepo.md index 6c8ccad8ea36f..4f7009fe4e0e6 100644 --- a/docs/shared/migration/adding-to-monorepo.md +++ b/docs/shared/migration/adding-to-monorepo.md @@ -377,7 +377,7 @@ jobs: with: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - uses: nrwl/nx-set-shas@v4 # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: npx nx affected -t lint test build diff --git a/docs/shared/migration/migration-angular.md b/docs/shared/migration/migration-angular.md index e45cc985b722d..663edd2ed7848 100644 --- a/docs/shared/migration/migration-angular.md +++ b/docs/shared/migration/migration-angular.md @@ -180,7 +180,7 @@ jobs: with: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - uses: nrwl/nx-set-shas@v4 # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected - run: npx nx affected -t lint test build diff --git a/docs/shared/tutorials/angular-monorepo.md b/docs/shared/tutorials/angular-monorepo.md index 190ca91e4dd52..a7c8f83001bc3 100644 --- a/docs/shared/tutorials/angular-monorepo.md +++ b/docs/shared/tutorials/angular-monorepo.md @@ -1303,7 +1303,7 @@ jobs: with: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - uses: nrwl/nx-set-shas@v4 # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected # When you enable task distribution, run the e2e-ci task instead of e2e diff --git a/docs/shared/tutorials/angular-standalone.md b/docs/shared/tutorials/angular-standalone.md index b4f4be03773e0..e18a23742d64c 100644 --- a/docs/shared/tutorials/angular-standalone.md +++ b/docs/shared/tutorials/angular-standalone.md @@ -1104,7 +1104,7 @@ jobs: with: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - uses: nrwl/nx-set-shas@v4 # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected # When you enable task distribution, run the e2e-ci task instead of e2e diff --git a/docs/shared/tutorials/react-monorepo.md b/docs/shared/tutorials/react-monorepo.md index fc1513587d352..22d6f38954a7c 100644 --- a/docs/shared/tutorials/react-monorepo.md +++ b/docs/shared/tutorials/react-monorepo.md @@ -1106,7 +1106,7 @@ jobs: with: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - uses: nrwl/nx-set-shas@v4 # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected # When you enable task distribution, run the e2e-ci task instead of e2e diff --git a/docs/shared/tutorials/react-standalone.md b/docs/shared/tutorials/react-standalone.md index c3044b90c47db..7d60651181741 100644 --- a/docs/shared/tutorials/react-standalone.md +++ b/docs/shared/tutorials/react-standalone.md @@ -1055,7 +1055,7 @@ jobs: with: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - uses: nrwl/nx-set-shas@v4 # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected # When you enable task distribution, run the e2e-ci task instead of e2e diff --git a/docs/shared/tutorials/typescript-packages.md b/docs/shared/tutorials/typescript-packages.md index 0342a4b1bb782..7045cfb1c4d2b 100644 --- a/docs/shared/tutorials/typescript-packages.md +++ b/docs/shared/tutorials/typescript-packages.md @@ -462,7 +462,7 @@ jobs: with: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - uses: nrwl/nx-set-shas@v4 # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected # When you enable task distribution, run the e2e-ci task instead of e2e diff --git a/docs/shared/tutorials/vue-standalone.md b/docs/shared/tutorials/vue-standalone.md index 1f5a232def46a..582b2816063ba 100644 --- a/docs/shared/tutorials/vue-standalone.md +++ b/docs/shared/tutorials/vue-standalone.md @@ -1104,7 +1104,7 @@ jobs: with: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - uses: nrwl/nx-set-shas@v4 # Nx Affected runs only tasks affected by the changes in this PR/commit. Learn more: https://nx.dev/ci/features/affected # When you enable task distribution, run the e2e-ci task instead of e2e diff --git a/e2e/release/src/lock-file-updates.test.ts b/e2e/release/src/lock-file-updates.test.ts index 28fc24633ffe7..9720515500104 100644 --- a/e2e/release/src/lock-file-updates.test.ts +++ b/e2e/release/src/lock-file-updates.test.ts @@ -1,6 +1,8 @@ import { + checkFilesExist, cleanupProject, newProject, + readJson, runCLI, runCommand, uniq, @@ -154,6 +156,42 @@ describe('nx release lock file updates', () => { `); }); + it('should not mess with peer dependencies when package manager is npm', async () => { + initializeProject('npm'); + + updateJson('package.json', (json) => { + json.workspaces = [pkg1, pkg2, pkg3]; + return json; + }); + + updateJson(`${pkg1}/package.json`, (json) => { + json.peerDependencies = { + semver: '^7.3.2', + }; + return json; + }); + + runCommand(`npm install`); + + // Make sure that the peer dependency was installed, and that the lock file + // is up-to-date, otherwise we are testing nothing. + checkFilesExist('node_modules/semver/package.json'); + runCommand('npm ci', { failOnError: true }); + + // workaround for NXC-143 + runCLI('reset'); + + runCommand(`git add .`); + runCommand(`git commit -m "chore: initial commit"`); + + runCLI(`release version 999.9.9`); + + // Make sure that the lock file is still valid, and that the peer dependency + // was not removed. + runCommand('npm ci', { failOnError: true }); + checkFilesExist('node_modules/semver/package.json'); + }); + it('should not update lock file when package manager is yarn classic', async () => { initializeProject('yarn'); diff --git a/e2e/utils/command-utils.ts b/e2e/utils/command-utils.ts index 4c6907291a3dc..fc0787108a798 100644 --- a/e2e/utils/command-utils.ts +++ b/e2e/utils/command-utils.ts @@ -131,8 +131,8 @@ export function getPackageManagerCommand({ runUninstalledPackage: `npx --yes`, install: 'npm install', ciInstall: 'npm ci', - addProd: `npm install --legacy-peer-deps`, - addDev: `npm install --legacy-peer-deps -D`, + addProd: `npm install`, + addDev: `npm install -D`, list: 'npm ls --depth 10', runLerna: `npx lerna`, }, diff --git a/packages/angular/src/generators/ngrx-feature-store/lib/add-ngrx-to-package-json.ts b/packages/angular/src/generators/ngrx-feature-store/lib/add-ngrx-to-package-json.ts index d501aab02845d..f09a67e8b0300 100644 --- a/packages/angular/src/generators/ngrx-feature-store/lib/add-ngrx-to-package-json.ts +++ b/packages/angular/src/generators/ngrx-feature-store/lib/add-ngrx-to-package-json.ts @@ -13,8 +13,6 @@ export function addNgRxToPackageJson( : '~0.8.3'; const ngrxVersion = versions(tree).ngrxVersion; - process.env.npm_config_legacy_peer_deps ??= 'true'; - return addDependenciesToPackageJson( tree, { diff --git a/packages/angular/src/generators/ngrx-root-store/lib/add-ngrx-to-package-json.ts b/packages/angular/src/generators/ngrx-root-store/lib/add-ngrx-to-package-json.ts index ccd0f75092857..48c15a95cc517 100644 --- a/packages/angular/src/generators/ngrx-root-store/lib/add-ngrx-to-package-json.ts +++ b/packages/angular/src/generators/ngrx-root-store/lib/add-ngrx-to-package-json.ts @@ -13,8 +13,6 @@ export function addNgRxToPackageJson( : '~0.8.3'; const ngrxVersion = versions(tree).ngrxVersion; - process.env.npm_config_legacy_peer_deps ??= 'true'; - return addDependenciesToPackageJson( tree, { diff --git a/packages/angular/src/generators/ngrx/lib/add-ngrx-to-package-json.ts b/packages/angular/src/generators/ngrx/lib/add-ngrx-to-package-json.ts index ab45c546da0e2..101170337e3f2 100644 --- a/packages/angular/src/generators/ngrx/lib/add-ngrx-to-package-json.ts +++ b/packages/angular/src/generators/ngrx/lib/add-ngrx-to-package-json.ts @@ -13,8 +13,6 @@ export function addNgRxToPackageJson( : '~0.8.3'; const ngrxVersion = versions(tree).ngrxVersion; - process.env.npm_config_legacy_peer_deps ??= 'true'; - return addDependenciesToPackageJson( tree, { diff --git a/packages/angular/src/generators/utils/add-jest.ts b/packages/angular/src/generators/utils/add-jest.ts index 1365ced1d5265..973736612f1f0 100644 --- a/packages/angular/src/generators/utils/add-jest.ts +++ b/packages/angular/src/generators/utils/add-jest.ts @@ -18,8 +18,6 @@ export async function addJest( options: AddJestOptions ): Promise { if (!options.skipPackageJson) { - process.env.npm_config_legacy_peer_deps ??= 'true'; - addDependenciesToPackageJson( tree, {}, diff --git a/packages/nx/src/command-line/migrate/migrate.ts b/packages/nx/src/command-line/migrate/migrate.ts index 78e6718f426d2..f00410b3602d2 100644 --- a/packages/nx/src/command-line/migrate/migrate.ts +++ b/packages/nx/src/command-line/migrate/migrate.ts @@ -1382,10 +1382,6 @@ function showConnectToCloudMessage() { function runInstall() { const pmCommands = getPackageManagerCommand(); - // TODO: remove this - if (detectPackageManager() === 'npm') { - process.env.npm_config_legacy_peer_deps ??= 'true'; - } output.log({ title: `Running '${pmCommands.install}' to make sure necessary packages are installed`, }); diff --git a/packages/nx/src/utils/package-manager.ts b/packages/nx/src/utils/package-manager.ts index da88ac1edb959..e9007767f03d7 100644 --- a/packages/nx/src/utils/package-manager.ts +++ b/packages/nx/src/utils/package-manager.ts @@ -166,12 +166,9 @@ export function getPackageManagerCommand( }; }, npm: () => { - // TODO: Remove this - process.env.npm_config_legacy_peer_deps ??= 'true'; - return { install: 'npm install', - ciInstall: 'npm ci --legacy-peer-deps', + ciInstall: 'npm ci', updateLockFile: 'npm install --package-lock-only', add: 'npm install', addDev: 'npm install -D', diff --git a/packages/workspace/src/generators/ci-workflow/__snapshots__/ci-workflow.spec.ts.snap b/packages/workspace/src/generators/ci-workflow/__snapshots__/ci-workflow.spec.ts.snap index 595ddaf931065..1299a2eb124b8 100644 --- a/packages/workspace/src/generators/ci-workflow/__snapshots__/ci-workflow.spec.ts.snap +++ b/packages/workspace/src/generators/ci-workflow/__snapshots__/ci-workflow.spec.ts.snap @@ -50,7 +50,7 @@ jobs: # Uncomment this line to enable task distribution # - script: npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="e2e-ci" - - script: npm ci --legacy-peer-deps + - script: npm ci - script: npx cypress install - script: git branch --track main origin/main condition: eq(variables['Build.Reason'], 'PullRequest') @@ -83,7 +83,7 @@ pipelines: # Uncomment this line to enable task distribution # - npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="e2e-ci" - - npm ci --legacy-peer-deps + - npm ci - npx cypress install # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud @@ -103,7 +103,7 @@ pipelines: # Learn more at https://nx.dev/ci/reference/nx-cloud-cli#npx-nxcloud-startcirun - npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="e2e-ci" - - npm ci --legacy-peer-deps + - npm ci # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - npx nx-cloud record -- echo Hello World @@ -131,7 +131,7 @@ jobs: # Uncomment this line to enable task distribution # - run: npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="e2e-ci" - - run: npm ci --legacy-peer-deps + - run: npm ci - run: npx cypress install - nx/set-shas: main-branch-name: 'main' @@ -184,7 +184,7 @@ jobs: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - run: npx cypress install - uses: nrwl/nx-set-shas@v4 @@ -229,7 +229,7 @@ jobs: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - run: npx cypress install - uses: nrwl/nx-set-shas@v4 @@ -560,7 +560,7 @@ jobs: # Uncomment this line to enable task distribution # - script: npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build" - - script: npm ci --legacy-peer-deps + - script: npm ci - script: git branch --track main origin/main condition: eq(variables['Build.Reason'], 'PullRequest') @@ -591,7 +591,7 @@ pipelines: # Uncomment this line to enable task distribution # - npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build" - - npm ci --legacy-peer-deps + - npm ci # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # npx nx-cloud record -- echo Hello World @@ -609,7 +609,7 @@ pipelines: # Learn more at https://nx.dev/ci/reference/nx-cloud-cli#npx-nxcloud-startcirun - npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build" - - npm ci --legacy-peer-deps + - npm ci # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - npx nx-cloud record -- echo Hello World @@ -637,7 +637,7 @@ jobs: # Uncomment this line to enable task distribution # - run: npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build" - - run: npm ci --legacy-peer-deps + - run: npm ci - nx/set-shas: main-branch-name: 'main' @@ -688,7 +688,7 @@ jobs: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - uses: nrwl/nx-set-shas@v4 # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud @@ -731,7 +731,7 @@ jobs: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - uses: nrwl/nx-set-shas@v4 # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud @@ -759,7 +759,7 @@ CI: # Uncomment this line to enable task distribution # - npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build" - - npm ci --legacy-peer-deps + - npm ci - NX_HEAD=$CI_COMMIT_SHA - NX_BASE=\${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA} @@ -1361,7 +1361,7 @@ jobs: # Connect your workspace by running "nx connect" and uncomment this line to enable task distribution # - script: npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="e2e-ci" - - script: npm ci --legacy-peer-deps + - script: npm ci - script: npx cypress install - script: git branch --track main origin/main condition: eq(variables['Build.Reason'], 'PullRequest') @@ -1394,7 +1394,7 @@ pipelines: # Connect your workspace by running "nx connect" and uncomment this line to enable task distribution # - npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="e2e-ci" - - npm ci --legacy-peer-deps + - npm ci - npx cypress install # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud @@ -1415,7 +1415,7 @@ pipelines: # Connect your workspace by running "nx connect" and uncomment this # - npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="e2e-ci" - - npm ci --legacy-peer-deps + - npm ci # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - npx nx-cloud record -- echo Hello World @@ -1443,7 +1443,7 @@ jobs: # Connect your workspace by running "nx connect" and uncomment this line to enable task distribution # - run: npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="e2e-ci" - - run: npm ci --legacy-peer-deps + - run: npm ci - run: npx cypress install - nx/set-shas: main-branch-name: 'main' @@ -1496,7 +1496,7 @@ jobs: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - run: npx cypress install - uses: nrwl/nx-set-shas@v4 @@ -1541,7 +1541,7 @@ jobs: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - run: npx cypress install - uses: nrwl/nx-set-shas@v4 @@ -1873,7 +1873,7 @@ jobs: # Connect your workspace by running "nx connect" and uncomment this line to enable task distribution # - script: npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build" - - script: npm ci --legacy-peer-deps + - script: npm ci - script: git branch --track main origin/main condition: eq(variables['Build.Reason'], 'PullRequest') @@ -1904,7 +1904,7 @@ pipelines: # Connect your workspace by running "nx connect" and uncomment this line to enable task distribution # - npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build" - - npm ci --legacy-peer-deps + - npm ci # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # npx nx-cloud record -- echo Hello World @@ -1923,7 +1923,7 @@ pipelines: # Connect your workspace by running "nx connect" and uncomment this # - npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build" - - npm ci --legacy-peer-deps + - npm ci # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud # - npx nx-cloud record -- echo Hello World @@ -1951,7 +1951,7 @@ jobs: # Connect your workspace by running "nx connect" and uncomment this line to enable task distribution # - run: npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build" - - run: npm ci --legacy-peer-deps + - run: npm ci - nx/set-shas: main-branch-name: 'main' @@ -2002,7 +2002,7 @@ jobs: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - uses: nrwl/nx-set-shas@v4 # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud @@ -2045,7 +2045,7 @@ jobs: node-version: 20 cache: 'npm' - - run: npm ci --legacy-peer-deps + - run: npm ci - uses: nrwl/nx-set-shas@v4 # Prepend any command with "nx-cloud record --" to record its logs to Nx Cloud @@ -2073,7 +2073,7 @@ CI: # Connect your workspace by running "nx connect" and uncomment this line to enable task distribution # - npx nx-cloud start-ci-run --distribute-on="3 linux-medium-js" --stop-agents-after="build" - - npm ci --legacy-peer-deps + - npm ci - NX_HEAD=$CI_COMMIT_SHA - NX_BASE=\${CI_MERGE_REQUEST_DIFF_BASE_SHA:-$CI_COMMIT_BEFORE_SHA}