Repository navigation
Expand file tree
/
Copy pathproject.ts
More file actions
236 lines (212 loc) · 13.3 KB
/
Copy pathproject.ts
File metadata and controls
236 lines (212 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/** Project marker detection and path canonicalization. */
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { shortFingerprint } from './fingerprint.js';
import { extractErrorMessage, runGit } from './util.js';
import { canonicalize, foldPath } from './path_containment.js';
import { displaySafeText, normalizeDarwinSystemAlias } from './paths.js';
import { registerReset } from './reset.js';
// Re-exported so every existing `import { canonicalize, isInsideRoot } from './project.js'` keeps resolving to the same live binding. The definitions moved to a leaf module because util.ts's write helpers now run isInsideRoot on every write; see path_containment.ts's header.
export { canonicalize, isInsideRoot, escapesOntoNetworkThroughLinks } from './path_containment.js';
export const PROJECT_MARKERS = [
'.git',
'package.json',
'pyproject.toml',
'Cargo.toml',
'go.mod',
'shopify.app.toml',
'_config.yml',
'deno.json',
'deno.jsonc',
] as const;
const REPO_CONTAINER_THRESHOLD = 3;
/** Project root with hash and marker file. */
export interface Project {
root: string;
hash: string;
marker: string;
}
/** Return SHA256 hash (first 16 chars, hex) of canonical posix path. */
export function projectHash(canonicalRoot: string): string {
// canonicalize() only lowercases the drive letter, not the rest of the path, so two differently-cased strings for the identical physical directory on a case-insensitive filesystem (Windows/macOS) would otherwise hash to different keys and split one project's state directory (compact.ts's writeSessionManifest keys sessions by this hash) across two hashes. Fold through foldPath (util.ts) first, matching the platform-gated convention used elsewhere (isUnderBlockedRoot, assertWalkableRoot, pruneDeletedFiles).
return shortFingerprint(foldPath(canonicalRoot));
}
/** Create a Project for any directory without requiring a project marker. */
export function makeProjectAt(root: string | URL): Project {
let canonical: string;
try {
canonical = canonicalize(root);
} catch (exc) {
throw new Error(
`makeProjectAt: could not resolve path: ${extractErrorMessage(exc)}`,
{ cause: exc }
);
}
try {
const stat = fs.statSync(canonical);
if (!stat.isDirectory()) {
throw new Error(`makeProjectAt: path is not a directory: ${canonical}`);
}
} catch (exc) {
if (exc instanceof Error && exc.message.includes('ENOENT')) {
throw new Error(`makeProjectAt: path does not exist: ${canonical}`, { cause: exc });
}
throw exc;
}
const hash = projectHash(canonical);
return { root: canonical, hash, marker: 'manual' };
}
/** True if path merely contains independent repos rather than being a project. */
function isRepoContainer(pathStr: string): boolean {
let nestedRepos = 0;
try {
const entries = fs.readdirSync(pathStr, { withFileTypes: true });
for (const entry of entries) {
if (entry.isDirectory()) {
// Build the path from pathStr (already known) + entry.name rather than entry.path/ entry.parentPath: entry.path only exists from Node 20.1+ and is undefined on Node 18/19 (this package's declared minimum), which throws inside this try and silently disables repo-container detection via the catch below.
const gitPath = path.join(pathStr, entry.name, '.git');
if (fs.existsSync(gitPath)) {
// A git-submodule (or worktree) root has a `.git` FILE -- a one-line `gitdir: ...` pointer into the superproject's `.git/modules` -- not a real independent repo. Only a `.git` DIRECTORY is a genuinely separate repo root and should count toward the container threshold; otherwise a monorepo with 3+ submodules at its root gets misclassified as a container of unrelated repos, and findProject walks past the actual project root.
let isGitDir = false;
try {
isGitDir = fs.statSync(gitPath).isDirectory();
} catch {
// Race between existsSync and statSync (deleted, permissions): treat as not a nested repo rather than aborting the whole directory scan.
}
if (isGitDir) {
nestedRepos++;
if (nestedRepos >= REPO_CONTAINER_THRESHOLD) {
return true;
}
}
}
}
}
} catch {
return false;
}
return false;
}
/** Return true when marker exists and is not a symlink escaping the root. */
function markerExists(current: string, marker: string): boolean {
const markerPath = path.join(current, marker);
try {
if (!fs.existsSync(markerPath)) {
return false;
}
const stat = fs.lstatSync(markerPath);
if (!stat.isSymbolicLink()) {
return true;
}
// Symlink: verify target stays inside current. On Windows, path.relative() across drive letters (e.g. C:\project -> D:\evil\file) returns the absolute target path unchanged rather than a '..'-prefixed relative path, so the startsWith('..') check alone lets a cross-drive escaping symlink through; also reject any result that is itself absolute (mirrors hooks_read.ts's relPathWithinRoot / pack.ts's isPathWithinRoot).
const resolved = fs.realpathSync(markerPath);
const rel = path.relative(path.resolve(current), path.resolve(resolved));
return !rel.startsWith('..') && !path.isAbsolute(rel);
} catch {
return false;
}
}
/** Walk up from cwd looking for a project marker. */
export function findProject(cwd: string): Project | null {
let p: string;
try {
p = canonicalize(cwd);
} catch {
return null;
}
let sysTemp: string | null = null;
try {
sysTemp = canonicalize(os.tmpdir());
} catch {
// ignore
}
let current = p;
while (true) {
// canonicalize() only lowercases the drive letter, not the rest of the path. `current` (from cwd) and `sysTemp` (from os.tmpdir(), which reads TEMP/TMP/TMPDIR) can legitimately differ in case beyond the drive letter, so fold both sides before comparing -- matching the platform-gated convention used elsewhere (isUnderBlockedRoot, assertWalkableRoot, pruneDeletedFiles) -- or this guard silently stops matching and the walk continues past the temp boundary.
if (
sysTemp &&
normalizeDarwinSystemAlias(foldPath(current)) ===
normalizeDarwinSystemAlias(foldPath(sysTemp))
) {
break;
}
for (const marker of PROJECT_MARKERS) {
if (markerExists(current, marker)) {
if (!isRepoContainer(current)) {
return {
root: current,
hash: projectHash(current),
marker,
};
}
break;
}
}
const parent = path.dirname(current);
if (parent === current) {
break;
}
current = parent;
}
return null;
}
let _displayRootCache: { cwd: string; root: string | undefined } | null = null;
/** Resolve the project root to use for shortening an indexed absolute path to a project-relative one for human-facing CLI output (see `toDisplayPath()` in `paths.ts`). Precedence: an explicitly-passed root wins outright (a caller that already resolved its own project root for querying, e.g. via `resolveProjectRoot()`); otherwise fall back to the cheap, subprocess-free `findProject()` marker walk from `process.cwd()`; otherwise `undefined`, which `toDisplayPath()` treats as "no project found" and returns the path unchanged (absolute). Deliberately never falls back to `process.cwd()` itself as a display root -- only to the *project root* that `findProject()` resolves by walking up from cwd. Using cwd directly would make output depend on which subdirectory the command was run from; the marker-walked project root is stable regardless of cwd's subdirectory. Memoized per `process.cwd()` for the explicit-root-omitted branch, matching `resolveConfigProjectRoot()`'s (config.ts) memoization rationale: this is a one-shot CLI process, cwd does not change within its lifetime, but a single command can call this helper in a loop over many result rows, so the marker walk should run once per process, not once per row. */
export function getDisplayRoot(explicitRoot?: string): string | undefined {
if (explicitRoot !== undefined) return explicitRoot;
const cwd = process.cwd();
if (_displayRootCache !== null && _displayRootCache.cwd === cwd) return _displayRootCache.root;
const project = findProject(cwd);
const root = project !== null ? project.root : undefined;
_displayRootCache = { cwd, root };
return root;
}
// Registered so batch_serve, which runs many requests in one process and calls clearModuleCaches() after each, does not hand a later request a root cached by an earlier one. The memoization note above is accurate for the CLI but not for that mode, which is neither one-shot nor fixed-cwd: keying on cwd covers a request that runs somewhere else, not a project root at a fixed path changing shape between two requests.
registerReset(() => {
_displayRootCache = null;
}, { perRequest: true });
/** True when `filePath` lives inside the OS system temp directory (`os.tmpdir()`), including any subdirectory of it -- scratch checkouts, ad hoc debugging copies, per-test fixture dirs, etc. Nothing under system temp should ever become a permanent index citizen: it is ephemeral by definition, often a byte-identical duplicate of a real project checked out elsewhere for throwaway work. Without this guard, editing a file inside such a copy (e.g. via an AI coding agent's own scratchpad) silently and permanently indexes it as a distinct "project" -- observed in practice on this repo's own global index, where half a dozen old scratch-dir copies of token-goat's own source produced 8+ duplicate results for every single `symbol`/`dead`/`refs` lookup, with no existing cleanup path. Used to gate future auto-indexing (hooks_edit.ts) and to retroactively purge already-polluted rows (`project prune`, cli_project_commands.ts). */
export function isUnderSystemTemp(filePath: string): boolean {
let sysTemp: string;
try {
sysTemp = canonicalize(os.tmpdir());
} catch {
return false;
}
let target: string;
try {
target = canonicalize(filePath);
} catch {
return false;
}
const foldedTemp = normalizeDarwinSystemAlias(foldPath(sysTemp));
const foldedTarget = normalizeDarwinSystemAlias(foldPath(target));
// canonicalize() always normalizes to forward slashes (both win32 and posix) -- comparing against path.sep (backslash on Windows) here would silently never match a real prefix.
return foldedTarget === foldedTemp || foldedTarget.startsWith(`${foldedTemp}/`);
}
// Bases already reported, so a caller that resolves the same directory many times in one run says it once. Cleared per request for the same reason _displayRootCache is.
const _reportedGitRootTimeouts = new Set<string>();
registerReset(() => {
_reportedGitRootTimeouts.clear();
}, { perRequest: true });
/** Says on stderr that git did not answer while resolving the root of `base`, so the marker-file fallback that follows is not mistaken for git's own answer: in a nested repository or a submodule the two roots differ. */
function noteGitRootTimeout(base: string): void {
if (_reportedGitRootTimeouts.has(base)) return;
_reportedGitRootTimeouts.add(base);
process.stderr.write(`token-goat: git did not answer while finding the project root of ${displaySafeText(base)}; using the nearest folder with a project marker instead, which can differ from git's top level.\n`);
}
/** Resolve "the current project root" using one shared precedence, replacing three previously-divergent conventions that had drifted apart across the codebase: - read_commands.ts's `runChanged` used `opts.projectRoot ?? process.cwd()`, then unconditionally tried to override that with `git rev-parse --show-toplevel`. - resume.ts used `findProject(process.cwd())?.root ?? process.cwd()`, with no git step at all. - cli_context_stats.ts used a bare `path.resolve(opts.project ?? process.cwd())`, with neither a git nor a findProject step. Resolution starts from a base directory -- `opts.project` if given (resolved to an absolute path), else `process.cwd()` -- and then, from that base: 1. `git rev-parse --show-toplevel`, if the base directory is inside a git repo. This matters because callers that pass changed-file paths through (e.g. `git diff --name-only`) always get them relative to the repo top-level, regardless of which subdirectory git was invoked from; resolving to the top-level keeps those relative paths correct even when the base directory is a subdirectory of the repo. 2. Else, {@link findProject}'s marker-based project root (package.json, .git, etc.), walking up from the base directory -- this covers non-git projects. 3. Else, the base directory itself. */
export function resolveProjectRoot(opts?: { project?: string }): string {
const base = opts?.project !== undefined ? path.resolve(opts.project) : process.cwd();
const toplevel = runGit(['rev-parse', '--show-toplevel'], { cwd: base });
if (toplevel.unfinished) noteGitRootTimeout(base);
if (toplevel.exitCode === 0) {
const trimmed = toplevel.stdout.trim();
// `git rev-parse` echoes back whatever drive-letter casing the OS reports (often uppercase on Windows), unlike every other project-root source in this module (findProject, makeProjectAt), which is always canonicalize()'d. Canonicalizing here too keeps the return value consistent regardless of which resolution step produced it.
if (trimmed.length > 0) return canonicalize(trimmed);
}
const project = findProject(base);
if (project !== null) return project.root;
return canonicalize(base);
}