Skip to content

fix(sdk): normalize scheme-less base URLs so location query params apply#32125

Open
tangtaizong666 wants to merge 1 commit into
anomalyco:devfrom
tangtaizong666:fix/attach-schemeless-url-directory
Open

fix(sdk): normalize scheme-less base URLs so location query params apply#32125
tangtaizong666 wants to merge 1 commit into
anomalyco:devfrom
tangtaizong666:fix/attach-schemeless-url-directory

Conversation

@tangtaizong666

Copy link
Copy Markdown

Issue for this PR

Closes #32077

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

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 in packages/sdk/js/src/v2/client.ts, which decides whether to add the location[directory] query param by checking url.pathname.startsWith("/api/"). With a scheme-less base URL, new URL("localhost:4096/api/fs/find") parses localhost as the protocol, the pathname comes out as 4096/api/fs/find, and the check fails. The request goes out with only the flat directory param (which the v2 location middleware does not read) and the x-opencode-directory header 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 to http://. 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 serve in directory A, attached from directory B with attach localhost:<port> --dir <B>, typed @ in the prompt - results came from A. Server-side logging showed /api/fs/find arriving without location[directory]. Same flow with http:// 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 typecheck passes in packages/sdk/js and packages/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):

@file -> serverfile_alpha.txt        (server's cwd)
GET /api/fs/find?query=file&limit=20&directory=...   <- location[directory] missing

After:

@file -> clientfile_beta.txt         (attached client's dir)
GET /api/fs/find?query=file&limit=20&directory=...&location[directory]=...

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@tangtaizong666 tangtaizong666 force-pushed the fix/attach-schemeless-url-directory branch from 7c438d3 to ed66c38 Compare June 13, 2026 04:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Fuzzy search does not respect working directory when attached to server

1 participant