Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apps/server/src/cli/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -315,6 +316,7 @@ const runProjectMutation = Effect.fn("runProjectMutation")(function* (
| HttpClient.HttpClient
| Path.Path
| WorkspacePaths.WorkspacePaths
| ChildProcessSpawner.ChildProcessSpawner
>,
) {
const logLevel = yield* GlobalFlag.LogLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
7 changes: 5 additions & 2 deletions apps/server/src/orchestration-v2/ThreadLaunchService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 4 additions & 0 deletions apps/server/src/project/ProjectService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = (
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/storageCleanup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -686,6 +687,7 @@ const runCleanupTest = <A, E>(
| GitVcsDriver.GitVcsDriver
| Path.Path
| Scope.Scope
| ChildProcessSpawner.ChildProcessSpawner
>,
) => effect.pipe(Effect.provide(cleanupTestLayer));

Expand Down
35 changes: 35 additions & 0 deletions apps/server/src/workspace/TaskFolders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>; 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 });
}),
),
);
47 changes: 32 additions & 15 deletions apps/server/src/workspace/TaskFolders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<void>;
/** When a repository starts and when it finishes; `running` names the ones in flight. */
readonly onRepository?: (progress: {
readonly name: string;
readonly running: ReadonlyArray<string>;
readonly done: number;
readonly total: number;
}) => Effect.Effect<void>;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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) =>
Expand All @@ -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`)),
Expand Down
39 changes: 39 additions & 0 deletions apps/server/src/workspace/WorkspaceFolders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
59 changes: 56 additions & 3 deletions apps/server/src/workspace/WorkspaceFolders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -274,7 +278,11 @@ const scanFolders = Effect.fn("WorkspaceFolders.scan")(function* (
*/
export const discoverFolders = Effect.fn("WorkspaceFolders.discover")(function* (
workspaceRoot: string,
): Effect.fn.Return<ReadonlyArray<ProjectTrackedFolder>, never, FileSystem.FileSystem | Path.Path> {
): Effect.fn.Return<
ReadonlyArray<ProjectTrackedFolder>,
never,
FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner
> {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const { realPath, ownsHistory } = yield* folderProbes;
Expand Down Expand Up @@ -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("/")),
);
Comment thread
yordis marked this conversation as resolved.
Resolved
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<string>,
) {
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<string>();
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 !== ""));
});

/**
Expand Down Expand Up @@ -336,7 +387,7 @@ export const projectFolders = Effect.fn("WorkspaceFolders.projectFolders")(funct
}): Effect.fn.Return<
ReadonlyArray<ProjectTrackedFolder>,
never,
FileSystem.FileSystem | Path.Path
FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner
> {
return mergeProjectFolders(yield* discoverFolders(project.workspaceRoot), project.trackedFolders);
});
Expand All @@ -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;

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion docs/user/multi-repo-projects.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Loading