Skip to content

Commit

Permalink
fix: auto installing peer dep in a workspace (#5307)
Browse files Browse the repository at this point in the history
* fix: auto installing peer dep in a workspace

close #5144

* fix: auto installing peer dep in a workspace

* fix: auto installing peer dep in a workspace
  • Loading branch information
zkochan committed Sep 5, 2022
1 parent 728c0cd commit 2acf38b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .changeset/twenty-dragons-explode.md
@@ -0,0 +1,7 @@
---
"@pnpm/core": patch
"@pnpm/resolve-dependencies": patch
"pnpm": patch
---

Auto installing a peer dependency in a workspace that also has it as a dev dependency in another project [#5144](https://github.com/pnpm/pnpm/issues/5144).
40 changes: 40 additions & 0 deletions packages/core/test/install/autoInstallPeers.ts
@@ -1,3 +1,5 @@
import path from 'path'
import assertProject from '@pnpm/assert-project'
import { addDependenciesToPackage, install, mutateModules } from '@pnpm/core'
import { prepareEmpty, preparePackages } from '@pnpm/prepare'
import { addDistTag, REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
Expand Down Expand Up @@ -226,3 +228,41 @@ test('automatically install root peer dependencies', async () => {
})
}
})

test('automatically install peer dependency when it is a dev dependency in another workspace project', async () => {
prepareEmpty()

await mutateModules([
{
buildIndex: 0,
manifest: {
name: 'project-1',
devDependencies: {
'is-positive': '1.0.0',
},
},
mutation: 'install',
rootDir: path.resolve('project-1'),
},
{
buildIndex: 0,
manifest: {
name: 'project-2',
peerDependencies: {
'is-positive': '1.0.0',
},
},
mutation: 'install',
rootDir: path.resolve('project-2'),
},
], await testDefaults({ autoInstallPeers: true }))

const project = assertProject(process.cwd())
const lockfile = await project.readLockfile()
expect(lockfile.importers['project-1'].devDependencies).toStrictEqual({
'is-positive': '1.0.0',
})
expect(lockfile.importers['project-2'].dependencies).toStrictEqual({
'is-positive': '1.0.0',
})
})
19 changes: 11 additions & 8 deletions packages/resolve-dependencies/src/index.ts
Expand Up @@ -135,6 +135,16 @@ export default async function (
}

if (updatedManifest != null) {
if (opts.autoInstallPeers) {
if (updatedManifest.peerDependencies) {
const allDeps = getAllDependenciesFromManifest(updatedManifest)
for (const [peerName, peerRange] of Object.entries(updatedManifest.peerDependencies)) {
if (allDeps[peerName]) continue
updatedManifest.dependencies ??= {}
updatedManifest.dependencies[peerName] = peerRange
}
}
}
const projectSnapshot = opts.wantedLockfile.importers[project.id]
opts.wantedLockfile.importers[project.id] = addDirectDependenciesToLockfile(
updatedManifest,
Expand Down Expand Up @@ -334,14 +344,7 @@ function addDirectDependenciesToLockfile (
return acc
}, {})

let allDepsObj = getAllDependenciesFromManifest(newManifest)
if (autoInstallPeers) {
allDepsObj = {
...newManifest.peerDependencies,
...allDepsObj,
}
}
const allDeps = Array.from(new Set(Object.keys(allDepsObj)))
const allDeps = Array.from(new Set(Object.keys(getAllDependenciesFromManifest(newManifest))))

for (const alias of allDeps) {
if (directDependenciesByAlias[alias]) {
Expand Down

0 comments on commit 2acf38b

Please sign in to comment.