Skip to content

fix: re-add positional arg and abbrev warnings #8145

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

Merged
merged 1 commit into from
Mar 7, 2025
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
13 changes: 13 additions & 0 deletions workspaces/config/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)) {
Expand Down
45 changes: 45 additions & 0 deletions workspaces/config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})