diff --git a/apps/server/src/cli/project.ts b/apps/server/src/cli/project.ts index 1fa7cc0f3f264..dcf22b8699925 100644 --- a/apps/server/src/cli/project.ts +++ b/apps/server/src/cli/project.ts @@ -16,6 +16,7 @@ import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; import * as References from "effect/References"; import * as Schema from "effect/Schema"; import { Argument, Command, Flag, GlobalFlag } from "effect/cli"; @@ -315,6 +316,7 @@ const runProjectMutation = Effect.fn("runProjectMutation")(function* ( | HttpClient.HttpClient | Path.Path | WorkspacePaths.WorkspacePaths + | ChildProcessSpawner.ChildProcessSpawner >, ) { const logLevel = yield* GlobalFlag.LogLevel; diff --git a/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts b/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts index 9bc617e48823b..0229f1651c677 100644 --- a/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts +++ b/apps/server/src/orchestration-v2/ThreadLaunchService.test.ts @@ -3081,7 +3081,7 @@ it.live("launches into a task folder and leaves nothing behind when its setup is ["agent", "pending"], ], ); - assert.match(stages[1]?.detail ?? "", /\(2 of 2\)$/); + assert.equal(stages[1]?.detail, "2 of 2 repositories"); assert.isTrue(yield* tracker.cancel(launched.threadId)); diff --git a/apps/server/src/orchestration-v2/ThreadLaunchService.ts b/apps/server/src/orchestration-v2/ThreadLaunchService.ts index 9262695987cbf..65eef21fb9e43 100644 --- a/apps/server/src/orchestration-v2/ThreadLaunchService.ts +++ b/apps/server/src/orchestration-v2/ThreadLaunchService.ts @@ -860,9 +860,12 @@ const make = Effect.gen(function* () { createdWorktreePath = taskFolder.projectPath; createdBranch = claimedBranch; }), - onRepository: ({ name, done, total }) => + onRepository: ({ running, done, total }) => setupTracker.stage(threadId, "checkout", { - detail: `${name} (${done} of ${total})`, + detail: + running.length === 0 + ? `${done} of ${total} repositories` + : `${running.join(", ")} (${done} of ${total} done)`, }), }) .pipe( diff --git a/apps/server/src/project/ProjectService.ts b/apps/server/src/project/ProjectService.ts index 80fabe6101c05..b6b03a5468d7a 100644 --- a/apps/server/src/project/ProjectService.ts +++ b/apps/server/src/project/ProjectService.ts @@ -22,6 +22,7 @@ import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; import * as Result from "effect/Result"; import * as Schema from "effect/Schema"; @@ -204,6 +205,7 @@ export const make = Effect.gen(function* () { const serverSettings = yield* ServerSettings.ServerSettingsService; const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; const isManagedWorktreePath = yield* makeIsManagedWorktreePath; const isWorktreeContainer = yield* makeIsWorktreeContainer.pipe( Effect.provide(ProcessRunner.layer), @@ -246,6 +248,7 @@ export const make = Effect.gen(function* () { const discovered = yield* discoverFolders(row.workspaceRoot).pipe( Effect.provideService(FileSystem.FileSystem, fileSystem), Effect.provideService(Path.Path, path), + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), ); if (sameProjectFolders(mergeProjectFolders(discovered, row.trackedFolders), row.trackedFolders)) return; @@ -323,6 +326,7 @@ export const make = Effect.gen(function* () { projectFolders({ workspaceRoot, trackedFolders: added }).pipe( Effect.provideService(FileSystem.FileSystem, fileSystem), Effect.provideService(Path.Path, path), + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), ); const normalizeTrackedFolders = ( diff --git a/apps/server/src/storageCleanup.test.ts b/apps/server/src/storageCleanup.test.ts index 3b9d2b7bb6971..dda62cdc67e92 100644 --- a/apps/server/src/storageCleanup.test.ts +++ b/apps/server/src/storageCleanup.test.ts @@ -13,6 +13,7 @@ import * as Option from "effect/Option"; import * as Queue from "effect/Queue"; import type * as Path from "effect/Path"; import type * as Scope from "effect/Scope"; +import type * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; import * as Stream from "effect/Stream"; import { SCAN_CACHE_FILE_NAME } from "./usage/usageScanCache.ts"; import { TestClock } from "effect/testing"; @@ -686,6 +687,7 @@ const runCleanupTest = ( | GitVcsDriver.GitVcsDriver | Path.Path | Scope.Scope + | ChildProcessSpawner.ChildProcessSpawner >, ) => effect.pipe(Effect.provide(cleanupTestLayer)); diff --git a/apps/server/src/workspace/TaskFolders.test.ts b/apps/server/src/workspace/TaskFolders.test.ts index 2be3e39c09817..ba042ab64080f 100644 --- a/apps/server/src/workspace/TaskFolders.test.ts +++ b/apps/server/src/workspace/TaskFolders.test.ts @@ -945,3 +945,38 @@ it.live("restores a recorded repository the project stopped tracking as its work }), ), ); + +// A slow fetch must be named while it runs, not only once it is done. +it.live("reports which repositories are still being checked out", () => + withState((state) => + Effect.gen(function* () { + const { path, tmp, repository } = yield* fixture; + const shop = path.join(tmp, "shop"); + const api = yield* repository("shop/api"); + const web = yield* repository("shop/web"); + state.workspaceRoot = shop; + state.trackedFolders = tracked(api, web); + const taskFolders = yield* TaskFolders.TaskFolders; + const plan = yield* taskFolders.plan({ + projectId, + repositories: [{ path: api }, { path: web }], + }); + const reports: Array<{ running: ReadonlyArray; done: number; total: number }> = []; + + yield* taskFolders.create({ + plan: plan!, + branch: "feature", + projectStart: { refName: "main", baseRefName: "main" }, + startFromOrigin: false, + onRepository: (report) => Effect.sync(() => reports.push(report)), + }); + + for (const name of ["api", "web"]) { + const started = reports.findIndex((report) => report.running.includes(name)); + assert.isAtLeast(started, 0); + assert.isBelow(reports[started]!.done, 2); + } + assert.deepEqual(reports.at(-1), { running: [], done: 2, total: 2 }); + }), + ), +); diff --git a/apps/server/src/workspace/TaskFolders.ts b/apps/server/src/workspace/TaskFolders.ts index 75c12d8a7409b..5cfd1287f89e0 100644 --- a/apps/server/src/workspace/TaskFolders.ts +++ b/apps/server/src/workspace/TaskFolders.ts @@ -27,6 +27,7 @@ import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; import type * as PlatformError from "effect/PlatformError"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; import * as Schema from "effect/Schema"; import * as ServerConfig from "../config.ts"; @@ -106,8 +107,9 @@ export class TaskFolders extends Context.Service< readonly projectProgress?: GitVcsDriver.CreateWorktreeProgress; /** As soon as the folder is recorded, so a cancelled caller knows what to remove. */ readonly onClaimed?: (taskFolder: TaskFolder, branch: string) => Effect.Effect; + /** When a repository starts and when it finishes; `running` names the ones in flight. */ readonly onRepository?: (progress: { - readonly name: string; + readonly running: ReadonlyArray; readonly done: number; readonly total: number; }) => Effect.Effect; @@ -231,6 +233,7 @@ const isGitCommandError = Schema.is(GitCommandError); const make = Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; const digest = yield* Digest.Digest; const config = yield* ServerConfig.ServerConfig; const settings = yield* ServerSettings.ServerSettingsService; @@ -264,6 +267,7 @@ const make = Effect.gen(function* () { projectFolders(project).pipe( Effect.provideService(FileSystem.FileSystem, fileSystem), Effect.provideService(Path.Path, path), + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), ); // The repository that pins a submodule owns its checkout: Git, a hook or a setup script puts it // in that repository's worktree, so it never gets one of its own here. @@ -1050,6 +1054,13 @@ const make = Effect.gen(function* () { yield* linkFolder(taskPlan.projectRoot, empty.projectPath, taskPlan.trackedRepositories); } let finished = 0; + const running: string[] = []; + const reportRepositories = () => + input.onRepository?.({ + running: [...running], + done: finished, + total: taskPlan.repositories.length, + }) ?? Effect.void; // Whatever the project put at a skipped path also stands for the repositories inside it. const skipped: string[] = []; // A repository inside another waits for the one that holds it. @@ -1069,13 +1080,25 @@ const make = Effect.gen(function* () { (repository) => (skipped.some((outer) => isWithinRoot(path, repository.path, outer)) ? Effect.succeed(null) - : createRepositoryWorktree( - empty, - repository, - branch, - input.startFromOrigin, - input.baseBranch, - recordBranch(repository.path), + : Effect.suspend(() => { + running.push(repository.name); + return reportRepositories(); + }).pipe( + Effect.andThen( + createRepositoryWorktree( + empty, + repository, + branch, + input.startFromOrigin, + input.baseBranch, + recordBranch(repository.path), + ), + ), + Effect.ensuring( + Effect.sync(() => { + running.splice(running.indexOf(repository.name), 1); + }), + ), ) ).pipe( Effect.tap((worktree) => @@ -1086,13 +1109,7 @@ const make = Effect.gen(function* () { created.push(worktree.repository); repositoriesStartedFromOrigin ||= worktree.fromOrigin; } - return ( - input.onRepository?.({ - name: repository.name, - done: finished, - total: taskPlan.repositories.length, - }) ?? Effect.void - ); + return reportRepositories(); }), ), Effect.mapError(fail("create", `Could not create the ${repository.name} worktree`)), diff --git a/apps/server/src/workspace/WorkspaceFolders.test.ts b/apps/server/src/workspace/WorkspaceFolders.test.ts index 3a02d202be891..18a33f3e4dd72 100644 --- a/apps/server/src/workspace/WorkspaceFolders.test.ts +++ b/apps/server/src/workspace/WorkspaceFolders.test.ts @@ -193,6 +193,45 @@ it.layer(NodeServices.layer)("WorkspaceFolders", (it) => { }), ); + it.effect("leaves out repositories in folders the project's Git ignores", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const workspaceRoot = yield* makeTempDir; + for (const folder of [ + "tmp/scratch/.git", + 'tmp/we"ird/.git', + "api/.git", + "packages/web/.git", + ]) { + yield* fileSystem.makeDirectory(path.join(workspaceRoot, folder), { recursive: true }); + } + const names = Effect.map(WorkspaceFolders.discoverFolders(workspaceRoot), (folders) => + folders.map((folder) => folder.name).toSorted(), + ); + // Outside a repository nothing is ignored. + assert.deepEqual(yield* names, ["api", "scratch", 'we"ird', "web"]); + + yield* fileSystem.makeDirectory(path.join(workspaceRoot, ".git")); + yield* fileSystem.writeFileString( + path.join(workspaceRoot, ".git", "HEAD"), + "ref: refs/heads/main\n", + ); + yield* fileSystem.makeDirectory(path.join(workspaceRoot, ".git", "objects")); + yield* fileSystem.makeDirectory(path.join(workspaceRoot, ".git", "refs")); + yield* fileSystem.writeFileString(path.join(workspaceRoot, ".gitignore"), "tmp\n"); + + assert.deepEqual(yield* names, ["api", "web"]); + + // A plain project folder inside that repository keeps its own repositories. + const nested = path.join(workspaceRoot, "tmp"); + const nestedNames = Effect.map(WorkspaceFolders.discoverFolders(nested), (folders) => + folders.map((folder) => folder.name).toSorted(), + ); + assert.deepEqual(yield* nestedNames, ["scratch", 'we"ird']); + }), + ); + it.effect("does not walk a project's folders when t3.json ignores the whole project", () => 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 0824c647ce360..60b7f133ce215 100644 --- a/apps/server/src/workspace/WorkspaceFolders.ts +++ b/apps/server/src/workspace/WorkspaceFolders.ts @@ -21,9 +21,13 @@ import * as FileSystem from "effect/FileSystem"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Path from "effect/Path"; +import * as ChildProcess from "effect/process/ChildProcess"; +import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; import * as Schema from "effect/Schema"; +import * as Stream from "effect/Stream"; import * as ProjectStore from "../orchestration-v2/ProjectStore.ts"; +import * as VcsProcess from "../vcs/VcsProcess.ts"; import { isWithinRoot } from "./KnownWorkspaces.ts"; import * as TaskFolderStore from "./TaskFolderStore.ts"; @@ -274,7 +278,11 @@ const scanFolders = Effect.fn("WorkspaceFolders.scan")(function* ( */ export const discoverFolders = Effect.fn("WorkspaceFolders.discover")(function* ( workspaceRoot: string, -): Effect.fn.Return, never, FileSystem.FileSystem | Path.Path> { +): Effect.fn.Return< + ReadonlyArray, + never, + FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner +> { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; const { realPath, ownsHistory } = yield* folderProbes; @@ -308,7 +316,50 @@ export const discoverFolders = Effect.fn("WorkspaceFolders.discover")(function* ), { concurrency: DETECTION_CONCURRENCY }, ); - return inside.map((folder) => ({ path: folder.path, name: folder.name, discovered: true })); + const gitIgnored = yield* gitIgnoredFolders( + root, + inside.map((folder) => path.relative(root, folder.path).split(path.sep).join("/")), + ); + return inside + .filter((folder) => !gitIgnored.has(path.relative(root, folder.path).split(path.sep).join("/"))) + .map((folder) => ({ path: folder.path, name: folder.name, discovered: true })); +}); + +// Clones under a folder the project's Git ignores, such as `tmp/`, are scratch, not part of it. +const gitIgnoredFolders = Effect.fn("WorkspaceFolders.gitIgnoredFolders")(function* ( + root: string, + relativePaths: ReadonlyArray, +) { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + // Without its own `.git`, Git would apply the rules of a repository the project sits inside. + const isRepository = yield* fileSystem + .exists(path.join(root, ".git")) + .pipe(Effect.orElseSucceed(() => false)); + if (relativePaths.length === 0 || !isRepository) return new Set(); + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; + const environment = yield* HostProcess.Environment; + const platform = yield* HostProcess.Platform; + // Exit 1 means nothing matched; any failure leaves every repository in. + const output = yield* spawner + .string( + ChildProcess.make("git", ["-c", "core.fsmonitor=false", "check-ignore", "--stdin", "-z"], { + cwd: root, + env: { + ...environment, + ...VcsProcess.unsetInheritedRepositoryEnv(environment, platform), + }, + stdin: Stream.make( + new TextEncoder().encode(relativePaths.map((entry) => `${entry}\0`).join("")), + ), + stderr: "ignore", + }), + ) + .pipe( + Effect.timeout("10 seconds"), + Effect.orElseSucceed(() => ""), + ); + return new Set(output.split("\0").filter((entry) => entry !== "")); }); /** @@ -336,7 +387,7 @@ export const projectFolders = Effect.fn("WorkspaceFolders.projectFolders")(funct }): Effect.fn.Return< ReadonlyArray, never, - FileSystem.FileSystem | Path.Path + FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner > { return mergeProjectFolders(yield* discoverFolders(project.workspaceRoot), project.trackedFolders); }); @@ -359,6 +410,7 @@ export const sameProjectFolders = ( const make = Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; + const spawner = yield* ChildProcessSpawner.ChildProcessSpawner; const projects = yield* ProjectStore.ProjectStoreV2; const taskFolders = yield* TaskFolderStore.TaskFolderStore; @@ -390,6 +442,7 @@ const make = Effect.gen(function* () { projectFolders(project).pipe( Effect.provideService(FileSystem.FileSystem, fileSystem), Effect.provideService(Path.Path, path), + Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner), ); // `identity` is `root` in the checkout the thread's was made from: it names the same submodule diff --git a/docs/user/multi-repo-projects.md b/docs/user/multi-repo-projects.md index 7962770417175..1500d19f6e9e0 100644 --- a/docs/user/multi-repo-projects.md +++ b/docs/user/multi-repo-projects.md @@ -15,7 +15,8 @@ There is no list to keep. A project covers every repository inside its folder: T level down, and one level further inside folders that are not repositories themselves, such as `src` or `packages`. It reads the disk each time, so a repository you clone there is part of the project straight away, and one you delete is gone from it. Worktrees of another repository are left -out. +out, and so are repositories the project's Git ignores, such as scratch clones in an ignored `tmp` +folder. **Settings → Projects → Repositories** shows what the project covers on each machine.