fix: approve builds command writes a dependency judgment logic error (#9223)

* fix: approve builds manifest pnpm ignoredBuiltDependencies condition error

* chore: changeset and test

* refactor: tests and changeset

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
This commit is contained in:
btea
2025-03-07 08:52:50 +08:00
committed by GitHub
parent c3aa4d8dd6
commit 1e6ae3ea6e
3 changed files with 85 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/exec.build-commands": patch
"pnpm": patch
---
When executing the `approve-builds` command, if package.json contains `onlyBuiltDependencies` or `ignoredBuiltDependencies`, the selected dependency package will continue to be written into `package.json`.

View File

@@ -123,7 +123,7 @@ Do you approve?`,
}
let { manifest, writeProjectManifest } = await tryReadProjectManifest(opts.rootProjectManifestDir)
manifest ??= {}
if (opts.workspaceDir == null || manifest.pnpm?.onlyBuiltDependencies != null || manifest.pnpm?.onlyBuiltDependencies != null) {
if (opts.workspaceDir == null || manifest.pnpm?.onlyBuiltDependencies != null || manifest.pnpm?.ignoredBuiltDependencies != null) {
manifest.pnpm ??= {}
if (updatedOnlyBuiltDependencies) {
manifest.pnpm.onlyBuiltDependencies = updatedOnlyBuiltDependencies

View File

@@ -90,3 +90,81 @@ test("works when root project manifest doesn't exist in a workspace", async () =
ignoredBuiltDependencies: ['@pnpm.e2e/install-script-example'],
})
})
test('should update onlyBuiltDependencies when package.json exists with ignoredBuiltDependencies defined', async () => {
const temp = tempDir()
prepare({
dependencies: {
'@pnpm.e2e/pre-and-postinstall-scripts-example': '1.0.0',
'@pnpm.e2e/install-script-example': '*',
},
pnpm: {
ignoredBuiltDependencies: ['@pnpm.e2e/install-script-example'],
},
}, {
tempDir: temp,
})
const workspaceManifestFile = path.join(temp, 'pnpm-workspace.yaml')
writeYamlFile(workspaceManifestFile, { packages: ['packages/*'] })
await runApproveBuilds({ workspaceDir: temp, rootProjectManifestDir: temp })
expect(readYamlFile(workspaceManifestFile)).toStrictEqual({
packages: ['packages/*'],
})
expect(loadJsonFile<ProjectManifest>(path.join(temp, 'package.json'))!.pnpm).toStrictEqual({
ignoredBuiltDependencies: ['@pnpm.e2e/install-script-example'],
onlyBuiltDependencies: ['@pnpm.e2e/pre-and-postinstall-scripts-example'],
})
})
test('should approve builds when package.json exists with onlyBuiltDependencies defined', async () => {
const temp = tempDir()
prepare({
dependencies: {
'@pnpm.e2e/pre-and-postinstall-scripts-example': '1.0.0',
'@pnpm.e2e/install-script-example': '*',
},
pnpm: {
onlyBuiltDependencies: ['@pnpm.e2e/install-script-example'],
},
}, {
tempDir: temp,
})
const workspaceManifestFile = path.join(temp, 'pnpm-workspace.yaml')
writeYamlFile(workspaceManifestFile, { packages: ['packages/*'] })
await runApproveBuilds({ workspaceDir: temp, rootProjectManifestDir: temp })
expect(readYamlFile(workspaceManifestFile)).toStrictEqual({
packages: ['packages/*'],
})
expect(loadJsonFile<ProjectManifest>(path.join(temp, 'package.json'))!.pnpm).toStrictEqual({
onlyBuiltDependencies: ['@pnpm.e2e/install-script-example', '@pnpm.e2e/pre-and-postinstall-scripts-example'],
})
})
test('should approve builds with package.json that has no onlyBuiltDependencies and ignoredBuiltDependencies fields defined', async () => {
const temp = tempDir()
prepare({
dependencies: {
'@pnpm.e2e/pre-and-postinstall-scripts-example': '1.0.0',
'@pnpm.e2e/install-script-example': '*',
},
}, {
tempDir: temp,
})
const workspaceManifestFile = path.join(temp, 'pnpm-workspace.yaml')
writeYamlFile(workspaceManifestFile, { packages: ['packages/*'] })
await runApproveBuilds({ workspaceDir: temp, rootProjectManifestDir: temp })
expect(readYamlFile(workspaceManifestFile)).toStrictEqual({
packages: ['packages/*'],
onlyBuiltDependencies: ['@pnpm.e2e/pre-and-postinstall-scripts-example'],
ignoredBuiltDependencies: ['@pnpm.e2e/install-script-example'],
})
})