Skip to content
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

fix: warn on invalid single-hyphen cli flags #8076

Merged
merged 1 commit into from
Jan 30, 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
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')
})
Loading