[PR #12424] fix: properly encode file URLs with special characters #14209

Closed
opened 2026-02-16 18:19:01 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/anomalyco/opencode/pull/12424

State: closed
Merged: Yes


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 (10 files)

  • packages/opencode/src/session/prompt.ts - Fixed 2 occurrences using pathToFileURL()
  • packages/opencode/src/cli/cmd/run.ts - Fixed 1 occurrence using pathToFileURL()
  • packages/opencode/src/acp/agent.ts - Fixed 3 occurrences using pathToFileURL()
  • packages/opencode/src/lsp/index.ts - Fixed decoding with fileURLToPath()
  • packages/opencode/src/cli/cmd/tui/component/dialog-status.tsx - Fixed 1 occurrence using fileURLToPath()
  • packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx - Fixed 1 occurrence using pathToFileURL()
  • packages/opencode/test/session/prompt-special-chars.test.ts - Added comprehensive test case for # character
  • packages/sdk/js/example/example.ts - Fixed SDK example code using pathToFileURL()
  • script/duplicate-pr.ts - Fixed duplicate-pr script using pathToFileURL()

Frontend Files (3 files)

  • packages/app/src/components/prompt-input/build-request-parts.ts - Added encodeFilePath() helper, fixed file attachments and context files (2 locations)
  • packages/app/src/context/file/path.ts - Added encodeFilePath() and decodeFilePath() helpers, fixed normalize() and tab() functions
  • packages/app/src/components/file-tree.tsx - Added pathToFileUrl() helper, fixed drag-and-drop encoding

Testing

All tests pass: bun test - 891 pass, 1 skip, 0 fail
TypeScript clean: All 17 packages typecheck successfully
Special chars test: Verifies # character handling end-to-end with file read/write
Backward compatible: Normal paths produce identical output
Comprehensive coverage: All instances of file://${...} pattern fixed across the codebase

Impact

Fixes:

  • File tagging with @file#name.txt
  • Diff/Review view loading for files with special characters
  • Drag-and-drop operations from file tree
  • SDK file operations with special characters
  • Write tool operations for files with special characters
  • Script utilities that process file paths

12 files changed, 114 insertions(+), 22 deletions(-)

Related Issues

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

**Original Pull Request:** https://github.com/anomalyco/opencode/pull/12424 **State:** closed **Merged:** Yes --- ## 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 (10 files) - `packages/opencode/src/session/prompt.ts` - Fixed 2 occurrences using `pathToFileURL()` - `packages/opencode/src/cli/cmd/run.ts` - Fixed 1 occurrence using `pathToFileURL()` - `packages/opencode/src/acp/agent.ts` - Fixed 3 occurrences using `pathToFileURL()` - `packages/opencode/src/lsp/index.ts` - Fixed decoding with `fileURLToPath()` - `packages/opencode/src/cli/cmd/tui/component/dialog-status.tsx` - Fixed 1 occurrence using `fileURLToPath()` - `packages/opencode/src/cli/cmd/tui/component/prompt/autocomplete.tsx` - Fixed 1 occurrence using `pathToFileURL()` - `packages/opencode/test/session/prompt-special-chars.test.ts` - Added comprehensive test case for `#` character - `packages/sdk/js/example/example.ts` - Fixed SDK example code using `pathToFileURL()` - `script/duplicate-pr.ts` - Fixed duplicate-pr script using `pathToFileURL()` ### Frontend Files (3 files) - `packages/app/src/components/prompt-input/build-request-parts.ts` - Added `encodeFilePath()` helper, fixed file attachments and context files (2 locations) - `packages/app/src/context/file/path.ts` - Added `encodeFilePath()` and `decodeFilePath()` helpers, fixed `normalize()` and `tab()` functions - `packages/app/src/components/file-tree.tsx` - Added `pathToFileUrl()` helper, fixed drag-and-drop encoding ## Testing ✅ **All tests pass**: `bun test` - 891 pass, 1 skip, 0 fail ✅ **TypeScript clean**: All 17 packages typecheck successfully ✅ **Special chars test**: Verifies `#` character handling end-to-end with file read/write ✅ **Backward compatible**: Normal paths produce identical output ✅ **Comprehensive coverage**: All instances of `file://${...}` pattern fixed across the codebase ## Impact **Fixes:** - File tagging with `@file#name.txt` - Diff/Review view loading for files with special characters - Drag-and-drop operations from file tree - SDK file operations with special characters - Write tool operations for files with special characters - Script utilities that process file paths **12 files changed, 114 insertions(+), 22 deletions(-)** ## Related Issues Fixes #11790 Partially fixes #9804 May improve #9173, #8172, #8759, #11630
yindo added the pull-request label 2026-02-16 18:19:01 -05:00
yindo closed this issue 2026-02-16 18:19:01 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#14209