Skip to content

Commit

Permalink
fix: --lockfile-only and readPackage hook modifying workspace pac…
Browse files Browse the repository at this point in the history
…kages (#5678)

close #5670
  • Loading branch information
Silic0nS0ldier committed Nov 26, 2022
1 parent c615856 commit 868f2fb
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changeset/proud-schools-jump.md
@@ -0,0 +1,7 @@
---
"@pnpm/core": patch
"@pnpm/plugin-commands-installation": patch
"pnpm": patch
---

readPackage hooks should not modify the `package.json` files in a workspace [#5670](https://github.com/pnpm/pnpm/issues/5670).
1 change: 1 addition & 0 deletions pkg-manager/core/src/install/index.ts
Expand Up @@ -690,6 +690,7 @@ export type ImporterToUpdate = {
} & DependenciesMutation

export interface UpdatedProject {
originalManifest?: ProjectManifest
manifest: ProjectManifest
peerDependencyIssues?: PeerDependencyIssues
rootDir: string
Expand Down
2 changes: 1 addition & 1 deletion pkg-manager/plugin-commands-installation/src/recursive.ts
Expand Up @@ -257,7 +257,7 @@ export async function recursive (
if (opts.save !== false) {
await Promise.all(
mutatedPkgs
.map(async ({ manifest }, index) => writeProjectManifests[index](manifest))
.map(async ({ originalManifest, manifest }, index) => writeProjectManifests[index](originalManifest ?? manifest))
)
}
return true
Expand Down
25 changes: 25 additions & 0 deletions pnpm/test/hooks.ts
Expand Up @@ -37,6 +37,18 @@ test('readPackage hook in single project doesn\'t modify manifest', async () =>
pkg = await loadJsonFile(path.resolve('package.json'))
expect(pkg.dependencies).toBeFalsy() // remove & readPackage hook work
await project.hasNot('is-positive')

// Reset for --lockfile-only checks
await fs.unlink('pnpm-lock.yaml');

await execPnpm(['install', '--lockfile-only'])
pkg = await loadJsonFile(path.resolve('package.json'))
expect(pkg.dependencies).toBeFalsy() // install --lockfile-only & readPackage hook work, without pnpm-lock.yaml

// runs with pnpm-lock.yaml should not mutate local projects
await execPnpm(['install', '--lockfile-only'])
pkg = await loadJsonFile(path.resolve('package.json'))
expect(pkg.dependencies).toBeFalsy() // install --lockfile-only & readPackage hook work, with pnpm-lock.yaml
})

test('readPackage hook in monorepo doesn\'t modify manifest', async () => {
Expand Down Expand Up @@ -79,6 +91,19 @@ test('readPackage hook in monorepo doesn\'t modify manifest', async () => {
await execPnpm(['remove', 'is-positive', '--filter', 'project-a'])
pkg = await loadJsonFile(path.resolve('project-a/package.json'))
expect(pkg.dependencies).toBeFalsy() // remove & readPackage hook work

// Reset for --lockfile-only checks
await fs.unlink('pnpm-lock.yaml');

await execPnpm(['install', '--lockfile-only'])
pkg = await loadJsonFile(path.resolve('project-a/package.json'))
expect(pkg.dependencies).toBeFalsy() // install --lockfile-only & readPackage hook work, without pnpm-lock.yaml

// runs with pnpm-lock.yaml should not mutate local projects
await execPnpm(['install', '--lockfile-only'])
pkg = await loadJsonFile(path.resolve('project-a/package.json'))
expect(pkg.dependencies).toBeFalsy() // install --lockfile-only & readPackage hook work, with pnpm-lock.yaml

})

test('filterLog hook filters peer dependency warning', async () => {
Expand Down

0 comments on commit 868f2fb

Please sign in to comment.