Skip to content

Commit

Permalink
fix: warn on invalid single-hyphen cli flags (#8076)
Browse files Browse the repository at this point in the history
Single hyphen cli flags traditionally are single-character only, so they
can be combined. npm already supports combining single-hyphen flags
together, so it eventually needs stop supporting multi-character ones.

Also re-added -ws to undocumented shorthands, it was accidentally
removed from the main config and not re-added to the internal one.

Finally, warnings on a few env configs that npm tosses around were
suppressed for now.
  • Loading branch information
wraithgar authored Jan 30, 2025
1 parent ed85b01 commit 593c849
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions tap-snapshots/test/lib/docs.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2547,6 +2547,7 @@ exports[`test/lib/docs.js TAP shorthands > docs 1`] = `
* \`--help\`: \`--usage\`
* \`-v\`: \`--version\`
* \`-w\`: \`--workspace\`
* \`--ws\`: \`--workspaces\`
* \`-y\`: \`--yes\`
`

Expand Down
1 change: 1 addition & 0 deletions workspaces/config/lib/definitions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const shorthands = {
readonly: ['--read-only'],
reg: ['--registry'],
iwr: ['--include-workspace-root'],
ws: ['--workspaces'],
...definitionProps.shorthands,
}

Expand Down
16 changes: 16 additions & 0 deletions workspaces/config/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ const {
mkdir,
} = require('node:fs/promises')

// TODO these need to be either be ignored when parsing env, formalized as config, or not exported to the env in the first place. For now this list is just to suppress warnings till we can pay off this tech debt.
const internalEnv = [
'global-prefix',
'local-prefix',
'npm-version',
'node-gyp',
]

const fileExists = (...p) => stat(resolve(...p))
.then((st) => st.isFile())
.catch(() => false)
Expand Down Expand Up @@ -349,6 +357,11 @@ class Config {
}

loadCLI () {
for (const s of Object.keys(this.shorthands)) {
if (s.length > 1 && this.argv.includes(`-${s}`)) {
log.warn(`-${s} is not a valid single-hyphen cli flag and will be removed in the future`)
}
}
nopt.invalidHandler = (k, val, type) =>
this.invalidHandler(k, val, type, 'command line options', 'cli')
const conf = nopt(this.types, this.shorthands, this.argv)
Expand Down Expand Up @@ -580,6 +593,9 @@ class Config {

#checkUnknown (where, key) {
if (!this.definitions[key]) {
if (internalEnv.includes(key)) {
return
}
if (!key.includes(':')) {
log.warn(`Unknown ${where} config "${where === 'cli' ? '--' : ''}${key}". This will stop working in the next major version of npm.`)
return
Expand Down
23 changes: 23 additions & 0 deletions workspaces/config/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1519,3 +1519,26 @@ t.test('catch project config prefix error', async t => {
'error', 'config', `prefix cannot be changed from project config: ${path}`,
]], 'Expected error logged')
})

t.test('invalid single hyphen 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, '-ws', '-iwr'],
cwd: path,
shorthands,
definitions,
nerfDarts,
})
await config.load()
const filtered = logs.filter(l => l[0] === 'warn')
t.match(filtered, [
['warn', '-iwr is not a valid single-hyphen cli flag and will be removed in the future'],
['warn', '-ws is not a valid single-hyphen cli flag and will be removed in the future'],
], 'Warns about single hyphen configs')
})

0 comments on commit 593c849

Please sign in to comment.