diff --git a/workspaces/config/lib/index.js b/workspaces/config/lib/index.js index fda049fe08535..e46fe3d2aa2f3 100644 --- a/workspaces/config/lib/index.js +++ b/workspaces/config/lib/index.js @@ -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) diff --git a/workspaces/config/test/index.js b/workspaces/config/test/index.js index 520d554436ea8..9e9fac4f6dc34 100644 --- a/workspaces/config/test/index.js +++ b/workspaces/config/test/index.js @@ -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') +})