|
1 | 1 | import { appendFile, mkdir, symlink, unlink } from 'fs/promises' |
2 | 2 | import memoize from 'lodash-es/memoize.js' |
3 | | -import { dirname, join } from 'path' |
| 3 | +import { dirname, join, resolve } from 'path' |
4 | 4 | import { getSessionId } from 'src/bootstrap/state.js' |
5 | 5 |
|
6 | 6 | import { type BufferedWriter, createBufferedWriter } from './bufferedWriter.js' |
@@ -90,10 +90,10 @@ export const getDebugFilePath = memoize((): string | null => { |
90 | 90 | for (let i = 0; i < process.argv.length; i++) { |
91 | 91 | const arg = process.argv[i]! |
92 | 92 | if (arg.startsWith('--debug-file=')) { |
93 | | - return arg.substring('--debug-file='.length) |
| 93 | + return resolve(arg.substring('--debug-file='.length)) |
94 | 94 | } |
95 | 95 | if (arg === '--debug-file' && i + 1 < process.argv.length) { |
96 | | - return process.argv[i + 1]! |
| 96 | + return resolve(process.argv[i + 1]!) |
97 | 97 | } |
98 | 98 | } |
99 | 99 | return null |
@@ -170,7 +170,20 @@ function getDebugWriter(): BufferedWriter { |
170 | 170 | // Directory already exists |
171 | 171 | } |
172 | 172 | } |
173 | | - getFsImplementation().appendFileSync(path, content) |
| 173 | + try { |
| 174 | + getFsImplementation().appendFileSync(path, content) |
| 175 | + } catch (e) { |
| 176 | + // If the directory was removed between mkdirSync and |
| 177 | + // appendFileSync (or the path was relative and CWD changed), |
| 178 | + // retry once with a fresh mkdirSync. Swallow on second failure |
| 179 | + // to avoid crashing the React render tree via the error boundary. |
| 180 | + try { |
| 181 | + getFsImplementation().mkdirSync(dir) |
| 182 | + getFsImplementation().appendFileSync(path, content) |
| 183 | + } catch { |
| 184 | + // Best-effort: debug log write failed, don't crash the UI |
| 185 | + } |
| 186 | + } |
174 | 187 | void updateLatestDebugLogSymlink() |
175 | 188 | return |
176 | 189 | } |
@@ -228,7 +241,9 @@ export function logForDebugging( |
228 | 241 | export function getDebugLogPath(): string { |
229 | 242 | return ( |
230 | 243 | getDebugFilePath() ?? |
231 | | - process.env.CLAUDE_CODE_DEBUG_LOGS_DIR ?? |
| 244 | + (process.env.CLAUDE_CODE_DEBUG_LOGS_DIR |
| 245 | + ? resolve(process.env.CLAUDE_CODE_DEBUG_LOGS_DIR) |
| 246 | + : null) ?? |
232 | 247 | join(getClaudeConfigHomeDir(), 'debug', `${getSessionId()}.txt`) |
233 | 248 | ) |
234 | 249 | } |
|
0 commit comments