Skip to content

fix: preserve user-defined npm_config_* env vars in lifecycle scripts#59

Merged
zkochan merged 3 commits into
pnpm:mainfrom
felipecrs:preserve-user-defined-npm-config-vars
Jun 14, 2026
Merged

fix: preserve user-defined npm_config_* env vars in lifecycle scripts#59
zkochan merged 3 commits into
pnpm:mainfrom
felipecrs:preserve-user-defined-npm-config-vars

Conversation

@felipecrs

@felipecrs felipecrs commented Jun 14, 2026

Copy link
Copy Markdown

makeEnv() strips all npm_* env vars from process.env when building the child environment.

This was fine before #55, which re-populated npm_config_* from opts.config. After #55 removed that, user-defined vars like npm_config_platform_arch are stripped and never restored.

Fix: narrow the filter from /^npm_/ to /^npm_package_/, since those are the only ones makeEnv regenerates.

Refs pnpm/pnpm#12399

Summary by CodeRabbit

  • Bug Fixes
    • Improved lifecycle environment variable filtering: when no explicit environment is provided, the app now preserves user-defined npm_config_* entries (including platform architecture), while continuing to exclude only the appropriate npm_package_* variables.
    • Added additional stripping of private authentication/config-related npm_config_* keys (such as internal auth and registry/scope auth variants) so they aren’t propagated into the generated lifecycle environment.

@coderabbitai

coderabbitai Bot commented Jun 14, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@zkochan, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 52 minutes and 32 seconds. Learn how PR review limits work.

Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file).

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 44c5f578-b13b-482b-831f-a4f8536bc88f

📥 Commits

Reviewing files that changed from the base of the PR and between 7e61c94 and d6be556.

📒 Files selected for processing (2)
  • index.js
  • test/index.js
📝 Walkthrough

Walkthrough

In makeEnv, the regex used to exclude inherited process.env variables is narrowed from ^npm_ to ^npm_package_, allowing npm_config_* variables to propagate into the lifecycle environment. Additionally, a filter is added to exclude "private" npm_config_* auth and config keys matching the pattern ^npm_config_([/@_]|.*:_ ). The unit test is updated to inject both user-defined npm_config_platform_arch and auth-related npm_config_* variants, assert that the user-defined value is preserved while auth keys are filtered out, and clean up via try/finally.

Changes

makeEnv env-var filter narrowing and auth key filtering

Layer / File(s) Summary
Narrow npm_ exclusion and add auth-key filtering with verification
index.js, test/index.js
makeEnv now excludes only ^npm_package_ variables instead of all ^npm_* variables when copying process.env entries, and adds a filter to strip private npm_config_* auth/config keys matching `^npm_config_([/@_]

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 A filter refined, auth secrets now gone,
While user configs carry bravely on!
The package vars still screened with care,
Private npm_config_ keys vanish in air.
One regex tightened, one test to declare —
The rabbit guards env with precision and flair! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: preserving user-defined npm_config_* environment variables in lifecycle scripts, which directly addresses the core issue detailed in the PR objectives.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

PR Summary by Qodo

Fix makeEnv filtering to preserve user-defined npm_config_* vars
🐞 Bug fix 🧪 Tests 🕐 10-20 Minutes

Grey Divider

Walkthroughs

Description
• Preserve user-defined npm_config_* variables when building lifecycle child environments.
• Narrow env-var stripping to only remove regenerated npm_package_* keys.
• Add regression test ensuring custom npm_config_* survives makeEnv().
Diagram
graph TD
  A[("process.env")] --> B["makeEnv()"] --> C["Strip npm_package_*"] --> D["Child env"] --> E["Lifecycle scripts"]
Loading
High-Level Assessment

The following are alternative approaches to this PR:

1. Allowlist specific npm_config_* variables
  • ➕ Most conservative: preserves only known-safe npm_config_* keys
  • ➖ Easy to miss legitimate user-defined keys; requires ongoing maintenance
2. Re-introduce regenerating npm_config_* from opts.config
  • ➕ Restores prior behavior where config is explicitly injected
  • ➖ May unintentionally override user-defined environment values; broader semantic change than needed

Recommendation: Keep the PR’s approach: makeEnv only needs to strip variables it regenerates (npm_package_). This minimizes behavior change, avoids resurrecting the removed opts.config→npm_config_ injection, and fixes the regression where user-defined npm_config_* values were silently dropped.

Grey Divider

File Changes

Bug fix (1)
index.js Only strip npm_package_* from process.env when building child env +1/-1

Only strip npm_package_* from process.env when building child env

• Updates makeEnv() to stop removing all npm_* variables from the inherited environment. The filter now removes only npm_package_* keys, which are the ones makeEnv regenerates from package data, preserving user-provided npm_config_* variables.

index.js


Tests (1)
index.js Regression test for preserving user-defined npm_config_* vars +17/-11

Regression test for preserving user-defined npm_config_* vars

• Extends the makeEnv test to set process.env.npm_config_platform_arch and assert it remains present in the generated env. Uses a try/finally cleanup to avoid leaking environment state across tests.

test/index.js


Grey Divider

Qodo Logo

@felipecrs felipecrs force-pushed the preserve-user-defined-npm-config-vars branch from 2e48645 to 0c3e9c8 Compare June 14, 2026 04:40
@felipecrs felipecrs force-pushed the preserve-user-defined-npm-config-vars branch from 61fb252 to 4d3b8b0 Compare June 14, 2026 05:23
@zkochan

zkochan commented Jun 14, 2026

Copy link
Copy Markdown
Member

I am unsure this is safe. Auth related env vars should still be stripped I believe.

zkochan added 2 commits June 14, 2026 13:06
Preserving all non-npm_package_* vars from process.env would leak
auth-related npm_config_* values (e.g. _auth, _authToken, _password,
//registry/:_authToken) into every dependency's lifecycle script.

Filter these out, mirroring npm's own env-export rule where config keys
starting with _, /, or @ (or containing :_) are treated as private.
Apply the same private-key filter to pnpm_config_* as npm_config_*, since
pnpm exports config under both prefixes. Also guard the scoped registry
auth token form (//registry/:@scope:_authToken) with a test.
@zkochan zkochan merged commit 4a14881 into pnpm:main Jun 14, 2026
5 checks passed
@felipecrs felipecrs deleted the preserve-user-defined-npm-config-vars branch June 14, 2026 14:56
@felipecrs

Copy link
Copy Markdown
Author

I am unsure this is safe. Auth related env vars should still be stripped I believe.

Interesting. Thanks a lot for patching and merging this PR, @zkochan!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants