Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prefix in .npmrc error log #6685

Merged
merged 6 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
})
Loading