From f1b2d1aa7ee6699a5122539019c7c8e96bf32f34 Mon Sep 17 00:00:00 2001 From: Gar Date: Thu, 30 Jan 2025 09:37:11 -0800 Subject: [PATCH] fix: warn on invalid single-hyphen cli flags 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. --- tap-snapshots/test/lib/docs.js.test.cjs | 1 + workspaces/config/lib/definitions/index.js | 1 + workspaces/config/lib/index.js | 16 +++++++++++++++ workspaces/config/test/index.js | 23 ++++++++++++++++++++++ 4 files changed, 41 insertions(+) diff --git a/tap-snapshots/test/lib/docs.js.test.cjs b/tap-snapshots/test/lib/docs.js.test.cjs index 0603424bbee0c..e43577271929f 100644 --- a/tap-snapshots/test/lib/docs.js.test.cjs +++ b/tap-snapshots/test/lib/docs.js.test.cjs @@ -2547,6 +2547,7 @@ exports[`test/lib/docs.js TAP shorthands > docs 1`] = ` * \`--help\`: \`--usage\` * \`-v\`: \`--version\` * \`-w\`: \`--workspace\` +* \`--ws\`: \`--workspaces\` * \`-y\`: \`--yes\` ` diff --git a/workspaces/config/lib/definitions/index.js b/workspaces/config/lib/definitions/index.js index cd781f2edf159..793b71ea40d6f 100644 --- a/workspaces/config/lib/definitions/index.js +++ b/workspaces/config/lib/definitions/index.js @@ -55,6 +55,7 @@ const shorthands = { readonly: ['--read-only'], reg: ['--registry'], iwr: ['--include-workspace-root'], + ws: ['--workspaces'], ...definitionProps.shorthands, } diff --git a/workspaces/config/lib/index.js b/workspaces/config/lib/index.js index d56dfe6551e8d..fbc8c2365ac0d 100644 --- a/workspaces/config/lib/index.js +++ b/workspaces/config/lib/index.js @@ -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) @@ -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) @@ -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 diff --git a/workspaces/config/test/index.js b/workspaces/config/test/index.js index 9e56f2428ceb1..9e93b593731c7 100644 --- a/workspaces/config/test/index.js +++ b/workspaces/config/test/index.js @@ -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') +})