From dceef19b08b51f7f9ddb53f9649cc76c153d21f0 Mon Sep 17 00:00:00 2001 From: Gar Date: Thu, 6 Mar 2025 07:15:16 -0800 Subject: [PATCH] fix: re-add positional arg and abbrev warnings pr #8071 originally had this but that appears to have gotten lost along the way. --- workspaces/config/lib/index.js | 13 ++++++++++ workspaces/config/test/index.js | 45 +++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/workspaces/config/lib/index.js b/workspaces/config/lib/index.js index cba2ebd3621c5..30ebf75b9b07e 100644 --- a/workspaces/config/lib/index.js +++ b/workspaces/config/lib/index.js @@ -364,8 +364,11 @@ class Config { } nopt.invalidHandler = (k, val, type) => this.invalidHandler(k, val, type, 'command line options', 'cli') + nopt.unknownHandler = this.unknownHandler + nopt.abbrevHandler = this.abbrevHandler const conf = nopt(this.types, this.shorthands, this.argv) nopt.invalidHandler = null + nopt.unknownHandler = null this.parsedArgv = conf.argv delete conf.argv this.#loadObject(conf, 'cli', 'command line options') @@ -531,6 +534,16 @@ class Config { log.warn('invalid config', msg, desc) } + abbrevHandler (short, long) { + log.warn(`Expanding --${short} to --${long}. This will stop working in the next major version of npm.`) + } + + unknownHandler (key, next) { + if (next) { + log.warn(`"${next}" is being parsed as a normal command line argument.`) + } + } + #getOneOfKeywords (mustBe, typeDesc) { let keyword if (mustBe.length === 1 && typeDesc.includes(Array)) { diff --git a/workspaces/config/test/index.js b/workspaces/config/test/index.js index 9e93b593731c7..a4dca9ec58890 100644 --- a/workspaces/config/test/index.js +++ b/workspaces/config/test/index.js @@ -1542,3 +1542,48 @@ t.test('invalid single hyphen warnings', async t => { ['warn', '-ws is not a valid single-hyphen cli flag and will be removed in the future'], ], 'Warns about single hyphen configs') }) + +t.test('positional arg warnings', async t => { + const path = t.testdir() + const logs = [] + const logHandler = (...args) => logs.push(args) + process.on('log', logHandler) + t.teardown(() => process.off('log', logHandler)) + const config = new Config({ + npmPath: `${path}/npm`, + env: {}, + argv: [process.execPath, __filename, '--something', 'extra'], + cwd: path, + shorthands, + definitions, + nerfDarts, + }) + await config.load() + const filtered = logs.filter(l => l[0] === 'warn') + t.match(filtered, [ + ['warn', '"extra" is being parsed as a normal command line argument.'], + ['warn', 'Unknown cli config "--something". This will stop working in the next major version of npm.'], + ], 'Warns about positional cli arg') +}) + +t.test('abbreviation expansion warnings', async t => { + const path = t.testdir() + const logs = [] + const logHandler = (...args) => logs.push(args) + process.on('log', logHandler) + t.teardown(() => process.off('log', logHandler)) + const config = new Config({ + npmPath: `${path}/npm`, + env: {}, + argv: [process.execPath, __filename, '--bef', '2020-01-01'], + cwd: path, + shorthands, + definitions, + nerfDarts, + }) + await config.load() + const filtered = logs.filter(l => l[0] === 'warn') + t.match(filtered, [ + ['warn', 'Expanding --bef to --before. This will stop working in the next major version of npm'], + ], 'Warns about expanded abbreviations') +})