diff --git a/apps/server/src/workspace/WorkspaceEntries.test.ts b/apps/server/src/workspace/WorkspaceEntries.test.ts index d861935fe17cc..0acb5b62d6692 100644 --- a/apps/server/src/workspace/WorkspaceEntries.test.ts +++ b/apps/server/src/workspace/WorkspaceEntries.test.ts @@ -108,6 +108,14 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => { yield* writeTextFile(cwd, "node_modules/pkg/index.js"); yield* writeTextFile(cwd, "src/index.ts"); yield* fileSystem.makeDirectory(path.join(cwd, "empty")); + yield* git(cwd, ["init", "sub"]); + yield* writeTextFile(cwd, "sub/.gitignore", "dist/\n"); + yield* writeTextFile(cwd, "sub/dist/out.js"); + const gitlink = yield* git(cwd, ["hash-object", "tracked.txt"]); + yield* git(cwd, ["update-index", "--add", "--cacheinfo", `160000,${gitlink},sub`]); + // A nested clone that isn't a submodule keeps the outer repository's ignore rules. + yield* git(cwd, ["init", "node_modules/pkg"]); + yield* git(cwd, ["init", "sub/dist"]); const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries; const root = yield* workspaceEntries.list({ cwd, directoryPath: "" }); @@ -127,6 +135,17 @@ it.layer(layerTest, { excludeTestServices: true })("WorkspaceEntries", (it) => { entries: [{ path: "node_modules/pkg/index.js", kind: "file", ignored: true }], truncated: false, }); + // Git refuses pathspecs inside a submodule from the outer repository. + expect((yield* workspaceEntries.list({ cwd, directoryPath: "sub" })).entries).toEqual( + expect.arrayContaining([ + { path: "sub/.gitignore", kind: "file" }, + { path: "sub/dist", kind: "directory", ignored: true }, + ]), + ); + expect(yield* workspaceEntries.list({ cwd, directoryPath: "sub/dist" })).toEqual({ + entries: [{ path: "sub/dist/out.js", kind: "file", ignored: true }], + truncated: false, + }); expect(yield* workspaceEntries.list({ cwd, directoryPath: "empty" })).toEqual({ entries: [], truncated: false, diff --git a/apps/server/src/workspace/WorkspaceEntries.ts b/apps/server/src/workspace/WorkspaceEntries.ts index 0b853ccdaa437..d388170f93f5e 100644 --- a/apps/server/src/workspace/WorkspaceEntries.ts +++ b/apps/server/src/workspace/WorkspaceEntries.ts @@ -306,7 +306,7 @@ export const make = Effect.gen(function* () { relativePath: directoryPath, }) .pipe(Effect.mapError(toError)); - const { entries, throughSymlink } = yield* Effect.tryPromise({ + const { entries, throughSymlink, nestedGitRoots } = yield* Effect.tryPromise({ try: async () => { const root = await NodeFSP.realpath(normalizedCwd); const directory = await NodeFSP.realpath(target.absolutePath); @@ -378,7 +378,17 @@ export const make = Effect.gen(function* () { )), ); } + const nestedGitRoots: Array = []; + const segments = target.relativePath.split("/").filter(Boolean); + for (let index = 1; index <= segments.length; index += 1) { + const candidate = segments.slice(0, index).join("/"); + const dotGit = await NodeFSP.lstat(path.join(normalizedCwd, candidate, ".git")).catch( + () => null, + ); + if (dotGit) nestedGitRoots.push(candidate); + } return { + nestedGitRoots, entries: children.flatMap((child, index): ProjectEntry[] => { const kind = kinds[index]; if (kind === undefined) return []; @@ -395,6 +405,8 @@ export const make = Effect.gen(function* () { }, catch: toError, }); + let gitRoot = ""; + let gitPrefix = ""; // stdin avoids argument limits; ignore classification is optional without git/repositories. const checkIgnore = (paths: ReadonlyArray) => vcsProcess @@ -402,20 +414,27 @@ export const make = Effect.gen(function* () { operation: "WorkspaceEntries.list", command: "git", args: ["-c", "core.fsmonitor=false", "check-ignore", "-z", "--stdin"], - cwd: normalizedCwd, - stdin: `${paths.join("\0")}\0`, + cwd: path.join(normalizedCwd, gitRoot), + stdin: `${paths.map((entryPath) => entryPath.slice(gitPrefix.length)).join("\0")}\0`, allowNonZeroExit: true, timeoutMs: 10_000, maxOutputBytes: 16 * 1024 * 1024, }) .pipe(Effect.orElseSucceed(() => undefined)); + // Git refuses paths inside a submodule (128) but still answers for nested clones. + for (const nestedGitRoot of nestedGitRoots) { + if ((yield* checkIgnore([`${nestedGitRoot}/.git`]))?.exitCode !== 128) continue; + gitRoot = nestedGitRoot; + gitPrefix = `${gitRoot}/`; + } + const directoryGitPath = target.relativePath.slice(gitPrefix.length); // Check ignored folders once to avoid child index lookups; exit 0 guarantees no tracked descendants. // Symlinks and pathspec magic still need per-child checks. if ( entries.length > IGNORE_CHECK_CHUNK_SIZE && - target.relativePath !== "" && + directoryGitPath !== "" && !throughSymlink && - !/^:|[*?[\\]/.test(target.relativePath) && + !/^:|[*?[\\]/.test(directoryGitPath) && (yield* checkIgnore([target.relativePath]))?.exitCode === 0 ) { return { @@ -429,7 +448,7 @@ export const make = Effect.gen(function* () { entries.slice(offset, offset + IGNORE_CHECK_CHUNK_SIZE).map((entry) => entry.path), ); if (!result || (result.exitCode !== 0 && result.exitCode !== 1)) break; - for (const ignoredPath of result.stdout.split("\0")) ignored.add(ignoredPath); + for (const ignoredPath of result.stdout.split("\0")) ignored.add(gitPrefix + ignoredPath); } return { entries: entries.map((entry) =>