Skip to content

Commit 99ead8b

Browse files
catlog22claude
andcommitted
fix(tool): add smart JSON parser with Windows path handling
- Auto-fix Windows backslash paths in JSON input - Provide helpful hints when path escaping errors occur 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <[email protected]>
1 parent 0c7f13d commit 99ead8b

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

ccw/src/commands/tool.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,42 @@ async function readStdin() {
9797
});
9898
}
9999

100+
/**
101+
* Smart JSON parser with Windows path handling
102+
*/
103+
function parseJsonWithPathFix(jsonString) {
104+
try {
105+
// Try normal parse first
106+
return JSON.parse(jsonString);
107+
} catch (firstError) {
108+
// If parsing fails, try to fix Windows paths
109+
try {
110+
// Pattern: "path": "X:\..." or "path":"X:\..."
111+
const fixedJson = jsonString.replace(
112+
/("(?:path|file|target|source|dest|destination)":\s*")([A-Za-z]:[^"]+)"/g,
113+
(match, prefix, path) => {
114+
// Convert backslashes to forward slashes (universal)
115+
const fixedPath = path.replace(/\\/g, '/');
116+
return `${prefix}${fixedPath}"`;
117+
}
118+
);
119+
120+
return JSON.parse(fixedJson);
121+
} catch (secondError) {
122+
// If still fails, throw original error with helpful message
123+
const errorMsg = firstError.message;
124+
const hint = errorMsg.includes('escaped character') || errorMsg.includes('position')
125+
? '\n\n' + chalk.yellow('Hint: Windows paths in JSON need forward slashes or double backslashes:') +
126+
'\n ' + chalk.green('✓ "D:/Claude_dms3/file.md"') +
127+
'\n ' + chalk.green('✓ "D:\\\\Claude_dms3\\\\file.md"') +
128+
'\n ' + chalk.red('✗ "D:\\Claude_dms3\\file.md"')
129+
: '';
130+
131+
throw new Error(errorMsg + hint);
132+
}
133+
}
134+
}
135+
100136
/**
101137
* Execute a tool with given parameters
102138
*/
@@ -119,7 +155,7 @@ async function execAction(toolName, jsonInput, options) {
119155

120156
if (jsonInput) {
121157
try {
122-
params = JSON.parse(jsonInput);
158+
params = parseJsonWithPathFix(jsonInput);
123159
} catch (error) {
124160
console.error(chalk.red(`Invalid JSON: ${error.message}`));
125161
process.exit(1);

0 commit comments

Comments
 (0)