Skip to content

Commit

Permalink
fix: auto-install-peers (#4855)
Browse files Browse the repository at this point in the history
close #4820
  • Loading branch information
zkochan committed Jun 6, 2022
1 parent e338f44 commit f04fa1e
Show file tree
Hide file tree
Showing 19 changed files with 396 additions and 159 deletions.
6 changes: 6 additions & 0 deletions .changeset/strange-ants-remain.md
@@ -0,0 +1,6 @@
---
"pnpm": patch
"@pnpm/resolve-dependencies": patch
---

When the same package is found several times in the dependency graph, correctly autoinstall its missing peer dependencies at all times [#4820](https://github.com/pnpm/pnpm/issues/4820).
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -7,7 +7,8 @@
"preinstall": "npx only-allow pnpm",
"prepare": "husky install",
"pretest": "pnpm run compile-only && pnpm --dir=fixtures run prepareFixtures",
"lint": "pnpm lint:meta && syncpack list-mismatches && eslint **/src/**/*.ts **/test/**/*.ts",
"lint": "pnpm lint:meta && syncpack list-mismatches && pnpm run lint:ts",
"lint:ts": "eslint **/src/**/*.ts **/test/**/*.ts",
"test-main": "pnpm pretest && pnpm lint --quiet && run-p -r verdaccio test-pkgs-main",
"remove-temp-dir": "shx rm -rf ../pnpm_tmp",
"test-pkgs-main": "pnpm remove-temp-dir && cross-env PNPM_REGISTRY_MOCK_UPLINK=http://localhost:7348 pnpm --filter=./packages/** --filter=./privatePackages/** run --no-sort --workspace-concurrency=2 _test",
Expand Down Expand Up @@ -37,7 +38,7 @@
"@commitlint/prompt-cli": "^16.3.0",
"@pnpm/eslint-config": "workspace:*",
"@pnpm/meta-updater": "0.0.6",
"@pnpm/registry-mock": "2.17.0",
"@pnpm/registry-mock": "2.19.0",
"@pnpm/tsconfig": "workspace:*",
"@types/jest": "^27.5.2",
"@types/node": "^14.18.20",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Expand Up @@ -75,7 +75,7 @@
"@pnpm/logger": "^4.0.0",
"@pnpm/package-store": "workspace:12.1.15",
"@pnpm/prepare": "workspace:*",
"@pnpm/registry-mock": "2.17.0",
"@pnpm/registry-mock": "2.19.0",
"@pnpm/store-path": "^5.0.0",
"@pnpm/test-fixtures": "workspace:*",
"@types/fs-extra": "^9.0.13",
Expand Down
79 changes: 78 additions & 1 deletion packages/core/test/install/autoInstallPeers.ts
@@ -1,6 +1,6 @@
import { addDependenciesToPackage } from '@pnpm/core'
import { prepareEmpty, preparePackages } from '@pnpm/prepare'
import { addDistTag } from '@pnpm/registry-mock'
import { addDistTag, REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
import { testDefaults } from '../utils'

test('auto install non-optional peer dependencies', async () => {
Expand Down Expand Up @@ -61,3 +61,80 @@ test('don\'t fail on linked package, when peers are auto installed', async () =>
const updatedManifest = await addDependenciesToPackage(pkgManifest, ['peer-b'], await testDefaults({ autoInstallPeers: true }))
expect(Object.keys(updatedManifest.dependencies ?? {})).toStrictEqual(['linked', 'peer-b'])
})

test('hoist a peer dependency in order to reuse it by other dependencies, when it satisfies them', async () => {
const project = prepareEmpty()
await addDependenciesToPackage({}, ['@pnpm/xyz-parent-parent-parent-parent', '@pnpm/xyz-parent-parent-with-xyz'], await testDefaults({ autoInstallPeers: true }))
const lockfile = await project.readLockfile()
expect(Object.keys(lockfile.packages)).toStrictEqual([
'/@pnpm/x/1.0.0',
'/@pnpm/xyz-parent-parent-parent-parent/1.0.0_e5suan7fvtov6fikg25btc2odi',
'/@pnpm/xyz-parent-parent-parent/1.0.0_e5suan7fvtov6fikg25btc2odi',
'/@pnpm/xyz-parent-parent-with-xyz/1.0.0',
'/@pnpm/xyz-parent-parent/1.0.0_e5suan7fvtov6fikg25btc2odi',
'/@pnpm/xyz-parent/1.0.0_e5suan7fvtov6fikg25btc2odi',
'/@pnpm/xyz/1.0.0_e5suan7fvtov6fikg25btc2odi',
'/@pnpm/y/1.0.0',
'/@pnpm/z/1.0.0',
])
})

test('don\'t hoist a peer dependency when there is a root dependency by that name', async () => {
const project = prepareEmpty()
await addDependenciesToPackage({}, [
'@pnpm/xyz-parent-parent-parent-parent',
'@pnpm/xyz-parent-parent-with-xyz',
'@pnpm/x@npm:peer-a@1.0.0',
`http://localhost:${REGISTRY_MOCK_PORT}/@pnpm/y/-/y-2.0.0.tgz`,
], await testDefaults({ autoInstallPeers: true }))
const lockfile = await project.readLockfile()
expect(Object.keys(lockfile.packages)).toStrictEqual([
'/@pnpm/x/1.0.0',
'/@pnpm/xyz-parent-parent-parent-parent/1.0.0_c3hmehglzcfufab5hu6m6d76li',
'/@pnpm/xyz-parent-parent-parent/1.0.0_c3hmehglzcfufab5hu6m6d76li',
'/@pnpm/xyz-parent-parent-with-xyz/1.0.0',
'/@pnpm/xyz-parent-parent/1.0.0_c3hmehglzcfufab5hu6m6d76li',
'/@pnpm/xyz-parent/1.0.0_c3hmehglzcfufab5hu6m6d76li',
'/@pnpm/xyz-parent/1.0.0_e5suan7fvtov6fikg25btc2odi',
'/@pnpm/xyz/1.0.0_c3hmehglzcfufab5hu6m6d76li',
'/@pnpm/xyz/1.0.0_e5suan7fvtov6fikg25btc2odi',
'/@pnpm/y/1.0.0',
'/@pnpm/y/2.0.0',
'/@pnpm/z/1.0.0',
'/peer-a/1.0.0',
])
})

test('don\'t auto-install a peer dependency, when that dependency is in the root', async () => {
const project = prepareEmpty()
await addDependenciesToPackage({}, [
'@pnpm/xyz-parent-parent-parent-parent',
'@pnpm/x@npm:peer-a@1.0.0',
`http://localhost:${REGISTRY_MOCK_PORT}/@pnpm/y/-/y-2.0.0.tgz`,
], await testDefaults({ autoInstallPeers: true }))
const lockfile = await project.readLockfile()
expect(Object.keys(lockfile.packages)).toStrictEqual([
'/@pnpm/xyz-parent-parent-parent-parent/1.0.0_c3hmehglzcfufab5hu6m6d76li',
'/@pnpm/xyz-parent-parent-parent/1.0.0_c3hmehglzcfufab5hu6m6d76li',
'/@pnpm/xyz-parent-parent/1.0.0_c3hmehglzcfufab5hu6m6d76li',
'/@pnpm/xyz-parent/1.0.0_c3hmehglzcfufab5hu6m6d76li',
'/@pnpm/xyz/1.0.0_c3hmehglzcfufab5hu6m6d76li',
'/@pnpm/y/2.0.0',
'/@pnpm/z/1.0.0',
'/peer-a/1.0.0',
])
})

test('don\'t install the same missing peer dependency twice', async () => {
await addDistTag({ package: '@pnpm/y', version: '2.0.0', distTag: 'latest' })
const project = prepareEmpty()
await addDependenciesToPackage({}, [
'has-has-y-peer-peer',
], await testDefaults({ autoInstallPeers: true }))
const lockfile = await project.readLockfile()
expect(Object.keys(lockfile.packages)).toStrictEqual([
'/@pnpm/y/1.0.0',
'/has-has-y-peer-peer/1.0.0_c7ewbmm644hn6ztbh6kbjiyhkq',
'/has-y-peer/1.0.0_@pnpm+y@1.0.0',
])
})
2 changes: 1 addition & 1 deletion packages/headless/package.json
Expand Up @@ -22,7 +22,7 @@
"@pnpm/package-store": "workspace:12.1.15",
"@pnpm/prepare": "workspace:*",
"@pnpm/read-projects-context": "workspace:5.0.19",
"@pnpm/registry-mock": "2.17.0",
"@pnpm/registry-mock": "2.19.0",
"@pnpm/store-path": "^5.0.0",
"@pnpm/test-fixtures": "workspace:*",
"@types/fs-extra": "^9.0.13",
Expand Down
2 changes: 1 addition & 1 deletion packages/package-requester/package.json
Expand Up @@ -65,7 +65,7 @@
"@pnpm/logger": "^4.0.0",
"@pnpm/package-requester": "workspace:17.0.3",
"@pnpm/package-store": "workspace:12.1.15",
"@pnpm/registry-mock": "2.17.0",
"@pnpm/registry-mock": "2.19.0",
"@pnpm/test-fixtures": "workspace:*",
"@types/normalize-path": "^3.0.0",
"@types/ramda": "0.27.39",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-commands-installation/package.json
Expand Up @@ -39,7 +39,7 @@
"@pnpm/modules-yaml": "workspace:9.1.1",
"@pnpm/plugin-commands-installation": "workspace:8.4.20",
"@pnpm/prepare": "workspace:*",
"@pnpm/registry-mock": "2.17.0",
"@pnpm/registry-mock": "2.19.0",
"@pnpm/test-fixtures": "workspace:*",
"@types/is-ci": "^3.0.0",
"@types/proxyquire": "^1.3.28",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-commands-listing/package.json
Expand Up @@ -38,7 +38,7 @@
"@pnpm/plugin-commands-installation": "workspace:8.4.20",
"@pnpm/plugin-commands-listing": "workspace:4.1.19",
"@pnpm/prepare": "workspace:*",
"@pnpm/registry-mock": "2.17.0",
"@pnpm/registry-mock": "2.19.0",
"@types/ramda": "0.27.39",
"execa": "npm:safe-execa@^0.1.1",
"strip-ansi": "^6.0.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-commands-outdated/package.json
Expand Up @@ -38,7 +38,7 @@
"@pnpm/plugin-commands-installation": "workspace:8.4.20",
"@pnpm/plugin-commands-outdated": "workspace:5.1.18",
"@pnpm/prepare": "workspace:*",
"@pnpm/registry-mock": "2.17.0",
"@pnpm/registry-mock": "2.19.0",
"@types/lru-cache": "^5.1.1",
"@types/ramda": "0.27.39",
"@types/wrap-ansi": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-commands-publishing/package.json
Expand Up @@ -39,7 +39,7 @@
"@pnpm/logger": "^4.0.0",
"@pnpm/plugin-commands-publishing": "workspace:4.5.12",
"@pnpm/prepare": "workspace:*",
"@pnpm/registry-mock": "2.17.0",
"@pnpm/registry-mock": "2.19.0",
"@types/cross-spawn": "^6.0.2",
"@types/is-ci": "^3.0.0",
"@types/is-windows": "^1.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-commands-rebuild/package.json
Expand Up @@ -37,7 +37,7 @@
"@pnpm/logger": "^4.0.0",
"@pnpm/plugin-commands-rebuild": "workspace:5.4.25",
"@pnpm/prepare": "workspace:*",
"@pnpm/registry-mock": "2.17.0",
"@pnpm/registry-mock": "2.19.0",
"@pnpm/test-fixtures": "workspace:*",
"@types/ramda": "0.27.39",
"@types/semver": "^7.3.9",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-commands-script-runners/package.json
Expand Up @@ -38,7 +38,7 @@
"@pnpm/logger": "^4.0.0",
"@pnpm/plugin-commands-script-runners": "workspace:4.6.14",
"@pnpm/prepare": "workspace:*",
"@pnpm/registry-mock": "2.17.0",
"@pnpm/registry-mock": "2.19.0",
"@types/is-windows": "^1.0.0",
"@types/ramda": "0.27.39",
"is-windows": "^1.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-commands-store/package.json
Expand Up @@ -38,7 +38,7 @@
"@pnpm/logger": "^4.0.0",
"@pnpm/plugin-commands-store": "workspace:4.1.23",
"@pnpm/prepare": "workspace:*",
"@pnpm/registry-mock": "2.17.0",
"@pnpm/registry-mock": "2.19.0",
"@types/archy": "0.0.31",
"@types/ramda": "0.27.39",
"@types/ssri": "^7.1.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/pnpm/package.json
Expand Up @@ -54,7 +54,7 @@
"@pnpm/prepare": "workspace:*",
"@pnpm/read-package-json": "workspace:5.0.12",
"@pnpm/read-project-manifest": "workspace:2.0.13",
"@pnpm/registry-mock": "2.17.0",
"@pnpm/registry-mock": "2.19.0",
"@pnpm/run-npm": "workspace:3.1.1",
"@pnpm/store-path": "^5.0.0",
"@pnpm/tabtab": "^0.1.2",
Expand Down

0 comments on commit f04fa1e

Please sign in to comment.