feat: add -C <path> flag to change directory before running#3442
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
7b3593a to
2cc297d
Compare
|
Rebased onto current main. Resolved one conflict in |
maphew
left a comment
There was a problem hiding this comment.
Thanks for the -C flag implementation. The feature is useful, but I’m requesting changes because the current implementation mutates process-global cwd in PersistentPreRun via os.Chdir. That risk profile is too high for this CLI: in-process command executions/tests can leak cwd state across commands, and startup/global command behavior is already sensitive.\n\nPlease rework this toward selected-context routing or target repo resolution instead of global cwd mutation. At minimum, add stronger coverage for invalid -C paths and repeated/in-process command execution so cwd cannot leak across commands.\n\nSuggested validation after rework: scripts/pr-preflight.sh 3442 --repo gastownhall/beads; CGO_ENABLED=1 go test -tags gms_pure_go ./cmd/bd -run '^TestEmbeddedReady/ready_with_C_flag$'; make test.
Adds a persistent -C/--directory flag (matching git -C) that calls os.Chdir before .beads/ discovery or database initialisation. Allows running bd commands against a different project without changing directory first. Example: bd -C ~/projects/myapp ready Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2cc297d to
b3c8ab8
Compare
|
Addressed the os.Chdir concern. What changed: Instead of permanently mutating the process cwd, the -C handler now:
Net effect: the process cwd is unchanged after -C processing. BEADS_DIR is already the canonical mechanism for pointing at a project, and New test coverage (three cases added):
|
Resolve -C by binding command selection to the target project's .beads directory instead of calling os.Chdir. Add focused coverage for invalid targets and no process cwd mutation.\n\nCo-authored-by: Robin Carswell <2803445+rjc123@users.noreply.github.com>
|
Addressed the |
maphew
left a comment
There was a problem hiding this comment.
The cwd-mutation concern is addressed in f68f3d3. The implementation now resolves the target beads project without os.Chdir, scopes/restores selector env, and has focused invalid-target/no-cwd-mutation coverage. Local validation passed: targeted cmd/bd resolver test, internal/beads test, embedded ready -C subtests with BEADS_TEST_EMBEDDED_DOLT=1, and make test.
t.TempDir() returns paths under /var/folders on macOS, but resolveChangeDirBeadsDir canonicalizes via EvalSymlinks to /private/var/folders. Resolve projectDir up front so the expected and actual paths match on every OS.
Summary
-C/--directoryflag to the root command, matchinggit -Cbehaviouros.Chdir()early inPersistentPreRun— before any.beads/discovery, environment loading, or store initialisationbdcommands against a different project directory withoutcdfirstMotivation
AI coding agents (Claude Code, Cursor, etc.) often run in a working directory different from the beads project. Currently this requires either:
cd /path/to/project && bd ready(chaining, which some agent frameworks disallow)BEADS_DIRenv var (verbose, easy to forget)git -Csolves this problem cleanly for git —bd -Cbrings the same ergonomics.Example
Test plan
ready_with_C_flaginready_embedded_test.go— runsbd readyfrom a temp dir using-Cto target the beads project-Ccontinue to work (no default changes)🤖 Generated with Claude Code