Skip to content

Warn on unknown configs #8071

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 5 commits 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
16 changes: 2 additions & 14 deletions lib/commands/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,11 @@ const { spawn } = require('node:child_process')
const { EOL } = require('node:os')
const localeCompare = require('@isaacs/string-locale-compare')('en')
const pkgJson = require('@npmcli/package-json')
const { defaults, definitions } = require('@npmcli/config/lib/definitions')
const { defaults, definitions, nerfDarts } = require('@npmcli/config/lib/definitions')
const { log, output } = require('proc-log')
const BaseCommand = require('../base-cmd.js')
const { redact } = require('@npmcli/redact')

// These are the configs that we can nerf-dart. Not all of them currently even
// *have* config definitions so we have to explicitly validate them here.
// This is used to validate during "npm config set"
const nerfDarts = [
'_auth',
'_authToken',
'_password',
'certfile',
'email',
'keyfile',
'username',
]
// These are the config values to swap with "protected". It does not catch
// every single sensitive thing a user may put in the npmrc file but it gets
// the common ones. This is distinct from nerfDarts because that is used to
Expand Down Expand Up @@ -125,7 +113,7 @@ class Config extends BaseCommand {
const action = argv[2]
switch (action) {
case 'set':
// todo: complete with valid values, if possible.
// TODO: complete with valid values, if possible.
if (argv.length > 3) {
return []
}
Expand Down
3 changes: 2 additions & 1 deletion lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { resolve, dirname, join } = require('node:path')
const Config = require('@npmcli/config')
const which = require('which')
const fs = require('node:fs/promises')
const { definitions, flatten, shorthands } = require('@npmcli/config/lib/definitions')
const { definitions, flatten, nerfDarts, shorthands } = require('@npmcli/config/lib/definitions')
const usage = require('./utils/npm-usage.js')
const LogFile = require('./utils/log-file.js')
const Timers = require('./utils/timers.js')
Expand Down Expand Up @@ -68,6 +68,7 @@ class Npm {
npmPath: this.#npmRoot,
definitions,
flatten,
nerfDarts,
shorthands,
argv: [...process.argv, ...argv],
excludeNpmCwd,
Expand Down
3 changes: 0 additions & 3 deletions node_modules/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@
!/node-gyp/node_modules/tar
!/node-gyp/node_modules/yallist
!/nopt
!/nopt/node_modules/
/nopt/node_modules/*
!/nopt/node_modules/abbrev
!/normalize-package-data
!/npm-audit-report
!/npm-bundled
Expand Down
45 changes: 40 additions & 5 deletions node_modules/nopt/lib/nopt-lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ function nopt (args, {
types,
shorthands,
typeDefs,
invalidHandler,
invalidHandler, // opt is configured but its value does not validate against given type
unknownHandler, // opt is not configured
abbrevHandler, // opt is being expanded via abbrev
typeDefault,
dynamicTypes,
} = {}) {
Expand All @@ -38,7 +40,9 @@ function nopt (args, {
original: args.slice(0),
}

parse(args, data, argv.remain, { typeDefs, types, dynamicTypes, shorthands })
parse(args, data, argv.remain, {
typeDefs, types, dynamicTypes, shorthands, unknownHandler, abbrevHandler,
})

// now data is full
clean(data, { types, dynamicTypes, typeDefs, invalidHandler, typeDefault })
Expand Down Expand Up @@ -247,6 +251,8 @@ function parse (args, data, remain, {
typeDefs = {},
shorthands = {},
dynamicTypes,
unknownHandler,
abbrevHandler,
} = {}) {
const StringType = typeDefs.String?.type
const NumberType = typeDefs.Number?.type
Expand Down Expand Up @@ -282,7 +288,7 @@ function parse (args, data, remain, {

// see if it's a shorthand
// if so, splice and back up to re-parse it.
const shRes = resolveShort(arg, shortAbbr, abbrevs, { shorthands })
const shRes = resolveShort(arg, shortAbbr, abbrevs, { shorthands, abbrevHandler })
debug('arg=%j shRes=%j', arg, shRes)
if (shRes) {
args.splice.apply(args, [i, 1].concat(shRes))
Expand All @@ -298,7 +304,13 @@ function parse (args, data, remain, {
arg = arg.slice(3)
}

if (abbrevs[arg]) {
// abbrev includes the original full string in its abbrev list
if (abbrevs[arg] && abbrevs[arg] !== arg) {
if (abbrevHandler) {
abbrevHandler(arg, abbrevs[arg])
} else if (abbrevHandler !== false) {
debug(`abbrev: ${arg} -> ${abbrevs[arg]}`)
}
arg = abbrevs[arg]
}

Expand Down Expand Up @@ -331,6 +343,23 @@ function parse (args, data, remain, {
(argType === null ||
isTypeArray && ~argType.indexOf(null)))

if (typeof argType === 'undefined') {
// la is going to unexpectedly be parsed outside the context of this arg
const hangingLa = !hadEq && la && !la?.startsWith('-') && !['true', 'false'].includes(la)
if (unknownHandler) {
if (hangingLa) {
unknownHandler(arg, la)
} else {
unknownHandler(arg)
}
} else if (unknownHandler !== false) {
debug(`unknown: ${arg}`)
if (hangingLa) {
debug(`unknown: ${la} parsed as normal opt`)
}
}
}

if (isBool) {
// just set and move along
val = !no
Expand Down Expand Up @@ -420,7 +449,7 @@ const singleCharacters = (arg, shorthands) => {
}

function resolveShort (arg, ...rest) {
const { types = {}, shorthands = {} } = rest.length ? rest.pop() : {}
const { abbrevHandler, types = {}, shorthands = {} } = rest.length ? rest.pop() : {}
const shortAbbr = rest[0] ?? abbrev(Object.keys(shorthands))
const abbrevs = rest[1] ?? abbrev(Object.keys(types))

Expand Down Expand Up @@ -457,7 +486,13 @@ function resolveShort (arg, ...rest) {
}

// if it's an abbr for a shorthand, then use that
// exact match has already happened so we don't need to account for that here
if (shortAbbr[arg]) {
if (abbrevHandler) {
abbrevHandler(arg, shortAbbr[arg])
} else if (abbrevHandler !== false) {
debug(`abbrev: ${arg} -> ${shortAbbr[arg]}`)
}
arg = shortAbbr[arg]
}

Expand Down
4 changes: 4 additions & 0 deletions node_modules/nopt/lib/nopt.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ function nopt (types, shorthands, args = process.argv, slice = 2) {
shorthands: shorthands || {},
typeDefs: exports.typeDefs,
invalidHandler: exports.invalidHandler,
unknownHandler: exports.unknownHandler,
abbrevHandler: exports.abbrevHandler,
})
}

Expand All @@ -26,5 +28,7 @@ function clean (data, types, typeDefs = exports.typeDefs) {
types: types || {},
typeDefs,
invalidHandler: exports.invalidHandler,
unknownHandler: exports.unknownHandler,
abbrevHandler: exports.abbrevHandler,
})
}
46 changes: 0 additions & 46 deletions node_modules/nopt/node_modules/abbrev/LICENSE

This file was deleted.

50 changes: 0 additions & 50 deletions node_modules/nopt/node_modules/abbrev/lib/index.js

This file was deleted.

43 changes: 0 additions & 43 deletions node_modules/nopt/node_modules/abbrev/package.json

This file was deleted.

8 changes: 4 additions & 4 deletions node_modules/nopt/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nopt",
"version": "8.0.0",
"version": "8.1.0",
"description": "Option parsing for Node, supporting types, shorthands, etc. Used by npm.",
"author": "GitHub Inc.",
"main": "lib/nopt.js",
Expand All @@ -23,11 +23,11 @@
},
"license": "ISC",
"dependencies": {
"abbrev": "^2.0.0"
"abbrev": "^3.0.0"
},
"devDependencies": {
"@npmcli/eslint-config": "^5.0.0",
"@npmcli/template-oss": "4.23.3",
"@npmcli/template-oss": "4.23.6",
"tap": "^16.3.0"
},
"tap": {
Expand All @@ -46,7 +46,7 @@
"templateOSS": {
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
"windowsCI": false,
"version": "4.23.3",
"version": "4.23.6",
"publish": true
}
}
Loading