Skip to content

feat(extensions): dispatch cu <word> to cu-<word> on PATH - #53

Merged
timimsms merged 3 commits into
mainfrom
feat/extension-dispatch
Nov 15, 2026
Merged

timimsms merged 3 commits into
mainfrom
feat/extension-dispatch

Conversation

@timimsms

@timimsms timimsms commented Sep 23, 2026 •

Copy link
Copy Markdown
Owner

Summary

Implements the kubectl-style PATH passthrough promised in PROJECT_SPEC §4.3 and specified in the context-layer design §3.6 — the subset that does not depend on refs, aliases or packs, so it can land on its own.

Any executable named cu-<word> on PATH becomes cu <word>:

$ cu demo plan --flag x        # runs cu-demo
args: plan --flag x
CU_CONFIG_DIR=/Users/tim/.config/cu
CU_PROJECT_CONFIG=/path/to/.cu.yml
$ echo $?
42                              # the extension's own exit code

Guarantees

  • Builtins always win. Dispatch is attempted only for a word cobra itself could not resolve, so an extension can never shadow a cu command. Verified with a cu-version on PATH: cu version still runs the builtin, while cu-version remains runnable directly — the escape hatch §3.6 calls for.
  • No startup cost. No PATH scan; one exec.LookPath only once a word is known to be unresolvable.
  • Unknown words still get cobra's error. If no matching extension exists, dispatch falls through so you keep the did-you-mean suggestions.
  • Exit status is propagated unchanged.

The part most worth reviewing

Finding the command word has a sharp edge: cu --config x worklog must dispatch to worklog, not to x. Guessing "first argument not starting with -" gets that wrong. Flag arity is read from the flag set instead, so value-taking flags consume their value and boolean flags do not. An unknown flag is assumed not to take a value, so it cannot swallow the command word.

Everything after the word is forwarded verbatim — including flags that look like cu's own, since they belong to the extension — while cu's own preceding flags are not.

Environment handed to the child

CU_CONFIG_DIR, CU_WORKSPACE, CU_PROJECT_CONFIG, per §3.6, so an extension does not have to rediscover cu's context. Config is loaded explicitly here because dispatch happens before PersistentPreRunE; a load failure is non-fatal and simply means less context.

Not included

cu which (shadowing/non-executable warnings) belongs with the wider context-layer work, as do aliases, refs and packs. This is only §5's step 4.

Tests

TestFirstCommandWord covers bare words, long and shorthand flags with values in both --flag value and --flag=value forms, boolean flags, unknown flags, and --. TestArgsAfter covers forwarding and the non-forwarding of cu's own flags. TestConfigFileFrom includes a dangling --config so it cannot panic. TestTryExtensionLeavesBuiltinsAlone asserts resolvable builtins return normally rather than being routed out.

Manually verified: dispatch with args, a global flag before the word, builtin precedence, the direct-invocation escape hatch, CU_PROJECT_CONFIG inside a project, and exit-code propagation.

Checklist

  • ./scripts/ci.sh passes locally — except errcheck, which reports the same pre-existing findings on main, none in files this PR touches
  • Commit messages use conventional prefixes
  • CLI docs regenerated — no builtin help text changed, so no doc diff
  • Docs updated if user-facing behavior changed — README gains an Extensions section

Review fixes

  • cu help cannot be hijacked. cobra only registers its default help (and completion) command inside Execute(), so Find couldn't see it and a cu-help on PATH would run in its place. Dispatch now calls InitDefaultHelpCmd() / InitDefaultCompletionCmd() before Find.
  • Args are forwarded by position. firstCommandWord now returns the word's index, and the extension gets args[idx+1:]. Before, slicing at the first string equal to the word meant cu --config hello hello real forwarded hello real; it now forwards real. argsAfter is gone.
  • An extension's --config stays with the extension. --config is now read only from the arguments before the extension word, so cu hello --config x no longer changes cu's own config.
  • Signal exits map to 128+signal on Unix. A child killed by a signal used to make cu exit 255. A build-tagged exitStatus helper reads syscall.WaitStatus on Unix and falls back to ExitCode() elsewhere (GOOS=windows go vet is clean).
  • New exec-based tests (TestExtensionDispatchExec, skipped on Windows) run the real Execute path in a child process with a fake cu-hello on a temporary PATH. They cover verbatim forwarding, the non-forwarding of cu's own flags, cu --config hello hello real → real, a trailing --config that stays with the extension, exit-code passthrough, SIGTERM → 143, and a cu-help on PATH that does not replace cu help. TestFirstCommandWord now asserts indices and includes the repeated-word case.

🤖 Generated with Claude Code

@timimsms
timimsms force-pushed the feat/extension-dispatch branch from a3c79f0 to 200bc07 Compare November 15, 2026 18:24
timimsms and others added 3 commits November 15, 2026 11:26
Implements the kubectl-style passthrough promised in PROJECT_SPEC §4.3 and
specified in the context-layer design §3.6 — the subset that does not depend on
refs, aliases or packs, so it can land on its own.

Builtins always win: dispatch is attempted only for a word cobra itself could
not resolve, so an extension can never shadow a cu command, and a shadowed
extension is still runnable directly as cu-<word>. There is no PATH scan at
startup — a single exec.LookPath once a word is known to be unresolvable — so
the normal path costs nothing.

The command word is found by reading flag arity from the flag set rather than
guessing, so `cu --config x worklog` dispatches to worklog and not to x.
Everything after the word is forwarded verbatim, including flags that look like
cu's own; cu's own preceding flags are not.

The child inherits CU_CONFIG_DIR, CU_WORKSPACE and CU_PROJECT_CONFIG, and its
exit status is propagated unchanged.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014aqbmccWm1tqttmBUCR5rv
ClickUp: 86dxbeqyt
CI's gosec caught a real one. exec.LookPath treats any word containing a
separator as a *path* rather than a PATH search, so `cu ../../tmp/evil` would
have resolved and run an executable that was never on PATH at all.

Extension words are now constrained to ^[a-zA-Z0-9][a-zA-Z0-9_-]*$, so an
extension is found on PATH, by name, or not at all; anything else falls through
to cobra's unknown-command error. Verified live that ./evil and ../outside/evil
are both refused while normal dispatch still works.

The suppression comment was also wrong: it named G204 while the rule that fires
is G702 (taint analysis), so it never applied to begin with. It now names both
and explains why the call is safe rather than asserting it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014aqbmccWm1tqttmBUCR5rv
ClickUp: 86dxbeqyt
- Register cobra's default help and completion commands before Find, so a
  cu-help on PATH can no longer replace cu help.
- firstCommandWord returns the command word's index; forward args[idx+1:]
  so cu --config hello hello real passes only real.
- Read --config only from the arguments before the extension word, so an
  extension's own --config no longer changes cu's config.
- Map a signal-killed extension to 128+signal on Unix instead of 255.
- Add exec-based tests with a fake extension on a temp PATH.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@timimsms
timimsms force-pushed the feat/extension-dispatch branch from 200bc07 to d567d75 Compare November 15, 2026 18:26
@timimsms
timimsms merged commit f1236b5 into main Nov 15, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant