Fix --debug rejected on many subcommands#1938
Open
mliem2k wants to merge 1 commit into
Open
Conversation
Application routed unrecognized top-level input to DefaultCommand via ArgumentParser's defaultSubcommand, which combined with DefaultCommand's .captureForPassthrough argument. That combination breaks flag parsing for unrelated, legitimately-matched subcommands throughout the tree: `container system status --debug`, `container system version --debug`, `container image inspect/delete/pull/tag/save --debug`, `container volume/network inspect --debug`, and `container builder status/delete --debug` all rejected --debug with "Unknown option '--debug'. Did you mean '--debug'?", even though it's listed in each command's own usage text. Confirmed with a minimal ArgumentParser reproduction that this persists through the latest 1.8.2 release and is triggered purely by defaultSubcommand pointing to a command with a .captureForPassthrough argument, regardless of the leaf command's own option declarations. Remove defaultSubcommand from Application's configuration and instead decide up front, in main(), whether input should be routed to DefaultCommand (plugin dispatch, or the plugin-aware help text) versus parsed normally via parseAsRoot. This keeps DefaultCommand's plugin dispatch behavior (including passing a plugin's own --debug flag through unchanged, rather than swallowing it) while no longer relying on the ArgumentParser mechanism that broke parsing elsewhere.
Contributor
|
@mliem2k We're still working on some issues that are preventing us from spending more time on contributor PRs, but in the meantime I closed your issue as a dupe of #360 and updated your PR description to match. There is another PR open for this but at first glance, what you've done seems like it could be a better approach. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #360.
container system status --debug(and many other subcommands) rejected--debugwith:even though
--debugis listed in the command's own usage text.Root cause:
Application'sCommandConfigurationsetdefaultSubcommand: DefaultCommand.self(used to dispatch unrecognized top-level input to plugins).DefaultCommanddeclares@Argument(parsing: .captureForPassthrough) var remaining: [String]. That specific combination —defaultSubcommandpointing at a command with a.captureForPassthrough/.allRemainingInputargument — breaks ArgumentParser's flag recognition for unrelated, legitimately-matched subcommands throughout the whole tree, not just the default/plugin path.I confirmed this against a minimal, isolated
swift-argument-parserreproduction (root command withdefaultSubcommand+.captureForPassthrough, and nested nested subcommands several levels down) — the bug reproduces on ArgumentParser 1.7.0 through the latest 1.8.2, and disappears immediately whendefaultSubcommandis removed. It is not about the leaf command's own option declarations — commands withlogOptionsdeclared in different orders/positions, with or without extra flags, are affected identically as long asdefaultSubcommandis present at the root.In
apple/containerthis affected (at least):system status/version/df,builder status/delete,image inspect/delete/pull/tag/save,network inspect,volume inspect— all rejected--debugwhen it appeared after the subcommand name.Fix
defaultSubcommandfromApplication'sCommandConfiguration.Application.pluginDispatchArguments(_:), which inspects the raw args before callingparseAsRootand decides whether they should go toDefaultCommand(plugin dispatch, or the plugin-aware bare/--debug-only help text) or be parsed normally as one ofApplication's own subcommands. This preservesDefaultCommand's existing plugin-dispatch behavior — including passing a plugin's own--debugflag through to it unchanged, rather than swallowing it (which is what I found happens if you instead switchDefaultCommandto ArgumentParser's.allUnrecognizedstrategy — a plugin invokingcontainer my-plugin --debugwould silently lose its own--debugto the root's global flag).main()also replicates theApplication.validate()side effects (debug log level, Rosetta check) that would otherwise have run on the way to the default subcommand, so plugin dispatch keeps the same behavior forcontainer --debug some-plugin ....Before / after
After the fix (daemon not running in this sandbox, but the flag is now recognized and parsing succeeds):
Verified unchanged behavior for: bare
container(still shows the plugin-aware help with thePLUGINS:section),container --help/-h,container --version,container --debug, unknown-plugin dispatch (container some-plugin --flag arg, including a plugin's own--debugpassed through untouched), and normal subcommands likecontainer image list --debug/container list --debug.Testing
swift buildsucceeds.Tests/ContainerCommandsTests/PluginDispatchArgumentsTests.swift(unit tests for the new dispatch-routing logic).Tests/IntegrationTests/System/TestCLIDebugFlag.swift(regression coverage running the actual CLI for the commands reported in [Bug]: debug flag not accepted #1936).swift testfor the affected suites:ContainerCommandsTests,TestCLIHelp,TestCLIStatus,TestCLIPluginErrors,TestCLIDebugFlag,PluginDispatchArgumentsTests— all 47 tests pass.Test plan
swift buildsucceedscontainer system status --debugno longer errors on--debugcontainer --help,-h,--version, barecontainerunchanged--debugpassed throughswift test --filter ...)