Skip to content

Commit

Permalink
fix: prefix in .npmrc error log (#6685)
Browse files Browse the repository at this point in the history
Co-authored-by: AaronHamilton965 <[email protected]>
  • Loading branch information
rahulio96 and AaronHamilton965 authored Jul 31, 2023
1 parent c1e01d9 commit ed9a461
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
10 changes: 9 additions & 1 deletion workspaces/config/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,15 @@ class Config {
process.emit('time', 'config:load:file:' + file)
// only catch the error from readFile, not from the loadObject call
await readFile(file, 'utf8').then(
data => this.#loadObject(ini.parse(data), type, file),
data => {
const parsedConfig = ini.parse(data)
if (type === 'project' && parsedConfig.prefix) {
// Log error if prefix is mentioned in project .npmrc
/* eslint-disable-next-line max-len */
log.error('config', `prefix cannot be changed from project config: ${file}.`)
}
return this.#loadObject(parsedConfig, type, file)
},
er => this.#loadObject(null, type, file, er)
)
process.emit('timeEnd', 'config:load:file:' + file)
Expand Down
31 changes: 31 additions & 0 deletions workspaces/config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1447,3 +1447,34 @@ t.test('umask', async t => {
t.equal(umask, 0)
})
})

t.test('catch project config prefix error', async t => {
const path = t.testdir()
t.testdir({
project: {
node_modules: {},
'.npmrc': `
project-config = true
foo = from-project-config
prefix=./lib
`,
},
})
const config = new Config({
npmPath: `${path}/npm`,
argv: [process.execPath, __filename, '--projectconfig', `${path}/project/.npmrc`],
cwd: join(`${path}/project`),
shorthands,
definitions,
})
const logs = []
const logHandler = (...args) => logs.push(args)
process.on('log', logHandler)
t.teardown(() => process.off('log', logHandler))
logs.length = 0
// config.load() triggers the error to be logged
await config.load()
t.match(logs, [[
'error', 'config', `prefix cannot be changed from project config: ${path}`,
]], 'Expected error logged')
})

0 comments on commit ed9a461

Please sign in to comment.