From 8a5709324f847e5cb9fd1bb8b088f5e8330630a6 Mon Sep 17 00:00:00 2001 From: James Long Date: Tue, 8 Sep 2026 12:03:53 -0400 Subject: [PATCH] fix(worktree): resolve configured paths relative to project (#47990) --- packages/core/src/config/plugin/worktree.ts | 2 +- packages/core/test/worktree.test.ts | 57 ++++++++++++++++++++- packages/schema/src/config/worktree.ts | 2 +- services/www/src/docs/content/config.mdx | 15 +++++- 4 files changed, 71 insertions(+), 5 deletions(-) diff --git a/packages/core/src/config/plugin/worktree.ts b/packages/core/src/config/plugin/worktree.ts index 56af489d77c..8bd97d7cc71 100644 --- a/packages/core/src/config/plugin/worktree.ts +++ b/packages/core/src/config/plugin/worktree.ts @@ -26,7 +26,7 @@ export const Plugin = define({ directory: AbsolutePath.make( directory.startsWith("~/") ? path.join(global.home, directory.slice(2)) - : path.resolve(entry.path ? path.dirname(entry.path) : location.directory, directory), + : path.resolve(location.project.canonical, directory), ), }) } diff --git a/packages/core/test/worktree.test.ts b/packages/core/test/worktree.test.ts index 151799a4081..d1deeef8299 100644 --- a/packages/core/test/worktree.test.ts +++ b/packages/core/test/worktree.test.ts @@ -315,6 +315,15 @@ describe("Worktree", () => { const bus = yield* Bus.Service const context = yield* Layer.build(worktreeLayer(selected.directory, selected.id, database, bus, root.path)) const worktrees = Context.get(context, Worktree.Service) + const config = yield* Config.Test + yield* config.setEntries([ + new Document({ + type: "document", + path: abs(path.join(root.path, "global/opencode.json")), + info: new Info({ worktree: { directory: ".lane/trees" } }), + }), + ]) + yield* ConfigWorktreePlugin.Plugin.effect(host()).pipe(Effect.provide(context)) yield* projects.update({ projectID: initial.id, commands: { @@ -326,11 +335,11 @@ describe("Worktree", () => { const created = yield* worktrees.create({ strategy: gitWorktree, from: selected.canonical, - directory: abs(path.join(root.path, "worktrees")), name: "selected-clone", }) expect(selected.id).toBe(initial.id) + expect(created.directory).toBe(abs(path.join(clone, ".lane/trees/selected-clone"))) expect((yield* projects.list()).find((project) => project.id === initial.id)?.canonical).toBe(main) expect(yield* Effect.promise(() => $`git rev-parse HEAD`.cwd(created.directory).text())).toBe( yield* Effect.promise(() => $`git rev-parse HEAD`.cwd(clone).text()), @@ -911,7 +920,7 @@ describe("Worktree", () => { }), ) const first = yield* worktrees.create({ name: "one" }) - expect(first.directory).toBe(abs(path.join(input.root.path, "nested/copies/one"))) + expect(first.directory).toBe(abs(path.join(input.root.path, "copies/one"))) expect(yield* stored(input.projectID)).toContainEqual({ directory: first.directory, strategy: "custom" }) yield* config.setEntries(documents.slice(0, 1)) yield* bus.publish(Event.Updated, {}) @@ -925,6 +934,50 @@ describe("Worktree", () => { expect(third.directory).toBe(abs(path.join(input.root.path, "worktree", input.projectID.slice(0, 6), "three"))) }), ) + ;["relative", "absolute", "home"].forEach((mode) => { + it.live(`resolves ${mode} global directory config from a linked checkout's subdirectory`, () => + Effect.gen(function* () { + const input = yield* setup() + const config = yield* Config.Test + const projects = yield* Project.Service + const global = yield* Global.Service + const worktrees = yield* Worktree.Service + const linked = abs(path.join(input.root.path, "linked")) + const nested = abs(path.join(linked, "src")) + const home = abs(path.join(input.root.path, "home")) + yield* Effect.promise(async () => { + await $`git worktree add ${linked} -b linked`.cwd(input.sourceDirectory).quiet() + await fs.mkdir(nested) + }) + const project = yield* projects.resolve(nested) + const directory = + mode === "relative" ? ".lane/trees" : mode === "home" ? "~/copies" : path.join(home, "absolute") + yield* config.setEntries([ + new Document({ + type: "document", + path: abs(path.join(home, ".config/opencode/opencode.json")), + info: new Info({ worktree: { directory } }), + }), + ]) + yield* ConfigWorktreePlugin.Plugin.effect(host()).pipe( + Effect.provideService(Location.Service, { directory: nested, project }), + Effect.provideService(Global.Service, { ...global, home }), + ) + + const created = yield* worktrees.create({ name: "task" }) + + expect(project.directory).toBe(linked) + expect(project.canonical).toBe(input.sourceDirectory) + expect(created.directory).toBe( + abs( + mode === "relative" + ? path.join(input.sourceDirectory, ".lane/trees/task") + : path.join(home, mode === "home" ? "copies/task" : "absolute/task"), + ), + ) + }), + ) + }) it.effect("normalization retains worktree directory and rejects invalid configuration", () => Effect.sync(() => { diff --git a/packages/schema/src/config/worktree.ts b/packages/schema/src/config/worktree.ts index 399f4ff1b1a..0446c6a9a37 100644 --- a/packages/schema/src/config/worktree.ts +++ b/packages/schema/src/config/worktree.ts @@ -4,7 +4,7 @@ import { Schema } from "effect" export const Info = Schema.Struct({ directory: Schema.Trim.pipe(Schema.check(Schema.isNonEmpty())).annotate({ - description: "Parent directory for new worktrees, relative to the declaring config file when not absolute", + description: "Parent directory for new worktrees, relative to the project's primary checkout when not absolute", }), }).annotate({ identifier: "Config.Worktree" }) export interface Info extends Schema.Schema.Type {} diff --git a/services/www/src/docs/content/config.mdx b/services/www/src/docs/content/config.mdx index 99eba666d45..7957b64bc19 100644 --- a/services/www/src/docs/content/config.mdx +++ b/services/www/src/docs/content/config.mdx @@ -473,7 +473,20 @@ Set the parent directory for new local worktrees. OpenCode appends the requested } ``` -Relative paths resolve against the config file that declares them; `~/` resolves against the user's home directory. +Relative paths resolve against the project's primary checkout, including when called from a subdirectory or linked +worktree. This applies to global and project configuration alike; absolute paths are used as-is, and `~/` resolves +against the user's home directory. + +For example, this global configuration places new worktrees under each project's own `.lane/trees/` directory: + +```jsonc +{ + "worktree": { + "directory": ".lane/trees", + }, +} +``` + Without this setting, creation uses the server's data directory under `worktree/`. Configuration applies to the caller's location, not every clone sharing a project ID. Changing it does not move existing worktrees.