Skip to content

Commit

Permalink
fix: don't crash on a .npmrc with missing env var (#5127)
Browse files Browse the repository at this point in the history
close #5093
  • Loading branch information
zkochan committed Jul 31, 2022
1 parent c907984 commit 34121d7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/three-mugs-hide.md
@@ -0,0 +1,6 @@
---
"@pnpm/config": patch
"pnpm": patch
---

Don't crash when a config file contains a setting with an env variable that doesn't exist [#5093](https://github.com/pnpm/pnpm/issues/5093).
2 changes: 1 addition & 1 deletion packages/config/package.json
Expand Up @@ -36,7 +36,7 @@
"@pnpm/error": "workspace:*",
"@pnpm/git-utils": "workspace:*",
"@pnpm/matcher": "workspace:*",
"@pnpm/npm-conf": "1.0.5",
"@pnpm/npm-conf": "2.0.0",
"@pnpm/pnpmfile": "workspace:*",
"@pnpm/read-project-manifest": "workspace:*",
"@pnpm/types": "workspace:*",
Expand Down
3 changes: 1 addition & 2 deletions packages/config/src/index.ts
Expand Up @@ -144,7 +144,6 @@ export default async (
const env = opts.env ?? process.env
const packageManager = opts.packageManager ?? { name: 'pnpm', version: 'undefined' }
const cliOptions = opts.cliOptions ?? {}
const warnings = new Array<string>()

if (cliOptions['hoist'] === false) {
if (cliOptions['shamefully-hoist'] === true) {
Expand Down Expand Up @@ -176,7 +175,7 @@ export default async (
cliOptions['prefix'] = cliOptions.dir // the npm config system still expects `prefix`
}
const rcOptionsTypes = { ...types, ...opts.rcOptionsTypes }
const npmConfig = loadNpmConf(cliOptions, rcOptionsTypes, {
const { config: npmConfig, warnings } = loadNpmConf(cliOptions, rcOptionsTypes, {
'auto-install-peers': false,
bail: true,
color: 'auto',
Expand Down
13 changes: 13 additions & 0 deletions packages/config/test/index.ts
Expand Up @@ -912,3 +912,16 @@ test('preferSymlinkedExecutables should be true when nodeLinker is hoisted', asy
})
expect(config.preferSymlinkedExecutables).toBeTruthy()
})

test('return a warning when the .npmrc has an env variable that does not exist', async () => {
await fs.writeFile('.npmrc', 'foo=${ENV_VAR_123}', 'utf8') // eslint-disable-line
const { warnings } = await getConfig({
cliOptions: {},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})

expect(warnings[0]).toContain('Failed to replace env in config: ${ENV_VAR_123}') // eslint-disable-line
})
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 34121d7

Please sign in to comment.