diff --git a/apps/server/src/workspace/TaskFolders.test.ts b/apps/server/src/workspace/TaskFolders.test.ts index 37fd360cf8b24..11b3f265bf3de 100644 --- a/apps/server/src/workspace/TaskFolders.test.ts +++ b/apps/server/src/workspace/TaskFolders.test.ts @@ -15,6 +15,7 @@ import * as ServerConfig from "../config.ts"; import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; import * as TaskFolders from "./TaskFolders.ts"; import * as TaskFolderStore from "./TaskFolderStore.ts"; +import * as WorkspaceFolders from "./WorkspaceFolders.ts"; import { layerTaskFoldersWithGit, taskFoldersTestProjectId as projectId, @@ -347,6 +348,51 @@ it.live("puts a clone the project ignores inside the project's worktree", () => ), ); +it.live("starts a thread when a checkout hook links a gitignored vendored repository", () => + withState((state) => + Effect.gen(function* () { + const { fileSystem, path, run, repository, isLink, worktreeCount } = yield* fixture; + const project = yield* repository("app"); + const api = yield* repository("app/services/api"); + // A setup script clones these; the project itself does not version them. + const vendored = yield* repository("app/vendor/lib"); + yield* fileSystem.writeFileString(path.join(project, ".gitignore"), "vendor/\n"); + yield* run(project, "add", ".gitignore"); + yield* run(project, "commit", "-m", "ignore the vendored checkout"); + // Like a project whose hook links its vendored checkouts into every new worktree. + const hooks = path.join(project, "hooks"); + yield* fileSystem.makeDirectory(hooks); + yield* fileSystem.writeFileString( + path.join(hooks, "post-checkout"), + `#!/bin/sh +if [ "$(git rev-parse --git-dir)" != "$(git rev-parse --git-common-dir)" ]; then + mkdir -p vendor + ln -s "${vendored}" vendor/lib +fi +`, + ); + yield* fileSystem.chmod(path.join(hooks, "post-checkout"), 0o755); + yield* run(project, "config", "core.hooksPath", hooks); + + state.workspaceRoot = project; + state.trackedFolders = yield* WorkspaceFolders.discoverFolders(project); + + // Only the repository the project versions is laid out; the hook links the vendored one. + assert.deepEqual( + state.trackedFolders.map((folder) => folder.path), + [api], + ); + const { taskFolder } = yield* create("feature"); + assert.deepEqual( + taskFolder.repositories.map(({ repositoryPath }) => repositoryPath), + [api], + ); + assert.isTrue(yield* isLink(path.join(taskFolder.root, "vendor", "lib"))); + assert.equal(yield* worktreeCount(vendored), 1); + }), + ), +); + it.live("treats a project folder inside a larger repository as plain", () => withState((state) => Effect.gen(function* () { diff --git a/apps/server/src/workspace/WorkspaceFolders.test.ts b/apps/server/src/workspace/WorkspaceFolders.test.ts index 61dac26a8c044..2907043e05346 100644 --- a/apps/server/src/workspace/WorkspaceFolders.test.ts +++ b/apps/server/src/workspace/WorkspaceFolders.test.ts @@ -193,6 +193,38 @@ it.layer(NodeServices.layer)("WorkspaceFolders", (it) => { }), ); + it.effect("leaves out auto-discovered repositories the project's git ignores", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const tmp = yield* makeTempDir; + const workspaceRoot = path.join(tmp, "wrapper"); + for (const folder of [".git", "api/.git", "vendor/big/.git", "vendor/nested/deep/.git"]) { + yield* fileSystem.makeDirectory(path.join(workspaceRoot, folder), { recursive: true }); + } + yield* fileSystem.writeFileString(path.join(workspaceRoot, ".gitignore"), "vendor/\n"); + const explicitlyAdded = { path: path.join(workspaceRoot, "vendor", "big"), name: "big" }; + const listed = Effect.gen(function* () { + const workspaceFolders = yield* WorkspaceFolders.WorkspaceFolders; + return yield* Effect.map(workspaceFolders.list({ projectId }), ({ folders }) => + folders.map((folder) => [folder.name, folder.path]), + ); + }); + + yield* Effect.gen(function* () { + assert.deepEqual(yield* listed, [["api", path.join(workspaceRoot, "api")]]); + }).pipe(Effect.provide(layerFor({ workspaceRoot, trackedFolders: [] }))); + + // Someone who adds an ignored checkout still gets it. + yield* Effect.gen(function* () { + assert.deepEqual(yield* listed, [ + ["api", path.join(workspaceRoot, "api")], + ["big", explicitlyAdded.path], + ]); + }).pipe(Effect.provide(layerFor({ workspaceRoot, trackedFolders: [explicitlyAdded] }))); + }), + ); + it.effect("keeps the folders someone added when a project holds more than fit", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; diff --git a/apps/server/src/workspace/WorkspaceFolders.ts b/apps/server/src/workspace/WorkspaceFolders.ts index 776f90ea6e5b0..c2a74bf598887 100644 --- a/apps/server/src/workspace/WorkspaceFolders.ts +++ b/apps/server/src/workspace/WorkspaceFolders.ts @@ -24,6 +24,7 @@ import * as Path from "effect/Path"; import * as Schema from "effect/Schema"; import * as ProjectStore from "../orchestration-v2/ProjectStore.ts"; +import { type IgnoreDecision, parseIgnoreFile } from "./IgnorePatterns.ts"; import { isWithinRoot } from "./KnownWorkspaces.ts"; import * as TaskFolderStore from "./TaskFolderStore.ts"; @@ -288,12 +289,38 @@ export const discoverFolders = Effect.fn("WorkspaceFolders.discover")(function* .pipe(Effect.orElseSucceed(() => "")), )?.ignoreRepositories ?? [] ).map((entry) => path.resolve(root, entry)); + // Reading the root file is cheaper than `git check-ignore` on every project read. Folders a + // workspace file lists were chosen on purpose, so only auto-discovered ones defer to git. + const gitignore = scan.value.isGitRepository + ? parseIgnoreFile( + yield* fileSystem + .readFileString(path.join(root, ".gitignore")) + .pipe(Effect.orElseSucceed(() => "")), + ) + : null; + const gitIgnored = (folderPath: string) => { + if (gitignore === null) return false; + const relative = path.relative(root, folderPath).split(path.sep).join("/"); + if (relative === "" || relative === ".." || relative.startsWith("../")) return false; + const segments = relative.split("/"); + // A `vendor/` pattern matches the folder, not `vendor/pkg`, so each parent decides too. Git + // cannot re-include anything below an excluded folder, so one ignored parent settles it. + for (let index = 1; index <= segments.length; index += 1) { + const decision: IgnoreDecision | undefined = gitignore.decide( + segments.slice(0, index).join("/"), + true, + ); + if (decision === "ignored") return true; + } + return false; + }; const inside = yield* Effect.filter( - scan.value.folders.filter( - (folder) => - folder.isGitRepository && - !ignored.some((entry) => folder.path === entry || isWithinRoot(path, folder.path, entry)), - ), + scan.value.folders.filter((folder) => { + if (!folder.isGitRepository) return false; + if (ignored.some((entry) => folder.path === entry || isWithinRoot(path, folder.path, entry))) + return false; + return folder.source === "code-workspace" || !gitIgnored(folder.path); + }), // A link to a repository elsewhere is not inside the project, whatever its name says, and a // worktree a workspace file lists follows another repository. (folder) =>