Skip to content

Commit 71e46de

Browse files
committed
fix: preserve user-defined npm_config_* env vars in lifecycle scripts
1 parent 8dfe438 commit 71e46de

6 files changed

Lines changed: 52 additions & 6 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@pnpm/exec.lifecycle": patch
3+
"pnpm": patch
4+
---
5+
6+
User-defined `npm_config_*` environment variables are now preserved during lifecycle script execution. Previously, all `npm_`-prefixed env vars were stripped, which caused user-set variables like `npm_config_platform_arch` to be lost [pnpm/pnpm#12399](https://github.com/pnpm/pnpm/issues/12399).
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "inspect-user-npm-config",
3+
"version": "1.0.0",
4+
"scripts": {
5+
"postinstall": "node postinstall.js | test-ipc-server-client ./test.sock"
6+
}
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const value = process.env['npm_config_platform_arch']
2+
3+
switch(value) {
4+
case undefined:
5+
process.stdout.write('unset')
6+
break
7+
case '':
8+
process.stdout.write('empty string')
9+
break
10+
default:
11+
process.stdout.write(value)
12+
break
13+
}

exec/lifecycle/test/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@ test('runLifecycleHook() does not set npm_config env vars', async () => {
7676
expect(server.getLines()).toStrictEqual(['unset'])
7777
})
7878

79+
test('runLifecycleHook() preserves user-defined npm_config_* env vars', async () => {
80+
const pkgRoot = f.find('inspect-user-npm-config')
81+
await using server = await createTestIpcServer(path.join(pkgRoot, 'test.sock'))
82+
const { default: pkg } = await import(path.join(pkgRoot, 'package.json'))
83+
process.env.npm_config_platform_arch = 'x64'
84+
try {
85+
await runLifecycleHook('postinstall', pkg, {
86+
depPath: '/inspect-user-npm-config/1.0.0',
87+
pkgRoot,
88+
rootModulesDir,
89+
unsafePerm: true,
90+
})
91+
} finally {
92+
delete process.env.npm_config_platform_arch
93+
}
94+
95+
expect(server.getLines()).toStrictEqual(['x64'])
96+
})
97+
7998
test('runPostinstallHooks()', async () => {
8099
const pkgRoot = f.find('with-many-scripts')
81100
await using server = await createTestIpcServer(path.join(pkgRoot, 'test.sock'))

pnpm-lock.yaml

Lines changed: 6 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pnpm-workspace.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ catalog:
9191
'@pnpm/logger': '^1100.0.0'
9292
'@pnpm/meta-updater': 2.0.6
9393
'@pnpm/nopt': ^0.3.1
94-
'@pnpm/npm-lifecycle': 1100.0.0-1
94+
'@pnpm/npm-lifecycle': https://github.com/pnpm/npm-lifecycle#refs/pull/59/head
9595
'@pnpm/npm-package-arg': ^2.0.0
9696
'@pnpm/os.env.path-extender': ^3.0.1
9797
'@pnpm/patch-package': 0.0.1

0 commit comments

Comments
 (0)