refactor(config): split Config interface into settings + runtime context (#11197)

* refactor(config): split Config interface into settings + runtime context

Create ConfigContext for runtime state (hooks, finders, workspace graph,
CLI metadata) and keep Config for user-facing settings only. Functions
use Pick<Config, ...> & Pick<ConfigContext, ...> to express which fields
they need from each interface.

getConfig() now returns { config, context, warnings }. The CLI wrapper
returns { config, context } and spreads both when calling command
handlers (to be refactored to separate params in follow-up PRs).

Closes #11195

* fix: address review feedback

- Initialize cliOptions on pnpmConfig so context.cliOptions is never undefined
- Move rootProjectManifestDir assignment before ignoreLocalSettings guard
- Add allProjectsGraph to INTERNAL_CONFIG_KEYS

* refactor: remove INTERNAL_CONFIG_KEYS from configToRecord

configToRecord now accepts Config and ConfigContext separately, so
context fields are never in scope. Only auth-related Config fields
(authConfig, authInfos, sslConfigs) need filtering.

* refactor: eliminate INTERNAL_CONFIG_KEYS from configToRecord

configToRecord now receives the clean Config object and explicitlySetKeys
separately (via opts.config and opts.context), so context fields are
never in scope. main.ts passes the original split objects alongside
the spread for command handlers that need them.

* fix: spelling

* fix: import sorting

* fix: --config.xxx nconf overrides conflicting with --config CLI flag

When `pnpm add` registers `config: Boolean`, nopt captures
--config.xxx=yyy as the --config flag value instead of treating it
as a nconf-style config override. Fix by extracting --config.xxx args
before nopt parsing and re-parsing them separately.

Also rename the split config/context properties on the command opts
object to _config/_context to avoid clashing with the --config CLI option.
This commit is contained in:
Zoltan Kochan
2026-04-04 23:44:25 +02:00
committed by GitHub
parent 96704a1c58
commit 3033bee430
66 changed files with 602 additions and 473 deletions

View File

@@ -129,6 +129,22 @@ export async function parseCliArgs (
return [noptExploratoryResults.argv.remain[indexOfRunScriptName]]
}
// When "config" is a registered CLI option (e.g. `pnpm add --config`),
// nopt captures --config.xxx=yyy as the "config" flag value instead of
// treating it as the nconf-style config override syntax. Work around this
// by rewriting --config.xxx=yyy to a placeholder before nopt, then restoring.
const hasConfigOption = 'config' in types
const configDotArgs: string[] = []
const filteredArgv = hasConfigOption
? inputArgv.map(arg => {
if (arg.startsWith('--config.')) {
configDotArgs.push(arg)
return undefined
}
return arg
}).filter((arg): arg is string => arg !== undefined)
: inputArgv
const { argv, ...options } = nopt(
{
recursive: Boolean,
@@ -138,10 +154,17 @@ export async function parseCliArgs (
...opts.universalShorthands,
...opts.shorthandsByCommandName[commandName],
},
inputArgv,
filteredArgv,
0,
{ escapeArgs: getEscapeArgsWithSpecialCases() }
)
// Re-parse extracted --config.xxx args through nopt so they get proper
// type coercion (e.g. "false" → false for Boolean settings).
if (configDotArgs.length > 0) {
const { argv: _, ...configOptions } = nopt({}, {}, configDotArgs, 0)
Object.assign(options, configOptions)
}
const workspaceDir = await getWorkspaceDir(options)
// For the run command, it's not clear whether --help should be passed to the