Skip to content

fix: properly encode file URLs with special characters#12424

Open
yudgnahk wants to merge 3 commits intoanomalyco:devfrom
yudgnahk:fix/file-url-encoding-upstream
Open

fix: properly encode file URLs with special characters#12424
yudgnahk wants to merge 3 commits intoanomalyco:devfrom
yudgnahk:fix/file-url-encoding-upstream

Conversation

@yudgnahk
Copy link
Contributor

@yudgnahk yudgnahk commented Feb 6, 2026

Summary

Fixes critical bug where filenames containing special characters (#, ?, %, spaces) caused ENOENT errors and path truncation throughout the application.

Root Cause

Using string interpolation `file://${path}` instead of proper URL encoding. The # character is treated as a URL fragment identifier, causing paths like /path/to/file#name.txt to be truncated to /path/to/file.

Solution

  • Backend (Node.js/Bun): Use pathToFileURL() and fileURLToPath() built-in APIs
  • Frontend (Browser): Custom encoding/decoding helpers (Bun APIs not available in browser)
  • Special characters are now properly encoded: #%23, ?%3F, etc.

Changes

Backend Files (7 files)

  • packages/opencode/src/session/prompt.ts - Fixed 2 occurrences
  • packages/opencode/src/cli/cmd/run.ts - Fixed 1 occurrence
  • packages/opencode/src/acp/agent.ts - Fixed 4 occurrences
  • packages/opencode/src/lsp/index.ts - Fixed decoding with fileURLToPath()
  • packages/opencode/src/cli/cmd/tui/component/dialog-status.tsx - Fixed 1 occurrence
  • packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx - Fixed 1 occurrence
  • packages/opencode/test/session/prompt-special-chars.test.ts - Added test case for # character

Frontend Files (2 files)

  • packages/app/src/components/prompt-input.tsx - Custom pathToFileUrl() helper
  • packages/app/src/context/file.tsx - encodeFilePath() and decodeFilePath() helpers

SDK/Demo Files (2 files)

  • packages/sdk/js/example/example.ts - Fixed example code
  • packages/app/src/components/file-tree.tsx - Fixed drag-and-drop encoding

Testing

All tests pass: bun test - 884 pass, 1 skip, 0 fail
TypeScript clean: All packages typecheck successfully
Real file testing: Created test files file#name.txt, file?name.txt and verified operations
Backward compatible: Normal paths produce identical output

Impact

Fixes:

  • File tagging with @file#name.txt
  • Diff/Review view loading
  • Drag-and-drop operations
  • SDK file operations
  • Write tool for files with special characters

11 files changed, 114 insertions(+), 21 deletions(-)

Related Issues

Fixes #11790
Partially fixes #9804
May improve #9173, #8172, #8759, #11630

Fixes ENOENT errors when filenames contain # ? % or other URL-reserved
characters by using pathToFileURL() instead of string interpolation.

Changes:
- Backend: Use pathToFileURL()/fileURLToPath() from Bun/Node.js
- Frontend: Custom encoding helpers (browser-compatible)
- SDK Example: Fixed demo code
- Drag-and-drop: Fixed file-tree.tsx encoding

Fixes anomalyco#11790
Partially fixes anomalyco#9804
May improve anomalyco#9173, anomalyco#8172, anomalyco#8759, anomalyco#11630

11 files changed, 102 insertions(+), 21 deletions(-)
@github-actions
Copy link
Contributor

github-actions bot commented Feb 6, 2026

The following comment was made by an LLM, it may be inaccurate:

I found 1 potentially related PR:

Related PR:

The other match (#10049 about Windows drive letter case) is less directly related to special character encoding.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

1 participant