fix(sdk): normalize scheme-less base URLs so location query params apply#32125
Open
tangtaizong666 wants to merge 1 commit into
Open
fix(sdk): normalize scheme-less base URLs so location query params apply#32125tangtaizong666 wants to merge 1 commit into
tangtaizong666 wants to merge 1 commit into
Conversation
7c438d3 to
ed66c38
Compare
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Issue for this PR
Closes #32077
Type of change
What does this PR do?
When attaching to a remote server with a scheme-less URL (
opencode attach localhost:4096 --dir ., exactly the command from the issue), fuzzy search runs against the server's working directory instead of the attached client's directory.Root cause: the SDK injects the client directory into GET requests via the
rewrite()interceptor inpackages/sdk/js/src/v2/client.ts, which decides whether to add thelocation[directory]query param by checkingurl.pathname.startsWith("/api/"). With a scheme-less base URL,new URL("localhost:4096/api/fs/find")parseslocalhostas the protocol, the pathname comes out as4096/api/fs/find, and the check fails. The request goes out with only the flatdirectoryparam (which the v2 location middleware does not read) and thex-opencode-directoryheader already stripped by the interceptor, so the server falls back to its own cwd. The TUI's@autocomplete hits/api/fs/find, hence the report. fetch resolves scheme-less URLs without complaint, so everything else keeps working and the breakage is silent.The fix normalizes the base URL in
createOpencodeClient: if it has no scheme, default tohttp://. URL parsing then sees the real pathname and the location params are applied as designed. Explicit http/https base URLs are untouched. Fixing it in the client (rather than in the attach command) covers every SDK consumer:attach,validateSession,run --attach.How did you verify your code works?
Reproduced on dev: started
servein directory A, attached from directory B withattach localhost:<port> --dir <B>, typed@in the prompt - results came from A. Server-side logging showed/api/fs/findarriving withoutlocation[directory]. Same flow withhttp://worked, confirming the mechanism.After the fix, the same scheme-less attach returns directory B's files and the request carries
location[directory].Added
packages/opencode/test/server/sdk-base-url.test.ts: the scheme-less case fails without the fix (location[directory]missing) and passes with it, plus two tests pinning that explicit http/https base URLs are preserved.bun run typecheckpasses inpackages/sdk/jsandpackages/opencode; the related server test files show no new failures compared to dev.Screenshots / recordings
Before (attach
localhost:4096, server started in a different directory):After:
Checklist