Skip to content

Commit db2408d

Browse files
lifecycle_hooks_envs prefixed with npm_config_ should get passed to gyp/lifecycle hook (#1294)
* Aliased platforms. * Pass npm_config_ prefixed env_properties to lifecycle hook. We need to explicitly pass `npm_config_` prefixed env-variables as configuration to the lifecycle hook (or gyp). 1. rules_js allow to provide per action_type environment using `lifecycle_hooks_envs`. 2. One of the important use-cases is to able provide mirror where prebuild binaries are stored: (see: https://github.com/mapbox/node-pre-gyp#download-binary-files-from-a-mirror by {module_name}_binary_host_mirror) 3. Such flags are taken (by gyp) from environment variables: https://github.com/mapbox/node-pre-gyp/blob/a74f5e367c0d71033620aa0112e7baf7f3515b9d/lib/util/versioning.js#L316 4. Unfortunetely pnpm/lifecycle drops all npm_ prefixed env-variables prior to calling lifecycle hook: https://github.com/pnpm/npm-lifecycle/blob/99ac0429025bdf1303879723d3fbd57c585ae8a1/index.js#L351 and later recreates it based on explicitly given config: https://github.com/pnpm/npm-lifecycle/blob/99ac0429025bdf1303879723d3fbd57c585ae8a1/index.js#L408 5. So we need to perform reversed process: generate rawConfig based on env-variables to preserve them.
1 parent 7762b73 commit db2408d

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

npm/private/lifecycle/lifecycle-hooks.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ async function main(args) {
165165

166166
// Create .bin entry point files for all packages in node_modules
167167
await makeBins(nodeModulesPath, '', segmentsUp)
168-
169168
// export interface RunLifecycleHookOptions {
170169
// args?: string[];
171170
// depPath: string;
@@ -183,6 +182,29 @@ async function main(args) {
183182
// stdio?: string;
184183
// unsafePerm: boolean;
185184
// }
185+
186+
// We need to explicitly pass `npm_config_` prefixed env-variables as configuration to the lifecycle hook (or gyp).
187+
// 1. rules_js allow to provide per action_type environment using `lifecycle_hooks_envs`.
188+
// 2. One of the important use-cases is to able provide mirror where prebuild binaries are stored:
189+
// (see: https://github.com/mapbox/node-pre-gyp#download-binary-files-from-a-mirror
190+
// by {module_name}_binary_host_mirror)
191+
// 3. Such flags are taken (by gyp) from environment variables:
192+
// https://github.com/mapbox/node-pre-gyp/blob/a74f5e367c0d71033620aa0112e7baf7f3515b9d/lib/util/versioning.js#L316
193+
// 4. Unfortunetely pnpm/lifecycle drops all npm_ prefixed env-variables prior to calling lifecycle hook:
194+
// https://github.com/pnpm/npm-lifecycle/blob/99ac0429025bdf1303879723d3fbd57c585ae8a1/index.js#L351
195+
// and later recreates it based on explicitly given config:
196+
// https://github.com/pnpm/npm-lifecycle/blob/99ac0429025bdf1303879723d3fbd57c585ae8a1/index.js#L408
197+
// 5. So we need to perform reversed process: generate rawConfig based on env-variables to preserve them.
198+
let inherited_env = {}
199+
const npm_config_prefix = 'npm_config_'
200+
const config_regexp = new RegExp('^' + npm_config_prefix, 'i')
201+
for (let e in process.env) {
202+
if (e.match(config_regexp)) {
203+
inherited_env[e.substring(npm_config_prefix.length)] =
204+
process.env[e]
205+
}
206+
}
207+
186208
const opts = {
187209
pkgRoot: path.resolve(outputDir),
188210

@@ -196,11 +218,15 @@ async function main(args) {
196218
// https://yarnpkg.com/package?name=prebuild-install:
197219
// "... you can set environment variables npm_config_build_from_source=true, npm_config_platform,
198220
// npm_config_arch, npm_config_target npm_config_runtime and npm_config_libc".
199-
rawConfig: {
200-
stdio: 'inherit',
201-
platform: platform,
202-
arch: arch,
203-
},
221+
rawConfig: Object.assign(
222+
{},
223+
{
224+
stdio: 'inherit',
225+
platform: platform,
226+
arch: arch,
227+
},
228+
inherited_env
229+
),
204230
silent: false,
205231
stdio: 'inherit',
206232
rootModulesDir: nodeModulesPath,

npm/private/lifecycle/min/index.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)