-
Notifications
You must be signed in to change notification settings - Fork 15.8k
fix(tui): canonicalize cwd after chdir #16641
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import { describe, expect, mock, test } from "bun:test" | ||
| import fs from "fs/promises" | ||
| import path from "path" | ||
| import { tmpdir } from "../../fixture/fixture" | ||
|
|
||
| const stop = new Error("stop") | ||
| const seen = { | ||
| tui: [] as string[], | ||
| inst: [] as string[], | ||
| } | ||
|
|
||
| mock.module("../../../src/cli/cmd/tui/app", () => ({ | ||
| tui: async (input: { directory: string }) => { | ||
| seen.tui.push(input.directory) | ||
| throw stop | ||
| }, | ||
| })) | ||
|
|
||
| mock.module("@/util/rpc", () => ({ | ||
| Rpc: { | ||
| client: () => ({ | ||
| call: async () => ({ url: "http://127.0.0.1" }), | ||
| on: () => {}, | ||
| }), | ||
| }, | ||
| })) | ||
|
|
||
| mock.module("@/cli/ui", () => ({ | ||
| UI: { | ||
| error: () => {}, | ||
| }, | ||
| })) | ||
|
|
||
| mock.module("@/util/log", () => ({ | ||
| Log: { | ||
| init: async () => {}, | ||
| create: () => ({ | ||
| error: () => {}, | ||
| info: () => {}, | ||
| warn: () => {}, | ||
| debug: () => {}, | ||
| time: () => ({ stop: () => {} }), | ||
| }), | ||
| Default: { | ||
| error: () => {}, | ||
| info: () => {}, | ||
| warn: () => {}, | ||
| debug: () => {}, | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| mock.module("@/util/timeout", () => ({ | ||
| withTimeout: <T>(input: Promise<T>) => input, | ||
| })) | ||
|
|
||
| mock.module("@/cli/network", () => ({ | ||
| withNetworkOptions: <T>(input: T) => input, | ||
| resolveNetworkOptions: async () => ({ | ||
| mdns: false, | ||
| port: 0, | ||
| hostname: "127.0.0.1", | ||
| }), | ||
| })) | ||
|
|
||
| mock.module("../../../src/cli/cmd/tui/win32", () => ({ | ||
| win32DisableProcessedInput: () => {}, | ||
| win32InstallCtrlCGuard: () => undefined, | ||
| })) | ||
|
|
||
| mock.module("@/config/tui", () => ({ | ||
| TuiConfig: { | ||
| get: () => ({}), | ||
| }, | ||
| })) | ||
|
|
||
| mock.module("@/project/instance", () => ({ | ||
| Instance: { | ||
| provide: async (input: { directory: string; fn: () => Promise<unknown> | unknown }) => { | ||
| seen.inst.push(input.directory) | ||
| return input.fn() | ||
| }, | ||
| }, | ||
| })) | ||
|
|
||
| describe("tui thread", () => { | ||
| async function call(project?: string) { | ||
| const { TuiThreadCommand } = await import("../../../src/cli/cmd/tui/thread") | ||
| const args: Parameters<NonNullable<typeof TuiThreadCommand.handler>>[0] = { | ||
| _: [], | ||
| $0: "opencode", | ||
| project, | ||
| prompt: "hi", | ||
| model: undefined, | ||
| agent: undefined, | ||
| session: undefined, | ||
| continue: false, | ||
| fork: false, | ||
| port: 0, | ||
| hostname: "127.0.0.1", | ||
| mdns: false, | ||
| "mdns-domain": "opencode.local", | ||
| mdnsDomain: "opencode.local", | ||
| cors: [], | ||
| } | ||
| return TuiThreadCommand.handler(args) | ||
| } | ||
|
|
||
| async function check(project?: string) { | ||
| await using tmp = await tmpdir({ git: true }) | ||
| const cwd = process.cwd() | ||
| const pwd = process.env.PWD | ||
| const worker = globalThis.Worker | ||
| const tty = Object.getOwnPropertyDescriptor(process.stdin, "isTTY") | ||
| const link = path.join(path.dirname(tmp.path), path.basename(tmp.path) + "-link") | ||
| const type = process.platform === "win32" ? "junction" : "dir" | ||
| seen.tui.length = 0 | ||
| seen.inst.length = 0 | ||
| await fs.symlink(tmp.path, link, type) | ||
|
|
||
| Object.defineProperty(process.stdin, "isTTY", { | ||
| configurable: true, | ||
| value: true, | ||
| }) | ||
| globalThis.Worker = class extends EventTarget { | ||
| onerror = null | ||
| onmessage = null | ||
| onmessageerror = null | ||
| postMessage() {} | ||
| terminate() {} | ||
| } as unknown as typeof Worker | ||
|
|
||
| try { | ||
| process.chdir(tmp.path) | ||
| process.env.PWD = link | ||
| await expect(call(project)).rejects.toBe(stop) | ||
| expect(seen.inst[0]).toBe(tmp.path) | ||
| expect(seen.tui[0]).toBe(tmp.path) | ||
| } finally { | ||
| process.chdir(cwd) | ||
| if (pwd === undefined) delete process.env.PWD | ||
| else process.env.PWD = pwd | ||
| if (tty) Object.defineProperty(process.stdin, "isTTY", tty) | ||
| else delete (process.stdin as { isTTY?: boolean }).isTTY | ||
| globalThis.Worker = worker | ||
| await fs.rm(link, { recursive: true, force: true }).catch(() => undefined) | ||
| } | ||
| } | ||
|
|
||
| test("uses the real cwd when PWD points at a symlink", async () => { | ||
| await check() | ||
| }) | ||
|
|
||
| test("uses the real cwd after resolving a relative project from PWD", async () => { | ||
| await check(".") | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
globalThis.Workerstub extendsEventTargetwithout defining a constructor. SinceTuiThreadCommandinstantiatesnew Worker(file, { env: ... }), the default subclass constructor will forward those arguments toEventTarget, which typically throws because it expects no arguments. Add an explicitconstructor(..._args: any[]) { super() }(or otherwise accept and ignore args) so the test doesn’t fail during Worker construction.