Skip to content

Commit b49b5a9

Browse files
committed
🤖 Use runtime.normalizePath() for DRY in validateNoRedundantPrefix
Instead of manually normalizing paths with string operations, leverage the runtime's existing normalizePath() method for consistent path handling. This reduces duplication and ensures we handle paths the same way the runtime does. Changes: - Use runtime.normalizePath(".", cwd) to normalize the working directory - Keeps manual trailing slash cleanup for absolute paths (can't use normalizePath directly since it's designed for relative path resolution) - Update documentation to reflect use of runtime method - Remove underscore prefix from runtime param (now used) _Generated with `cmux`_
1 parent 302cbe7 commit b49b5a9

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

src/services/tools/fileCommon.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,36 +53,41 @@ export function validateFileSize(stats: FileStat): { error: string } | null {
5353
* Returns an error object if the path contains the cwd prefix, null if valid.
5454
* This helps save tokens by encouraging relative paths.
5555
*
56-
* Works for both local and SSH runtimes by using simple string matching
57-
* for absolute paths instead of Node's path module (which only handles local paths).
56+
* Works for both local and SSH runtimes by using runtime.normalizePath()
57+
* for consistent path handling across different runtime types.
5858
*
5959
* @param filePath - The file path to validate
6060
* @param cwd - The working directory
61-
* @param runtime - The runtime (unused, kept for consistency)
61+
* @param runtime - The runtime to use for path normalization
6262
* @returns Error object if redundant prefix found, null if valid
6363
*/
6464
export function validateNoRedundantPrefix(
6565
filePath: string,
6666
cwd: string,
67-
_runtime: Runtime
67+
runtime: Runtime
6868
): { error: string } | null {
6969
// Only check absolute paths (start with /) - relative paths are fine
7070
// This works for both local and SSH since both use Unix-style paths
7171
if (!filePath.startsWith("/")) {
7272
return null;
7373
}
7474

75-
// Normalize both paths: remove trailing slashes for consistent comparison
75+
// Use runtime's normalizePath to ensure consistent handling across local and SSH
76+
// Normalize the cwd to get canonical form (removes trailing slashes, etc.)
77+
const normalizedCwd = runtime.normalizePath(".", cwd);
78+
79+
// For absolute paths, we can't use normalizePath directly (it resolves relative paths)
80+
// so just clean up trailing slashes manually
7681
const normalizedPath = filePath.replace(/\/+$/, "");
77-
const normalizedCwd = cwd.replace(/\/+$/, "");
82+
const cleanCwd = normalizedCwd.replace(/\/+$/, "");
7883

7984
// Check if the absolute path starts with the cwd
8085
// Use startsWith + check for path separator to avoid partial matches
8186
// e.g., /workspace/project should match /workspace/project/src but not /workspace/project2
82-
if (normalizedPath === normalizedCwd || normalizedPath.startsWith(normalizedCwd + "/")) {
87+
if (normalizedPath === cleanCwd || normalizedPath.startsWith(cleanCwd + "/")) {
8388
// Calculate what the relative path would be
8489
const relativePath =
85-
normalizedPath === normalizedCwd ? "." : normalizedPath.substring(normalizedCwd.length + 1);
90+
normalizedPath === cleanCwd ? "." : normalizedPath.substring(cleanCwd.length + 1);
8691
return {
8792
error: `Redundant path prefix detected. The path '${filePath}' contains the workspace directory. Please use relative paths to save tokens: '${relativePath}'`,
8893
};

0 commit comments

Comments
 (0)