diff --git a/.changeset/allow-builds-pnpmfile-fix.md b/.changeset/allow-builds-pnpmfile-fix.md new file mode 100644 index 0000000000..f166e27edb --- /dev/null +++ b/.changeset/allow-builds-pnpmfile-fix.md @@ -0,0 +1,6 @@ +--- +"@pnpm/cli-utils": patch +"pnpm": patch +--- + +Fixed `allowBuilds` not working when set via `.pnpmfile.cjs` [#10516](https://github.com/pnpm/pnpm/issues/10516). diff --git a/cli/cli-utils/src/getConfig.ts b/cli/cli-utils/src/getConfig.ts index 562c580795..e2d0d661a7 100644 --- a/cli/cli-utils/src/getConfig.ts +++ b/cli/cli-utils/src/getConfig.ts @@ -116,4 +116,24 @@ function applyDerivedConfig (config: Config): void { delete config.hoistPattern delete config.publicHoistPattern } + if (config.allowBuilds) { + config.onlyBuiltDependencies ??= [] + config.ignoredBuiltDependencies ??= [] + const onlyBuiltSet = new Set(config.onlyBuiltDependencies) + const ignoredBuiltSet = new Set(config.ignoredBuiltDependencies) + for (const [packagePattern, build] of Object.entries(config.allowBuilds)) { + switch (build) { + case true: + if (!onlyBuiltSet.has(packagePattern)) { + config.onlyBuiltDependencies.push(packagePattern) + } + break + case false: + if (!ignoredBuiltSet.has(packagePattern)) { + config.ignoredBuiltDependencies.push(packagePattern) + } + break + } + } + } } diff --git a/cli/cli-utils/test/getConfig.test.ts b/cli/cli-utils/test/getConfig.test.ts index 245f46cdb1..148f8de1bc 100644 --- a/cli/cli-utils/test/getConfig.test.ts +++ b/cli/cli-utils/test/getConfig.test.ts @@ -42,3 +42,43 @@ test('hoist: false removes hoistPattern', async () => { expect(config.hoist).toBe(false) expect(config.hoistPattern).toBeUndefined() }) + +test('allowBuilds populates onlyBuiltDependencies and ignoredBuiltDependencies', async () => { + prepare() + + const config = await getConfig({ + allowBuilds: { + 'allowed-pkg': true, + 'another-allowed': true, + 'blocked-pkg': false, + }, + }, { + workspaceDir: '.', + excludeReporter: false, + rcOptionsTypes: {}, + }) + + expect(config.onlyBuiltDependencies).toContain('allowed-pkg') + expect(config.onlyBuiltDependencies).toContain('another-allowed') + expect(config.ignoredBuiltDependencies).toContain('blocked-pkg') +}) + +test('allowBuilds does not add duplicates', async () => { + prepare() + + const config = await getConfig({ + onlyBuiltDependencies: ['already-allowed'], + ignoredBuiltDependencies: ['already-blocked'], + allowBuilds: { + 'already-allowed': true, + 'already-blocked': false, + }, + }, { + workspaceDir: '.', + excludeReporter: false, + rcOptionsTypes: {}, + }) + + expect(config.onlyBuiltDependencies).toEqual(['already-allowed']) + expect(config.ignoredBuiltDependencies).toEqual(['already-blocked']) +})