Skip to content

Turbocharge Claude Code, Cursor, Codex, Gemini & every coding agent: faster, cheaper, with contextual understanding specific to your codebase.

feat: shouldSkipDir/walkDir gain an includes override, persisted via BuildConfig

Alex Matthews3455201785e7
diff --git a/src/graph/build.ts b/src/graph/build.ts
index 07eda91e..1957fa0b 100644
--- a/src/graph/build.ts
+++ b/src/graph/build.ts
@@ -21,6 +21,7 @@ import { extractFile, languageLabelOf, languageOf, type RawEdge } from "./extrac
import { contentHash } from "../util/id.js";
import { relPosix } from "../util/paths.js";
import { readSourceFile } from "../util/source.js";
+import { readIncludeDirs } from "../util/state.js";
import {
emptyExtractCache,
readExtractCache,
@@ -138,8 +139,9 @@ export async function buildGraph(
const root = resolve(dir);
const outDir = contextDirFor(root, opts.contextDir);
// Enumerate once: source extraction, scope discovery, and Go module
- // resolution must agree on the same Git-ignore-aware working-tree view.
- const repoFiles = walkDir(root);
+ // resolution must agree on the same Git-ignore-aware working-tree view —
+ // including the repo's persisted `--include-dir` override.
+ const repoFiles = walkDir(root, readIncludeDirs(root));
const files = listSourceStats(root, outDir, repoFiles);
const discoveredScopes = discoverScopes(root, repoFiles);
diff --git a/src/graph/scopes.ts b/src/graph/scopes.ts
index b4e658bd..407b6c30 100644
--- a/src/graph/scopes.ts
+++ b/src/graph/scopes.ts
@@ -25,6 +25,7 @@ import { existsSync, readdirSync, readFileSync, type Dirent } from "node:fs";
import { join, resolve } from "node:path";
import { shouldSkipDir, walkDir } from "../ingest/fs.js";
import { relPosix } from "../util/paths.js";
+import { readIncludeDirs } from "../util/state.js";
import type { GraphV1, ScopeV1 } from "./types.js";
/** Project-marker files, checked in this order (also the order `markers` is built in). */
@@ -127,8 +128,12 @@ interface Candidate {
isWorkspace: boolean;
}
-/** Walk the tree (reusing `walkDir`'s skip rules) and find project-marker dirs. */
-export function discoverScopes(root: string, repoFiles: string[] = walkDir(root)): ScopeV1[] {
+/** Walk the tree (reusing `walkDir`'s skip rules, including the repo's persisted
+ * `--include-dir` override) and find project-marker dirs. */
+export function discoverScopes(
+ root: string,
+ repoFiles: string[] = walkDir(root, readIncludeDirs(resolve(root))),
+): ScopeV1[] {
const absRoot = resolve(root);
const dirs = collectDirs(absRoot, repoFiles);
@@ -320,6 +325,7 @@ export function assertPrefixIndexed(graph: GraphV1, prefix: string): void {
* Used by workspace federation (Task 5). */
export function discoverWorkspaceChildren(root: string): string[] {
const absRoot = resolve(root);
+ const includes = readIncludeDirs(absRoot);
let entries: Dirent[];
try {
entries = readdirSync(absRoot, { withFileTypes: true });
@@ -327,7 +333,7 @@ export function discoverWorkspaceChildren(root: string): string[] {
return [];
}
return entries
- .filter((e) => e.isDirectory() && !shouldSkipDir(e.name) && existsSync(join(absRoot, e.name, ".git")))
+ .filter((e) => e.isDirectory() && !shouldSkipDir(e.name, includes) && existsSync(join(absRoot, e.name, ".git")))
.map((e) => e.name);
}
diff --git a/src/graph/source-files.ts b/src/graph/source-files.ts
index 7fd87200..1dcb903b 100644
--- a/src/graph/source-files.ts
+++ b/src/graph/source-files.ts
@@ -7,12 +7,25 @@
* {@link listSourceFiles} so its existing importers are unaffected.
*/
import { statSync } from "node:fs";
+import { resolve } from "node:path";
import { walkDir } from "../ingest/fs.js";
import { relPosix } from "../util/paths.js";
+import { readIncludeDirs } from "../util/state.js";
import { languageOf } from "./extract.js";
-/** The source files a graph build parses: supported languages, minus the output dir. */
-export function listSourceFiles(root: string, outDir: string, repoFiles: string[] = walkDir(root)): string[] {
+/**
+ * The source files a graph build parses: supported languages, minus the
+ * output dir. When no pre-enumerated `repoFiles` is passed, the walk reads
+ * `root`'s persisted `--include-dir` override (if any) directly from state —
+ * so every caller that enumerates through here (the fingerprint probe, the
+ * hooks/refresh path, none of which ever see a CLI flag) behaves identically
+ * to the `graft build --include-dir` invocation that set it.
+ */
+export function listSourceFiles(
+ root: string,
+ outDir: string,
+ repoFiles: string[] = walkDir(root, readIncludeDirs(resolve(root))),
+): string[] {
return repoFiles.filter((f) => !f.startsWith(outDir) && languageOf(f) !== null);
}
diff --git a/src/ingest/fs.ts b/src/ingest/fs.ts
index c9b7c444..3d072e2c 100644
--- a/src/ingest/fs.ts
+++ b/src/ingest/fs.ts
@@ -24,35 +24,48 @@ export const MAX_FILE_BYTES = 1_000_000;
/**
* Whether a directory named `name` should be skipped when walking a repo tree:
* any dot-prefixed directory (`.git`, `.github`, `.vscode`, ...) or one of
- * {@link SKIP_DIRS}. The single source of truth for "is this dir source" —
- * `skippedPath` and `walkFilesystem` below and the git-child discovery in
- * `graph/scopes.ts` share it, so they can never independently drift on what
- * counts as skippable.
+ * {@link SKIP_DIRS} not named in `includes`. The single source of truth for
+ * "is this dir source" — `skippedPath` and `walkFilesystem` below and the
+ * git-child discovery in `graph/scopes.ts` share it, so they can never
+ * independently drift on what counts as skippable.
*
- * KNOWN LIMITATION: a dot-directory is skipped WHOLESALE — there is no
- * override for a dot-prefixed directory. A repo that keeps real,
- * hand-written source under one is out of scope.
+ * `includes` is the explicit, per-repo `graft build --include-dir` override
+ * (persisted via `util/state.ts`'s `readIncludeDirs`, threaded in by each
+ * caller) — a name in it is removed from the effective skip set for THIS
+ * repo's walks. Absent/empty ≡ today's default behavior. It lifts only
+ * graft's own skip list: in a Git repo, Git's ignore rules stay authoritative
+ * (see {@link walkDir}).
+ *
+ * KNOWN LIMITATION: a dot-directory is skipped WHOLESALE and is NEVER
+ * overridable, even via `includes` — unlike `SKIP_DIRS`, there is no path to
+ * un-skip one. A repo that keeps real, hand-written source under a
+ * dot-prefixed directory is out of scope.
*/
-export function shouldSkipDir(name: string): boolean {
- return name.startsWith(".") || SKIP_DIRS.has(name);
+export function shouldSkipDir(name: string, includes?: ReadonlySet<string>): boolean {
+ if (name.startsWith(".")) return true;
+ if (includes?.has(name)) return false;
+ return SKIP_DIRS.has(name);
}
/**
* Recursively list all files under a directory. Skips dot-directories,
- * dependency/build directories (node_modules, dist, …), and files over 1 MB.
+ * dependency/build directories (node_modules, dist, …) not named in
+ * `includes`, and files over 1 MB.
* In a Git worktree, tracked files plus untracked, non-ignored files come from
* `git ls-files`; this gives indexing exactly Git's nested `.gitignore`,
* negation, and global-exclude semantics. Non-Git directories retain the plain
- * filesystem walk.
+ * filesystem walk. `includes` lifts only the built-in skip list — it never
+ * overrides Git's ignore rules (un-ignore or `git add -f` a directory to
+ * index it, the same contract as tracked-but-ignored files).
*/
-export function walkDir(dir: string): string[] {
- return gitVisibleFiles(dir) ?? walkFilesystem(dir);
+export function walkDir(dir: string, includes?: ReadonlySet<string>): string[] {
+ return gitVisibleFiles(dir, includes) ?? walkFilesystem(dir, includes);
}
/** Git's canonical working-tree file set, relative to `dir`. Tracked files are
* deliberately included even when a later ignore rule matches them; `.gitignore`
* only controls untracked files in Git, and graft follows the same contract. */
-function gitVisibleFiles(dir: string): string[] | null {
+function gitVisibleFiles(dir: string, includes?: ReadonlySet<string>): string[] | null {
const root = resolve(dir);
const result = spawnSync(
"git",
@@ -68,7 +81,7 @@ function gitVisibleFiles(dir: string): string[] | null {
const out: string[] = [];
for (const rel of result.stdout.split("\0")) {
- if (!rel || skippedPath(rel)) continue;
+ if (!rel || skippedPath(rel, includes)) continue;
const abs = resolve(root, rel);
try {
const stat = lstatSync(abs);
@@ -86,17 +99,17 @@ function gitVisibleFiles(dir: string): string[] | null {
/** A path (git-relative, either separator) is skipped when any of its
* segments is a skippable directory name — the final segment doubles as the
* dot-FILE check (`.eslintrc.js` and friends are not source either). */
-function skippedPath(path: string): boolean {
- return path.replace(/\\/g, "/").split("/").some(shouldSkipDir);
+function skippedPath(path: string, includes?: ReadonlySet<string>): boolean {
+ return path.replace(/\\/g, "/").split("/").some((segment) => shouldSkipDir(segment, includes));
}
-function walkFilesystem(dir: string): string[] {
+function walkFilesystem(dir: string, includes?: ReadonlySet<string>): string[] {
const out: string[] = [];
for (const entry of readdirSync(dir, { withFileTypes: true })) {
const full = join(dir, entry.name);
if (entry.isDirectory()) {
- if (shouldSkipDir(entry.name)) continue;
- out.push(...walkFilesystem(full));
+ if (shouldSkipDir(entry.name, includes)) continue;
+ out.push(...walkFilesystem(full, includes));
} else if (entry.isFile()) {
if (entry.name.startsWith(".")) continue; // dot-files are not source either
try {
diff --git a/src/util/state.ts b/src/util/state.ts
index 0f309d4c..da770e1d 100644
--- a/src/util/state.ts
+++ b/src/util/state.ts
@@ -62,6 +62,34 @@ export function writeJsonAtomic(p: string, value: unknown, compact = false): voi
export function readStats(d: string): Stats | null { return readJson<Stats>(statsPath(d)); }
export function writeStats(d: string, s: Stats): void { writeJsonAtomic(statsPath(d), s); }
+
+/**
+ * Persisted per-repo build configuration — currently just `graft build
+ * --include-dir`'s override. An explicit input like the graph itself: absent
+ * or empty `includeDirs` means today's default (every `SKIP_DIRS` name stays
+ * skipped), so a repo that never used the flag is entirely unaffected.
+ */
+export interface BuildConfig {
+ /** SKIP_DIRS names to include in this repo's walks, persisted so a LATER
+ * no-flag build — and the fingerprint/refresh path, which never sees CLI
+ * flags at all — behave identically to the invocation that set it. */
+ includeDirs?: string[];
+}
+
+function configPath(d: string): string { return join(cacheDir(d), 'config.json'); }
+
+export function readBuildConfig(d: string): BuildConfig | null { return readJson<BuildConfig>(configPath(d)); }
+export function writeBuildConfig(d: string, c: BuildConfig): void { writeJsonAtomic(configPath(d), c); }
+
+/** The persisted `--include-dir` override for repo `d`, as a Set — `undefined`
+ * when nothing was ever persisted (or the persisted list is empty), which every
+ * `shouldSkipDir`/`walkDir` caller treats as "today's default behavior". Shared
+ * by every walkDir-driven entry point (source-files.ts, scopes.ts) so a build,
+ * a later no-flag rebuild, and the hooks/refresh path all agree. */
+export function readIncludeDirs(d: string): Set<string> | undefined {
+ const dirs = readBuildConfig(d)?.includeDirs;
+ return dirs && dirs.length ? new Set(dirs) : undefined;
+}
// Best-effort read-modify-write; not atomic across concurrent processes, but acceptable
// for episodic hook writes (worst case is a lost update, not corruption).
export function patchStats(d: string, patch: Partial<Stats>): Stats {
diff --git a/test/ingest-fs.test.ts b/test/ingest-fs.test.ts
index 4596a41c..928e8c0b 100644
--- a/test/ingest-fs.test.ts
+++ b/test/ingest-fs.test.ts
@@ -148,7 +148,7 @@ test("A5: --include-dir does not override gitignore — an ignored directory sta
write(dir, "src/app.ts");
write(dir, "build/gen.ts");
- assert.deepEqual(walked(dir, new Set(["build"])), [".gitignore", "src/app.ts"]);
+ assert.deepEqual(walked(dir, new Set(["build"])), ["src/app.ts"]);
} finally {
rmSync(dir, { recursive: true, force: true });
}