Skip to content

Commit ed9a461

Browse files
fix: prefix in .npmrc error log (#6685)
Co-authored-by: AaronHamilton965 <[email protected]>
1 parent c1e01d9 commit ed9a461

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

workspaces/config/lib/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,15 @@ class Config {
610610
process.emit('time', 'config:load:file:' + file)
611611
// only catch the error from readFile, not from the loadObject call
612612
await readFile(file, 'utf8').then(
613-
data => this.#loadObject(ini.parse(data), type, file),
613+
data => {
614+
const parsedConfig = ini.parse(data)
615+
if (type === 'project' && parsedConfig.prefix) {
616+
// Log error if prefix is mentioned in project .npmrc
617+
/* eslint-disable-next-line max-len */
618+
log.error('config', `prefix cannot be changed from project config: ${file}.`)
619+
}
620+
return this.#loadObject(parsedConfig, type, file)
621+
},
614622
er => this.#loadObject(null, type, file, er)
615623
)
616624
process.emit('timeEnd', 'config:load:file:' + file)

workspaces/config/test/index.js

+31
Original file line numberDiff line numberDiff line change
@@ -1447,3 +1447,34 @@ t.test('umask', async t => {
14471447
t.equal(umask, 0)
14481448
})
14491449
})
1450+
1451+
t.test('catch project config prefix error', async t => {
1452+
const path = t.testdir()
1453+
t.testdir({
1454+
project: {
1455+
node_modules: {},
1456+
'.npmrc': `
1457+
project-config = true
1458+
foo = from-project-config
1459+
prefix=./lib
1460+
`,
1461+
},
1462+
})
1463+
const config = new Config({
1464+
npmPath: `${path}/npm`,
1465+
argv: [process.execPath, __filename, '--projectconfig', `${path}/project/.npmrc`],
1466+
cwd: join(`${path}/project`),
1467+
shorthands,
1468+
definitions,
1469+
})
1470+
const logs = []
1471+
const logHandler = (...args) => logs.push(args)
1472+
process.on('log', logHandler)
1473+
t.teardown(() => process.off('log', logHandler))
1474+
logs.length = 0
1475+
// config.load() triggers the error to be logged
1476+
await config.load()
1477+
t.match(logs, [[
1478+
'error', 'config', `prefix cannot be changed from project config: ${path}`,
1479+
]], 'Expected error logged')
1480+
})

0 commit comments

Comments
 (0)