fix(cmd): -C operates on the given directory, not the Git root - #194
nicknsheth-beep wants to merge 1 commit into
Conversation
`-C <dir>` (and --project-root) resolved the nearest enclosing Git repository root and chdir'd there instead of into <dir> itself, so pointing codemap at a subfolder that isn't its own Git repo (e.g. a skill folder nested a few levels into a larger repo) silently re-scoped the scan to the whole repository. `git -C` never reinterprets its argument this way, and codemap's own --help text already documents "-C <repo> Operate on code in <repo>" — the old behavior didn't match its own contract. cmd.InvocationRoots gains an `Operate` field: the literal, canonicalized directory the caller named (falling back to the Git-root walk-up only when that directory doesn't exist, so a bad path still fails the same way). Project/Setup/Runtime keep the existing Git-boundary walk-up unchanged, since storage/config/linked-worktree/submodule discovery already self-heals by walking up from any cwd (projectpath.Select), so it doesn't need cwd itself to be a repo root. TestApplyGlobalRootOptions asserted the old (buggy) behavior directly; updated its expectation to the documented contract. Added TestDashCScopesToTheGivenDirectory, which confirms `-C sub` from the repo root now reports the same file count as running codemap directly from inside `sub`, and that both differ from the whole-repo count. Every existing root-option e2e test (linked worktree inheritance, submodule detection, explicit --setup-root, malformed-metadata rejection) still passes unchanged, since those scenarios all resolve to the same directory either way or rely on projectpath.Select's own walk-up, which is untouched.
|
Thanks — this one is well reasoned, and the PR body's honesty about which existing test asserted the old behavior made it easy to review. I found the behavior change is real and mostly does what you describe, but it also has a consequence the PR doesn't mention, and it's a contract decision rather than a bug, so I'm laying out what I measured. What I ranCI hasn't run (fork workflows await maintainer approval), so locally, comparing a build of
The consequence worth deciding onFile arguments now resolve against
And it can undercount with The sibling package's importer is silently absent. That's the same thing Two smaller knock-on effects: SuggestionWhether Generated by Claude Code |
Problem
-C <dir>(documented as "Operate on code in<repo>") doesn't scope the operation to<dir>when<dir>is a subdirectory of a Git repo rather than a repo root. It silently re-anchors to the nearest enclosing Git root instead, so-C <subdir>applies the whole repo's scan scope.Repro
-C sub .should match running codemap directly from insidesub/, the same waygit -C <dir>behaves.Root cause
The global root resolver walks upward from the given
-Cdirectory to the nearest ancestor containing.git, thenchdirs there — not to the literal directory passed in.Fix
cmd/root.go:InvocationRootsgains anOperatefield — the literal, canonicalized directory the caller named, falling back to the existing git-root walk-up only when that literal directory doesn't exist (so a bad path still fails the same way as before).Project/Setup/Runtimekeep their existing walk-up unchanged, since storage/config discovery (projectpath.Select) already self-heals by walking up from any cwd and doesn't need cwd itself to be a repo root.main.gonowchdirs toroots.Operateinstead ofroots.Project.TestApplyGlobalRootOptionsinmain_more_test.goasserted the old, buggy behavior directly; updated its expectation to match the documented contract. Every other existing root-option e2e test (linked worktree inheritance, submodule detection, explicit--setup-root, malformed-metadata rejection) passes unchanged — those scenarios resolve to the same directory either way, or rely purely onprojectpath.Select's own walk-up, which this change doesn't touch.Tests
root_options_e2e_test.go:TestDashCScopesToTheGivenDirectory—-C subfrom the repo root now reports the same file count as running codemap directly from insidesub, and both differ from the whole-repo count.go test ./...passes (all 17 packages),gofmt -l .andgo vet ./...are clean.Related: filing alongside two other independent fixes found during the same investigation (hidden-directory scanning, and coverage-honesty for unindexed files) — happy to merge in any order, this one has no dependency on the others.