TypeScript
fix(server): threads without a project no longer need Git installed (#17959)
Theo Brownec73b45480fb7
diff --git a/apps/server/src/project/ManagedProjectFolders.test.ts b/apps/server/src/project/ManagedProjectFolders.test.tsindex cd490079f2c..393c858a693 100644--- a/apps/server/src/project/ManagedProjectFolders.test.ts+++ b/apps/server/src/project/ManagedProjectFolders.test.ts@@ -1,6 +1,12 @@import { assert, it } from "@effect/vitest";import * as NodeServices from "@effect/platform-node/NodeServices";-import { CommandId, GitCommandError, ProjectId, ThreadId } from "@t3tools/contracts";+import {+ CommandId,+ GitCommandError,+ ProjectId,+ ThreadId,+ VcsProcessSpawnError,+} from "@t3tools/contracts";import * as Deferred from "effect/Deferred";import * as Effect from "effect/Effect";import * as Fiber from "effect/Fiber";@@ -8,9 +14,11 @@ 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 PlatformError from "effect/PlatformError";import * as Scope from "effect/Scope";import * as ServerConfig from "../config.ts";+import * as GitManager from "../git/GitManager.ts";import * as GitWorkflow from "../git/GitWorkflowService.ts";import * as RuntimeLayer from "../orchestration-v2/runtimeLayer.ts";import * as SqlitePersistence from "../persistence/Sqlite.ts";@@ -25,20 +33,13 @@ import * as ProjectService from "./ProjectService.ts";import * as RepositoryIdentityResolver from "./RepositoryIdentityResolver.ts";import * as ManagedProjectFolders from "./ManagedProjectFolders.ts";-// Real repository detection: the service only asks the Git workflow whether-// the data dir is inside a checkout.-const layerGitWorkflow = Layer.unwrap(- Effect.gen(function* () {- const registry = yield* VcsDriverRegistry.VcsDriverRegistry;- return Layer.mock(GitWorkflow.GitWorkflowService)({- isRepository: (cwd) =>- registry.detect({ cwd }).pipe(- Effect.map((handle) => handle?.kind === "git"),- Effect.orDie,- ),- });- }),-).pipe(Layer.provide(VcsDriverRegistry.layer.pipe(Layer.provide(VcsProcess.layer))));+// The real Git workflow over real repository detection, with a mock GitManager+// since the service only asks whether the data dir is inside a checkout.+const layerGitWorkflow = (vcsProcess?: Layer.Layer<VcsProcess.VcsProcess>) =>+ GitWorkflow.layer.pipe(+ Layer.provide(VcsDriverRegistry.layer.pipe(Layer.provide(vcsProcess ?? VcsProcess.layer))),+ Layer.provide(Layer.mock(GitManager.GitManager)({})),+ );const layerEnrichment = ProjectEnrichmentService.layer.pipe(Layer.provide(@@ -56,6 +57,8 @@ const layerEnrichment = ProjectEnrichmentService.layer.pipe(const layerRealGit = GitVcsDriver.layer.pipe(Layer.provide(VcsProcess.layer));interface HarnessOptions {+ /** The processes repository detection runs, such as a machine with no Git. */+ readonly vcsProcess?: Layer.Layer<VcsProcess.VcsProcess>;/** A git driver for failures real git cannot produce on demand. */readonly git?: Layer.Layer<GitVcsDriver.GitVcsDriver>;/** Wraps the real ProjectService, for failures it cannot produce on demand. */@@ -81,7 +84,7 @@ const layer = (baseDir: string, options?: HarnessOptions) =>Layer.provideMerge(RuntimeLayer.layerProjectService),Layer.provideMerge(layerEnrichment),Layer.provideMerge(WorkspacePaths.layer),- Layer.provideMerge(layerGitWorkflow),+ Layer.provideMerge(layerGitWorkflow(options?.vcsProcess)),Layer.provideMerge(options?.git ?? layerRealGit),Layer.provideMerge(SqlitePersistence.layerMemory),Layer.provideMerge(ServerConfig.layerTest(baseDir, baseDir)),@@ -145,6 +148,34 @@ it.effect("offers nothing when the data dir sits inside a Git checkout", () =>}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),);+it.effect("offers a Scratch folder when Git is not installed", () =>+ withScratch(+ ({ baseDir }) =>+ Effect.gen(function* () {+ const path = yield* Path.Path;+ assert.equal(yield* requireRoot, path.resolve(baseDir, "scratch"));+ }),+ {+ vcsProcess: Layer.succeed(VcsProcess.VcsProcess, {+ run: (input) =>+ Effect.fail(+ new VcsProcessSpawnError({+ operation: input.operation,+ command: input.command,+ cwd: input.cwd,+ cause: PlatformError.systemError({+ _tag: "NotFound",+ module: "ChildProcess",+ method: "spawn",+ syscall: `spawn ${input.command}`,+ }),+ }),+ ),+ }),+ },+ ),+);+it.effect("creates one Scratch project, even for concurrent first requests", () =>withScratch(() =>Effect.gen(function* () {diff --git a/apps/server/src/project/ManagedProjectFolders.ts b/apps/server/src/project/ManagedProjectFolders.tsindex 1ce8b56ea57..912f2dbda70 100644--- a/apps/server/src/project/ManagedProjectFolders.ts+++ b/apps/server/src/project/ManagedProjectFolders.ts@@ -9,7 +9,13 @@** @module ManagedProjectFolders*/-import { CommandId, ProjectId, type ThreadId } from "@t3tools/contracts";+import {+ CommandId,+ type GitManagerServiceError,+ ProjectId,+ type ThreadId,+ VcsProcessSpawnError,+} from "@t3tools/contracts";import { newProjectFolderName } from "@t3tools/shared/path";import * as Cause from "effect/Cause";import * as Context from "effect/Context";@@ -220,6 +226,15 @@ function describeCommitFailure(stderr: string): string {return lines.at(-1) ?? "Git could not make the first commit.";}+const isVcsProcessSpawnError = Schema.is(VcsProcessSpawnError);++// Spawning a `git` that is not on PATH fails with NotFound, on Windows too.+const isGitMissing = (error: GitManagerServiceError) =>+ error._tag === "GitManagerError" &&+ isVcsProcessSpawnError(error.cause) &&+ PlatformError.isPlatformError(error.cause.cause) &&+ error.cause.cause.reason._tag === "NotFound";+const make = Effect.gen(function* () {const config = yield* ServerConfig.ServerConfig;const fileSystem = yield* FileSystem.FileSystem;@@ -258,11 +273,13 @@ const make = Effect.gen(function* () {// Inside a checkout (a dev worktree's .t3, a dotfiles home) the folder would// inherit the repo's git status and checkpoints, so Scratch is offered only- // when the data dir is outside any work tree. Probed once; detection+ // when the data dir is outside any work tree. With no Git installed nothing+ // can see a checkout, so Scratch is offered. Probed once; other detection// failures hide Scratch rather than failing callers. An interrupted probe// invalidates the cache so the next caller probes again.const [probe, invalidate] = yield* Effect.cachedInvalidateWithTTL(gitWorkflow.isRepository(config.baseDir).pipe(+ Effect.catchIf(isGitMissing, () => Effect.succeed(false)),Effect.map((isRepository) =>isRepository ? Option.none<string>() : Option.some(path.resolve(config.baseDir, "scratch")),),