Skip to content

Commit

Permalink
fix: don't modify the inject project's manifest (#5294)
Browse files Browse the repository at this point in the history
  • Loading branch information
zkochan committed Sep 1, 2022
1 parent 678933a commit 53506c7
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .changeset/quiet-carrots-exist.md
@@ -0,0 +1,7 @@
---
"@pnpm/npm-resolver": patch
"@pnpm/resolve-dependencies": patch
"pnpm": patch
---

Don't modify the manifest of the injected workspace project, when it has the same dependency in prod and peer dependencies.
81 changes: 81 additions & 0 deletions packages/core/test/install/injectLocalPackages.ts
Expand Up @@ -1296,3 +1296,84 @@ test('peer dependency of injected project should be resolved correctly', async (
const lockfile = await rootModules.readLockfile()
expect(lockfile.packages?.['file:project-2_project-1@project-1'].dependencies?.['project-1']).toEqual('link:project-1')
})

// There was a bug related to this. The manifests in the workspacePackages object were modified
test('do not modify the manifest of the injected workpspace project', async () => {
const project1Manifest = {
name: 'project-1',
version: '1.0.0',
dependencies: {
'is-positive': '1.0.0',
},
peerDependencies: {
'is-positive': '>=1.0.0',
},
}
const project2Manifest = {
name: 'project-2',
version: '1.0.0',
dependencies: {
'project-1': 'workspace:1.0.0',
},
devDependencies: {
'is-positive': '1.0.0',
},
dependenciesMeta: {
'project-1': {
injected: true,
},
},
}
preparePackages([
{
location: 'project-1',
package: project1Manifest,
},
{
location: 'project-2',
package: project2Manifest,
},
])

const importers: MutatedProject[] = [
{
buildIndex: 0,
manifest: project1Manifest,
mutation: 'install',
rootDir: path.resolve('project-1'),
},
{
buildIndex: 0,
manifest: project2Manifest,
mutation: 'install',
rootDir: path.resolve('project-2'),
},
]
const workspacePackages = {
'project-1': {
'1.0.0': {
dir: path.resolve('project-1'),
manifest: project1Manifest,
},
},
'project-2': {
'1.0.0': {
dir: path.resolve('project-2'),
manifest: project2Manifest,
},
},
}
const [project1] = await mutateModules(importers, await testDefaults({
workspacePackages,
}))
expect(project1.manifest).toStrictEqual({
name: 'project-1',
version: '1.0.0',
dependencies: {
'is-positive': '1.0.0',
},
peerDependencies: {
'is-positive': '>=1.0.0',
},
})
})
2 changes: 2 additions & 0 deletions packages/npm-resolver/package.json
Expand Up @@ -50,6 +50,7 @@
"p-memoize": "4.0.1",
"parse-npm-tarball-url": "^3.0.0",
"path-temp": "^2.0.0",
"ramda": "npm:@pnpm/ramda@0.28.1",
"rename-overwrite": "^4.0.2",
"semver": "^7.3.7",
"ssri": "^9.0.1",
Expand All @@ -61,6 +62,7 @@
"@pnpm/npm-resolver": "workspace:*",
"@pnpm/test-fixtures": "workspace:*",
"@types/normalize-path": "^3.0.0",
"@types/ramda": "0.28.15",
"@types/semver": "7.3.10",
"@types/ssri": "^7.1.1",
"nock": "13.2.9",
Expand Down
3 changes: 2 additions & 1 deletion packages/npm-resolver/src/index.ts
Expand Up @@ -16,6 +16,7 @@ import { DependencyManifest } from '@pnpm/types'
import LRU from 'lru-cache'
import normalize from 'normalize-path'
import pMemoize from 'p-memoize'
import clone from 'ramda/src/clone'
import semver from 'semver'
import ssri from 'ssri'
import pickPackage, {
Expand Down Expand Up @@ -317,7 +318,7 @@ function resolveFromLocalPackage (
}
return {
id,
manifest: localPackage.manifest,
manifest: clone(localPackage.manifest),
normalizedPref,
resolution: {
directory,
Expand Down
16 changes: 10 additions & 6 deletions packages/resolve-dependencies/src/resolveDependencies.ts
Expand Up @@ -41,6 +41,7 @@ import {
import * as dp from 'dependency-path'
import exists from 'path-exists'
import isEmpty from 'ramda/src/isEmpty'
import omit from 'ramda/src/omit'
import zipWith from 'ramda/src/zipWith'
import semver from 'semver'
import encodePkgId from './encodePkgId'
Expand Down Expand Up @@ -935,14 +936,17 @@ async function resolveDependency (
}
if (pkg.peerDependencies && pkg.dependencies) {
if (ctx.autoInstallPeers) {
for (const peerDep of Object.keys(pkg.peerDependencies)) {
delete pkg.dependencies[peerDep]
pkg = {
...pkg,
dependencies: omit(Object.keys(pkg.peerDependencies), pkg.dependencies),
}
} else {
for (const peerDep of Object.keys(pkg.peerDependencies)) {
if (options.parentPkgAliases[peerDep]) {
delete pkg.dependencies[peerDep]
}
pkg = {
...pkg,
dependencies: omit(
Object.keys(pkg.peerDependencies).filter((peerDep) => options.parentPkgAliases[peerDep]),
pkg.dependencies
),
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 53506c7

Please sign in to comment.