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
6 changes: 5 additions & 1 deletion packages/core/src/config/plugin/instruction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * as ConfigInstructionPlugin from "./instruction.js"
import { define } from "@opencode/plugin/effect/plugin"
import { FSUtil } from "@opencode/util/fs-util"
import { Global } from "@opencode/util/global"
import { sameDirectory } from "@opencode/util/path"
import { dirname, join } from "path"
import { Effect, PubSub, Semaphore, Stream } from "effect"
import { Watcher } from "../../filesystem/watcher.js"
Expand Down Expand Up @@ -133,7 +134,10 @@ export const Plugin = define({
}),
})

// `start` keeps the client's spelling while `stop` may come from git, so a Windows drive
// letter can differ only in case (`c:\repo` vs `C:\repo`). Compare the way FSUtil.contains
// admitted `start` beneath `stop`, or the walk passes `stop` and recurses at the drive root.
function ancestorDirectories(start: string, stop: string): string[] {
if (start === stop) return [start]
if (sameDirectory(start, stop)) return [start]
return [start, ...ancestorDirectories(dirname(start), stop)]
}
47 changes: 47 additions & 0 deletions packages/core/test/instruction-discovery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,53 @@ describe("ConfigInstructionPlugin.Plugin", () => {
),
)

// A client CWD can spell the drive `c:` while git reports the project root as `C:`.
;(process.platform === "win32" ? it.live : it.live.skip)(
"stops at the project root when the location drive letter differs in case",
() =>
Effect.acquireRelease(
Effect.promise(() => tmpdir()),
(tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
).pipe(
Effect.flatMap((tmp) => {
const global = path.join(tmp.path, "global")
const home = path.join(tmp.path, "home")
const project = path.join(tmp.path, "repo")
const directory = project[0].toLowerCase() + project.slice(1)
return Effect.gen(function* () {
yield* Effect.promise(() => fs.mkdir(project, { recursive: true }))
yield* Effect.promise(() => fs.writeFile(path.join(project, "AGENTS.md"), "project"))
yield* Effect.promise(() => fs.writeFile(path.join(tmp.path, "AGENTS.md"), "outside"))
const discovery = yield* start()
const watcher = yield* Watcher.Test
expect(yield* watcher.subscriptions()).toEqual([
{ path: path.join(global, "AGENTS.md"), type: "file" },
{ path: path.join(directory, "AGENTS.md"), type: "file" },
])
expect((yield* readInitial(yield* discovery.load())).text).toBe(
`Instructions from: ${path.join(directory, "AGENTS.md")}\nproject`,
)
}).pipe(
Effect.provide(
instructionLayer({
config: global,
home,
locationServiceLayer: Layer.succeed(
Location.Service,
Location.Service.of(
location(
{ directory: AbsolutePath.make(directory) },
{ projectDirectory: AbsolutePath.make(project) },
),
),
),
}),
),
)
}),
),
)

it.effect("isolates source failure without failing activation", () => {
const failingFS = Layer.effect(
FSUtil.Service,
Expand Down
5 changes: 3 additions & 2 deletions packages/util/src/fs-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Glob } from "./glob.js"
import { serviceUse } from "./effect/service-use.js"
import { makeGlobalNode } from "./effect/app-node.js"
import { filesystem } from "./effect/app-node-platform.js"
import { sameDirectory } from "./path.js"

export namespace FSUtil {
export class FileSystemError extends Schema.TaggedError<FileSystemError>()("FileSystemError", {
Expand Down Expand Up @@ -178,7 +179,7 @@ export namespace FSUtil {
if (options.mode === "first") return result
}
}
if (options.stop === current) break
if (options.stop && sameDirectory(options.stop, current)) break
const parent = dirname(current)
if (parent === current) break
current = parent
Expand All @@ -198,7 +199,7 @@ export namespace FSUtil {
Effect.orElseSucceed(() => [] as string[]),
)
result.push(...matches)
if (stop === current) break
if (stop && sameDirectory(stop, current)) break
const parent = dirname(current)
if (parent === current) break
current = parent
Expand Down
Loading