From 49555e4d8ba2279ed7d2f33c60f2311fd7bb1135 Mon Sep 17 00:00:00 2001 From: AaronHamilton965 <91709196+AaronHamilton965@users.noreply.github.com> Date: Fri, 28 Jul 2023 22:22:28 +0000 Subject: [PATCH 1/6] added tap test that checks if error is logged --- workspaces/config/test/index.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/workspaces/config/test/index.js b/workspaces/config/test/index.js index 520d554436ea8..7ee5fd4b1ccab 100644 --- a/workspaces/config/test/index.js +++ b/workspaces/config/test/index.js @@ -1447,3 +1447,35 @@ 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', `prefix=./lib cannot be changed from project config: ${path}/project/.npmrc` + ]], 'Expected error logged') +}) From ac66b3497327ed59862db8d2fd637d1eba85ece0 Mon Sep 17 00:00:00 2001 From: rahulio96 Date: Fri, 28 Jul 2023 15:27:18 -0700 Subject: [PATCH 2/6] Log error if prefix mentioned in project config --- workspaces/config/lib/index.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/workspaces/config/lib/index.js b/workspaces/config/lib/index.js index fda049fe08535..7075deb5cb871 100644 --- a/workspaces/config/lib/index.js +++ b/workspaces/config/lib/index.js @@ -610,7 +610,17 @@ 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') { + const parsedConfigPrefix = parsedConfig.prefix + // Log error if prefix is mentioned in project .npmrc + if (parsedConfigPrefix) { + log.error(`prefix=${parsedConfigPrefix} cannot be changed from project config: ${file}`) + } + } + this.#loadObject(parsedConfig, type, file) + }, er => this.#loadObject(null, type, file, er) ) process.emit('timeEnd', 'config:load:file:' + file) From 8d91f11c98afc24995ea5a9aa759b0dfb5cf014a Mon Sep 17 00:00:00 2001 From: rahulio96 Date: Fri, 28 Jul 2023 18:29:38 -0700 Subject: [PATCH 3/6] Added return to #loadFile and fixed formatting for eslint --- workspaces/config/lib/index.js | 2 +- workspaces/config/test/index.js | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/workspaces/config/lib/index.js b/workspaces/config/lib/index.js index 7075deb5cb871..a754986d11409 100644 --- a/workspaces/config/lib/index.js +++ b/workspaces/config/lib/index.js @@ -619,7 +619,7 @@ class Config { log.error(`prefix=${parsedConfigPrefix} cannot be changed from project config: ${file}`) } } - this.#loadObject(parsedConfig, type, file) + return this.#loadObject(parsedConfig, type, file) }, er => this.#loadObject(null, type, file, er) ) diff --git a/workspaces/config/test/index.js b/workspaces/config/test/index.js index 7ee5fd4b1ccab..1a5fea2ebb635 100644 --- a/workspaces/config/test/index.js +++ b/workspaces/config/test/index.js @@ -1448,7 +1448,7 @@ t.test('umask', async t => { }) }) -t.test('catch project config prefix error', async t=> { +t.test('catch project config prefix error', async t => { const path = t.testdir() t.testdir({ project: { @@ -1470,12 +1470,11 @@ t.test('catch project config prefix error', async t=> { const logs = [] const logHandler = (...args) => logs.push(args) process.on('log', logHandler) - t.teardown(() => process.off('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', `prefix=./lib cannot be changed from project config: ${path}/project/.npmrc` + 'error', `prefix=./lib cannot be changed from project config: ${path}/project/.npmrc`, ]], 'Expected error logged') }) From b4afdb82a4b314ad4130cf8ad609f848ff9f7a5f Mon Sep 17 00:00:00 2001 From: rahulio96 Date: Sat, 29 Jul 2023 23:11:06 -0700 Subject: [PATCH 4/6] Potential windows tap test fix --- workspaces/config/test/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/workspaces/config/test/index.js b/workspaces/config/test/index.js index 1a5fea2ebb635..32cc3a33eb248 100644 --- a/workspaces/config/test/index.js +++ b/workspaces/config/test/index.js @@ -1475,6 +1475,6 @@ t.test('catch project config prefix error', async t => { // config.load() triggers the error to be logged await config.load() t.match(logs, [[ - 'error', `prefix=./lib cannot be changed from project config: ${path}/project/.npmrc`, + 'error', `prefix=./lib cannot be changed from project config: ${path}`, ]], 'Expected error logged') }) From 1975b3e81e5ea6a45e35c1d4c32bad4553e204fb Mon Sep 17 00:00:00 2001 From: rahulio96 Date: Mon, 31 Jul 2023 12:18:45 -0700 Subject: [PATCH 5/6] Changed error message and removed parsedConfigPrefix --- workspaces/config/lib/index.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/workspaces/config/lib/index.js b/workspaces/config/lib/index.js index a754986d11409..e46fe3d2aa2f3 100644 --- a/workspaces/config/lib/index.js +++ b/workspaces/config/lib/index.js @@ -612,12 +612,10 @@ class Config { await readFile(file, 'utf8').then( data => { const parsedConfig = ini.parse(data) - if (type === 'project') { - const parsedConfigPrefix = parsedConfig.prefix + if (type === 'project' && parsedConfig.prefix) { // Log error if prefix is mentioned in project .npmrc - if (parsedConfigPrefix) { - log.error(`prefix=${parsedConfigPrefix} cannot be changed from project config: ${file}`) - } + /* eslint-disable-next-line max-len */ + log.error('config', `prefix cannot be changed from project config: ${file}.`) } return this.#loadObject(parsedConfig, type, file) }, From 8f545f6715b0e546a959b6f6ddaa1749be6329cd Mon Sep 17 00:00:00 2001 From: rahulio96 Date: Mon, 31 Jul 2023 12:29:18 -0700 Subject: [PATCH 6/6] Added config prefix to tap test --- workspaces/config/test/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/workspaces/config/test/index.js b/workspaces/config/test/index.js index 32cc3a33eb248..9e9fac4f6dc34 100644 --- a/workspaces/config/test/index.js +++ b/workspaces/config/test/index.js @@ -1475,6 +1475,6 @@ t.test('catch project config prefix error', async t => { // config.load() triggers the error to be logged await config.load() t.match(logs, [[ - 'error', `prefix=./lib cannot be changed from project config: ${path}`, + 'error', 'config', `prefix cannot be changed from project config: ${path}`, ]], 'Expected error logged') })