(currentDir: string)
| 458 | const visitedDirs = new Set<string>() |
| 459 | |
| 460 | async function walk(currentDir: string): Promise<void> { |
| 461 | if (signal.aborted) { |
| 462 | return |
| 463 | } |
| 464 | |
| 465 | // Cycle detection: track visited directories by device+inode |
| 466 | // Uses bigint: true to handle filesystems with large inodes (e.g., ExFAT) |
| 467 | // that exceed JavaScript's Number precision (53 bits). |
| 468 | // See: https://github.com/anthropics/claude-code/issues/13893 |
| 469 | try { |
| 470 | const stats = await stat(currentDir, { bigint: true }) |
| 471 | if (stats.isDirectory()) { |
| 472 | const dirKey = |
| 473 | stats.dev !== undefined && stats.ino !== undefined |
| 474 | ? `${stats.dev}:${stats.ino}` // Unix/Linux: device + inode |
| 475 | : await realpath(currentDir) // Windows: canonical path |
| 476 | |
| 477 | if (visitedDirs.has(dirKey)) { |
| 478 | logForDebugging( |
| 479 | `Skipping already visited directory (circular symlink): ${currentDir}`, |
| 480 | ) |
| 481 | return |
| 482 | } |
| 483 | visitedDirs.add(dirKey) |
| 484 | } |
| 485 | } catch (error) { |
| 486 | const errorMessage = |
| 487 | error instanceof Error ? error.message : String(error) |
| 488 | logForDebugging(`Failed to stat directory ${currentDir}: ${errorMessage}`) |
| 489 | return |
| 490 | } |
| 491 | |
| 492 | try { |
| 493 | const entries = await readdir(currentDir, { withFileTypes: true }) |
| 494 | |
| 495 | for (const entry of entries) { |
| 496 | if (signal.aborted) { |
| 497 | break |
| 498 | } |
| 499 | |
| 500 | const fullPath = join(currentDir, entry.name) |
| 501 | |
| 502 | try { |
| 503 | // Handle symlinks: isFile() and isDirectory() return false for symlinks |
| 504 | if (entry.isSymbolicLink()) { |
| 505 | try { |
| 506 | const stats = await stat(fullPath) // stat() follows symlinks |
| 507 | if (stats.isDirectory()) { |
| 508 | await walk(fullPath) |
| 509 | } else if (stats.isFile() && entry.name.endsWith('.md')) { |
| 510 | files.push(fullPath) |
| 511 | } |
| 512 | } catch (error) { |
| 513 | const errorMessage = |
| 514 | error instanceof Error ? error.message : String(error) |
| 515 | logForDebugging( |
| 516 | `Failed to follow symlink ${fullPath}: ${errorMessage}`, |
| 517 | ) |
no test coverage detected