|
| 1 | +// Package projectroot is the layered project-root resolver used by every |
| 2 | +// CLI subcommand and the MCP server. |
| 3 | +// |
| 4 | +// Resolution order (highest wins): |
| 5 | +// |
| 6 | +// 1. Explicit positional argument (the legacy behavior; `codeiq <cmd> <path>`). |
| 7 | +// 2. `CODEIQ_PROJECT_ROOT` environment variable. Useful for wrappers and CI. |
| 8 | +// 3. Walk up from the current working directory looking for `.codeiq/` |
| 9 | +// (already-indexed project; strongest signal that this is the root). |
| 10 | +// 4. Walk up from the current working directory looking for `.git/` (repo root). |
| 11 | +// 5. Error with an actionable message. |
| 12 | +// |
| 13 | +// The MCP server adds a sixth signal at the top of the chain — the MCP |
| 14 | +// client's `ListRoots` response — wired separately in `internal/mcp` because |
| 15 | +// it requires an active session. |
| 16 | +package projectroot |
| 17 | + |
| 18 | +import ( |
| 19 | + "errors" |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | +) |
| 24 | + |
| 25 | +// EnvVar is the environment variable consulted by Resolve. |
| 26 | +const EnvVar = "CODEIQ_PROJECT_ROOT" |
| 27 | + |
| 28 | +// Markers walked up the directory tree. |
| 29 | +const ( |
| 30 | + graphMarker = ".codeiq/graph/codeiq.kuzu" // strongest signal |
| 31 | + gitMarker = ".git" // fallback |
| 32 | +) |
| 33 | + |
| 34 | +// ErrNotFound is returned when no resolution succeeds. |
| 35 | +var ErrNotFound = errors.New("project root could not be resolved from arg, " + EnvVar + ", or filesystem walk-up") |
| 36 | + |
| 37 | +// Options bundles the resolution inputs. Pass empty strings to skip a layer. |
| 38 | +// - Arg: the positional argument (or "" if the user didn't supply one). |
| 39 | +// - EnvValue: the value of CODEIQ_PROJECT_ROOT (or "" if unset). |
| 40 | +// - CWD: the current working directory (typically os.Getwd()). |
| 41 | +type Options struct { |
| 42 | + Arg string |
| 43 | + EnvValue string |
| 44 | + CWD string |
| 45 | +} |
| 46 | + |
| 47 | +// Resolve runs the layered resolution chain. Returns an absolute, validated |
| 48 | +// directory path on success. |
| 49 | +// |
| 50 | +// Any non-empty Arg or EnvValue that points at a non-directory is an error |
| 51 | +// (we don't silently fall through user-supplied paths — it's almost always |
| 52 | +// a typo we want surfaced). |
| 53 | +func Resolve(opts Options) (string, error) { |
| 54 | + if opts.Arg != "" { |
| 55 | + return validateDir(opts.Arg, "argument") |
| 56 | + } |
| 57 | + if opts.EnvValue != "" { |
| 58 | + return validateDir(opts.EnvValue, EnvVar) |
| 59 | + } |
| 60 | + if opts.CWD == "" { |
| 61 | + return "", ErrNotFound |
| 62 | + } |
| 63 | + if root, ok := WalkUp(opts.CWD); ok { |
| 64 | + return root, nil |
| 65 | + } |
| 66 | + return "", ErrNotFound |
| 67 | +} |
| 68 | + |
| 69 | +// WalkUp walks up from start looking for `.codeiq/graph/codeiq.kuzu` first |
| 70 | +// (already-indexed), then `.git` (repo root). Returns the matching ancestor |
| 71 | +// directory and true, or ("", false). |
| 72 | +// |
| 73 | +// start must be an absolute path; if not, it's resolved against the current |
| 74 | +// working directory at call time. |
| 75 | +func WalkUp(start string) (string, bool) { |
| 76 | + abs, err := filepath.Abs(start) |
| 77 | + if err != nil { |
| 78 | + return "", false |
| 79 | + } |
| 80 | + // First pass: prefer .codeiq/ because it tells us the project has been |
| 81 | + // indexed (the user almost certainly meant THIS root). Second pass: fall |
| 82 | + // back to .git/ because nearly every codebase has one. |
| 83 | + for _, marker := range []string{graphMarker, gitMarker} { |
| 84 | + if hit, ok := walkUpFor(abs, marker); ok { |
| 85 | + return hit, true |
| 86 | + } |
| 87 | + } |
| 88 | + return "", false |
| 89 | +} |
| 90 | + |
| 91 | +// walkUpFor walks dir → dir/.. → dir/../.. looking for marker. Stops at |
| 92 | +// filesystem root. |
| 93 | +func walkUpFor(dir, marker string) (string, bool) { |
| 94 | + for { |
| 95 | + candidate := filepath.Join(dir, marker) |
| 96 | + if _, err := os.Stat(candidate); err == nil { |
| 97 | + return dir, true |
| 98 | + } |
| 99 | + parent := filepath.Dir(dir) |
| 100 | + if parent == dir { // hit filesystem root |
| 101 | + return "", false |
| 102 | + } |
| 103 | + dir = parent |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// FromArgs is the call-site sugar used by every CLI subcommand. It bundles |
| 108 | +// args (the cobra positional slice), the env, and the cwd into Options and |
| 109 | +// runs Resolve. Cobra's `MaximumNArgs(1)` plus this helper means subcommands |
| 110 | +// stay tiny. |
| 111 | +func FromArgs(args []string) (string, error) { |
| 112 | + cwd, _ := os.Getwd() // best-effort; if it fails Resolve falls through to ErrNotFound |
| 113 | + arg := "" |
| 114 | + if len(args) > 0 { |
| 115 | + arg = args[0] |
| 116 | + } |
| 117 | + return Resolve(Options{ |
| 118 | + Arg: arg, |
| 119 | + EnvValue: os.Getenv(EnvVar), |
| 120 | + CWD: cwd, |
| 121 | + }) |
| 122 | +} |
| 123 | + |
| 124 | +// validateDir absolute-izes p and confirms it's an existing directory. |
| 125 | +// label is for the error message ("argument" / "CODEIQ_PROJECT_ROOT"). |
| 126 | +func validateDir(p, label string) (string, error) { |
| 127 | + abs, err := filepath.Abs(p) |
| 128 | + if err != nil { |
| 129 | + return "", fmt.Errorf("resolve %s %q: %w", label, p, err) |
| 130 | + } |
| 131 | + st, err := os.Stat(abs) |
| 132 | + if err != nil { |
| 133 | + return "", fmt.Errorf("%s %q does not exist", label, abs) |
| 134 | + } |
| 135 | + if !st.IsDir() { |
| 136 | + return "", fmt.Errorf("%s %q is not a directory", label, abs) |
| 137 | + } |
| 138 | + return abs, nil |
| 139 | +} |
0 commit comments