-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix: Incorrect escaping of spaces in file paths #2565
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
Fix: Incorrect escaping of spaces in file paths #2565
Conversation
- Fix escaping issues when file paths contain spaces and special characters - Improve handling of multi-level escaping in path mentions - Add comprehensive tests for path and context mentions with special characters - Add detailed documentation about backslash escaping logic
🦋 Changeset detectedLatest commit: d4e9d20 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| const fullPath = `C:\\Users\\user\\project\\${longDirectoryName}\\${longFileName}` | ||
|
|
||
| expect(convertToMentionPath(fullPath, "C:\\Users\\user\\project")).toBe( | ||
| `@/${longDirectoryName}/${longFileName.replace(/ /g, "\\ ")}`, |
Check failure
Code scanning / CodeQL
Incomplete string escaping or encoding High test
|
Hi, thank you for working on this! Unfortunately I'm not sure it's working for me though: I also took a brief attempt at this at #2363 as well in case that's helpful, but I got distracted and didn't have time to finish. Let me know if I can help at all or if I'm doing something wrong while testing this! |
|
@mrubens After writing the fix, I only add several unit test based on my understanding of this project. But I don't know how to actually test it as a plugin in vscode. Maybe you could help me with that. |
|
@Yikai-Liao let me know if this helps https://github.com/RooVetGit/Roo-Code?tab=readme-ov-file#local-setup--development |
This commit addresses an issue with path handling in the mentions feature, specifically for Windows platforms. The primary fixes include: 1. Modified the mention regex in context-mentions.ts to use greedy matching for paths, ensuring complete path capture including multiple spaces. 2. Updated path handling in mentions/index.ts to properly process relative paths and remove leading slashes before resolving. 3. Fixed XML tag display to maintain consistent path format in output. 4. Added detailed comments explaining the regex pattern and path handling logic. 5. Enhanced test suite with additional cases for complex paths and cross-platform path handling. The issue was causing paths with spaces to be truncated at the first space when using mentions on Windows, resulting in failed file lookups. These changes ensure consistent path handling across platforms while maintaining the original path format in display output.
- Added compiled files and directories to .gitignore to prevent unnecessary tracking. - Removed global type declarations for mockOpenFile from mentions/index.ts to streamline the code. - Enhanced error handling in openMention function to ensure proper error messages are displayed when workspace paths are not found. - Updated tests to improve coverage for various file path scenarios, including international characters and escaped spaces. - Ensured consistent path normalization across different platforms in mentions handling.
- Updated mock implementations for file system operations to use async/await for better readability and consistency. - Enhanced mock functions to return promises, ensuring proper handling of asynchronous operations in tests. - Improved path normalization checks in tests to ensure compatibility with various path formats, including escaped spaces and international characters. - Adjusted test cases to verify correct behavior for file and directory mentions, including edge cases with special characters and long paths.
- Improved the extraction logic in `extractFilePath` to handle special cases for escaped spaces and Windows-style paths. - Updated test cases to cover various scenarios, including paths with consecutive escaped spaces, international characters, and adjacent mentions. - Refactored mock implementations in tests for better clarity and consistency, ensuring proper handling of asynchronous operations. - Enhanced error handling for invalid paths and permission issues, providing clearer feedback in test results.
|
@mrubens Thanks, I can install it noe. After several commits, I find that roo code now could generate and parse all kinds of path. But the highlight in UI is incorrect(only highlight @/). I try to add a test to find out why but failed. Could you help me with that? |
- Introduced a mock implementation for `react-i18next` to simplify testing. - Enhanced the `parseMentionsFromText` and `extractFilePath` functions to improve handling of paths with escaped spaces and special characters. - Added comprehensive unit tests for mentions parsing, covering various scenarios including escaped spaces, international characters, and multiple mentions. - Updated the `highlightMentions` function to utilize the new parsing logic, ensuring accurate highlighting of mentions in the chat interface. - Improved debugging information in the context menu logic to assist in identifying valid mentions.
|
Oh, nice, I have fixed the highlight locally 🎉 |
…ndency - Refined space escaping logic in `context-mentions.ts` and `path-mentions.ts` to handle potential edge cases and improve robustness, addressing concerns about escaped characters. - Added `path-browserify` and `@types/path-browserify` to `webview-ui/package.json` to resolve missing dependency and type errors.
|
@mrubens Sorry, but it seems that I can't deal with the CodeQL Check by my self. I have no idea how to move forward. |
| const formattedValue = value | ||
| .replace(/\\\\\\\\/g, "\\DOUBLE_BACKSLASH") // First preserve actual double backslashes | ||
| .replace(/\\\\ /g, "\\ESCAPED_SPACE") // Temporarily replace already escaped spaces | ||
| // ADDED: Escape standalone backslashes not part of the above patterns | ||
| .replace(/(?<!\\\\)\\\\(?! |\\\\)/g, '\\\\\\\\') // Makes \ -> \\ | ||
| .replace(/ /g, "\\ ") // Escape normal spaces (makes ' ' -> '\\ ') |
Check failure
Code scanning / CodeQL
Incomplete string escaping or encoding High
- Added `escapePath` function to encapsulate multi-level escaping for backslashes and spaces in file paths. - Updated `convertToMentionPath` to utilize the new helper function, improving code clarity and consistency in path handling. - Ensured robust handling of paths with escaped characters across different scenarios.
- `context-mentions.ts`: The multi-step replace logic correctly handles backslash escaping, but may confuse static analysis. - `path-mentions.test.ts`: Warning triggered on a string literal within a test assertion.
- remove debug message
|
I further fixed "show context window". I found that if there are multiple @, this won't work in new @ But I still can't deal with the CodeQL warning @mrubens |
|
Could anyone give me some suggestions on this PR? |
|
@mrubens Could you review my PR and give me some advice? |
|
Thank you for the PR! We just merged #3044 to fix this, so going to close this one. |
|
Sorry for the late reply. But I found that something fixed in my pr is still there now. For example, if I have multiple "@", roo code will give some totally wrong highlights for these "@". But I don't have time to fix it in the current branch. So any plan about this now? @mrubens |

Description
This PR addresses the issues reported in Issue #2564 by implementing the following changes:
Context
This change fixes a bug where file paths containing spaces or special characters were not properly escaped, leading to failures in mention highlighting and file access. The problem affects utility functions like
convertToMentionPathandinsertMention, causing inconsistent behavior across different operating systems. Resolving this ensures better reliability for users dealing with common file naming conventions, improving overall user experience in path referencing.Implementation
The implementation involved updating the escaping logic in
path-mentions.tsandcontext-mentions.tsto handle spaces and special characters more accurately. Specifically:convertToMentionPathto correctly escape spaces using single backslashes and prevent double-escaping by checking for existing escapes.insertMentionto maintain consistent path formatting during insertion, ensuring that multi-level escapes are processed sequentially without adding extra backslashes.context-mentions.test.tsandpath-mentions.test.tsto cover edge cases like paths with multiple spaces, special characters, and pre-escaped strings.These changes were made with minimal refactoring to avoid breaking existing functionality, focusing on targeted fixes for the reported issues.
How to Test
To verify the fixes, follow these steps:
fix/path-mentions-escapingbranch.test folder with spaces/test file with spaces.txt).@to trigger the mention functionality and select the test file.@/test\ folder\ with\ spaces/test\ file\ with\ spaces.txt).npx jest webview-ui/src/utils/__tests__/context-mentions.test.tsandnpx jest webview-ui/src/utils/__tests__/path-mentions.test.tsto ensure all new test cases pass.#,@) and pre-escaped paths to confirm no double-escaping occurs.If any issues persist, check the console for errors and ensure your environment matches the one in the issue (e.g., Windows or Unix-based OS).
Important
Fixes incorrect escaping of spaces and special characters in file paths, with updated tests and documentation.
insertMentionandconvertToMentionPath.context-mentions.test.tsandpath-mentions.test.tsfor paths with spaces, special characters, and pre-escaped strings.context-mentions.tsandpath-mentions.tsto explain multi-level escaping logic.This description was created by
for 694f6dc. It will automatically update as commits are pushed.