Skip to content

Commit 79d3218

Browse files
committed
Ignore symbolic links in workspace file traversal and token calculations
- symbolic links should be ignored
1 parent 9cb3041 commit 79d3218

3 files changed

Lines changed: 71 additions & 137 deletions

File tree

‎apps/editor/src/context/providers/open-editors/open-editors-provider.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,7 @@ export class OpenEditorsProvider
460460
([file_path, state]) =>
461461
state === vscode.TreeItemCheckboxState.Checked &&
462462
fs.existsSync(file_path) &&
463-
(fs.lstatSync(file_path).isFile() ||
464-
fs.lstatSync(file_path).isSymbolicLink())
463+
fs.lstatSync(file_path).isFile()
465464
)
466465
.map(([path, _]) => path)
467466
}

‎apps/editor/src/context/providers/workspace/modules/token-calculator.ts‎

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ export class TokenCalculator implements vscode.Disposable {
426426
let total_shrink_tokens = 0
427427

428428
for (const entry of entries) {
429+
if (entry.isSymbolicLink()) continue
430+
429431
const full_path = path.join(dir_path, entry.name)
430432

431433
const file_workspace_root =
@@ -448,31 +450,13 @@ export class TokenCalculator implements vscode.Disposable {
448450
continue
449451
}
450452

451-
let is_directory = entry.isDirectory()
452-
const is_symbolic_link = entry.isSymbolicLink()
453-
let is_broken_link = false
454-
455-
// Resolve symbolic link to determine if it points to a directory
456-
if (is_symbolic_link) {
457-
try {
458-
const stats = await fs.promises.stat(full_path)
459-
is_directory = stats.isDirectory()
460-
} catch {
461-
// The symlink is broken
462-
is_broken_link = true
463-
}
464-
}
465-
466-
if (is_directory && !is_broken_link) {
467-
// Recurse into subdirectory (including resolved symlinks that are directories)
453+
if (entry.isDirectory()) {
454+
// Recurse into subdirectory
468455
const counts = await this.calculate_directory_tokens(full_path)
469456
total_tokens += counts.total
470457
total_shrink_tokens += counts.shrink
471-
} else if (
472-
entry.isFile() ||
473-
(is_symbolic_link && !is_broken_link && !is_directory)
474-
) {
475-
// Add file tokens (including resolved symlinks that are files)
458+
} else if (entry.isFile()) {
459+
// Add file tokens
476460
const counts = await this.calculate_file_tokens(full_path)
477461
total_tokens += counts.total
478462
total_shrink_tokens += counts.shrink
@@ -538,6 +522,8 @@ export class TokenCalculator implements vscode.Disposable {
538522
withFileTypes: true
539523
})
540524
for (const entry of entries) {
525+
if (entry.isSymbolicLink()) continue
526+
541527
const full_path = path.join(dir_path, entry.name)
542528

543529
const file_workspace_root =
@@ -559,18 +545,7 @@ export class TokenCalculator implements vscode.Disposable {
559545

560546
const checkbox_state = this._provider.get_check_state(full_path)
561547

562-
let entry_is_directory = entry.isDirectory()
563-
if (entry.isSymbolicLink()) {
564-
try {
565-
entry_is_directory = (
566-
await fs.promises.stat(full_path)
567-
).isDirectory()
568-
} catch {
569-
continue /* broken symlink */
570-
}
571-
}
572-
573-
if (entry_is_directory) {
548+
if (entry.isDirectory()) {
574549
if (checkbox_state === vscode.TreeItemCheckboxState.Checked) {
575550
const counts = await this.calculate_directory_tokens(full_path)
576551
selected_tokens += counts.total
@@ -581,7 +556,7 @@ export class TokenCalculator implements vscode.Disposable {
581556
selected_tokens += counts.total
582557
selected_shrink_tokens += counts.shrink
583558
}
584-
} else {
559+
} else if (entry.isFile()) {
585560
if (checkbox_state === vscode.TreeItemCheckboxState.Checked) {
586561
const counts = await this.calculate_file_tokens(full_path)
587562
selected_tokens += counts.total
@@ -620,7 +595,10 @@ export class TokenCalculator implements vscode.Disposable {
620595
continue
621596
}
622597

623-
if (fs.statSync(file_path).isFile()) {
598+
const stat = fs.lstatSync(file_path)
599+
if (stat.isSymbolicLink()) continue
600+
601+
if (stat.isFile()) {
624602
if (this._file_token_counts.has(file_path)) {
625603
result.total += this._file_token_counts.get(file_path)!
626604
result.shrink += this._file_shrink_token_counts.get(file_path)!

‎apps/editor/src/context/providers/workspace/workspace-provider.ts‎

Lines changed: 56 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,14 +1039,18 @@ export class WorkspaceProvider
10391039
})
10401040

10411041
dir_entries.sort((a, b) => {
1042-
const a_is_dir = a.isDirectory() || a.isSymbolicLink()
1043-
const b_is_dir = b.isDirectory() || b.isSymbolicLink()
1042+
const a_is_dir = a.isDirectory()
1043+
const b_is_dir = b.isDirectory()
10441044
if (a_is_dir && !b_is_dir) return -1
10451045
if (!a_is_dir && b_is_dir) return 1
10461046
return natural_sort(a.name, b.name)
10471047
})
10481048

10491049
for (const entry of dir_entries) {
1050+
if (entry.isSymbolicLink()) {
1051+
continue
1052+
}
1053+
10501054
const full_path = path.join(dir_path, entry.name)
10511055

10521056
// Exclude nested workspace folders from appearing in parent workspace trees
@@ -1062,20 +1066,9 @@ export class WorkspaceProvider
10621066
const is_partially_checked =
10631067
this._partially_checked_dirs.has(full_path)
10641068

1065-
let is_directory_for_check = entry.isDirectory()
1066-
if (entry.isSymbolicLink()) {
1067-
try {
1068-
is_directory_for_check = (
1069-
await fs.promises.stat(full_path)
1070-
).isDirectory()
1071-
} catch {
1072-
// broken symlink, will be skipped later
1073-
}
1074-
}
1075-
10761069
if (
10771070
state != vscode.TreeItemCheckboxState.Checked &&
1078-
!(is_directory_for_check && is_partially_checked)
1071+
!(entry.isDirectory() && is_partially_checked)
10791072
) {
10801073
continue
10811074
}
@@ -1094,24 +1087,7 @@ export class WorkspaceProvider
10941087
}
10951088

10961089
const uri = vscode.Uri.file(full_path)
1097-
let is_directory = entry.isDirectory()
1098-
const is_symbolic_link = entry.isSymbolicLink()
1099-
let is_broken_link = false
1100-
1101-
if (is_symbolic_link) {
1102-
try {
1103-
const stats = await fs.promises.stat(full_path)
1104-
is_directory = stats.isDirectory()
1105-
} catch (err) {
1106-
// The symlink is broken
1107-
is_broken_link = true
1108-
}
1109-
}
1110-
1111-
if (is_broken_link) {
1112-
continue
1113-
}
1114-
1090+
const is_directory = entry.isDirectory()
11151091
const key = full_path
11161092

11171093
let checkbox_state = this._checked_items.get(key)
@@ -1151,7 +1127,7 @@ export class WorkspaceProvider
11511127
: vscode.TreeItemCollapsibleState.None,
11521128
is_directory,
11531129
this._is_no_context_mode ? undefined : checkbox_state,
1154-
is_symbolic_link,
1130+
false,
11551131
false,
11561132
tokens.total,
11571133
tokens.shrink,
@@ -1248,6 +1224,10 @@ export class WorkspaceProvider
12481224
let has_non_ignored_child = false
12491225

12501226
for (const entry of dir_entries) {
1227+
if (entry.isSymbolicLink()) {
1228+
continue
1229+
}
1230+
12511231
const sibling_path = path.join(dir_path, entry.name)
12521232

12531233
const file_workspace_root =
@@ -1357,6 +1337,10 @@ export class WorkspaceProvider
13571337
})
13581338

13591339
for (const entry of dir_entries) {
1340+
if (entry.isSymbolicLink()) {
1341+
continue
1342+
}
1343+
13601344
const full_path = path.join(dir_path, entry.name)
13611345

13621346
const file_workspace_root = this.get_workspace_root_for_file(full_path)
@@ -1387,21 +1371,7 @@ export class WorkspaceProvider
13871371
this._checked_timestamps.delete(full_path)
13881372
}
13891373

1390-
let is_directory = entry.isDirectory()
1391-
const is_symbolic_link = entry.isSymbolicLink()
1392-
let is_broken_link = false
1393-
1394-
if (is_symbolic_link) {
1395-
try {
1396-
const stats = await fs.promises.stat(full_path)
1397-
is_directory = stats.isDirectory()
1398-
} catch {
1399-
// The symlink is broken
1400-
is_broken_link = true
1401-
}
1402-
}
1403-
1404-
if (is_directory && !is_broken_link) {
1374+
if (entry.isDirectory()) {
14051375
await this._update_directory_check_state(full_path, state, false)
14061376
}
14071377
}
@@ -1426,8 +1396,7 @@ export class WorkspaceProvider
14261396
([file_path, state]) =>
14271397
state == vscode.TreeItemCheckboxState.Checked &&
14281398
fs.existsSync(file_path) &&
1429-
(fs.lstatSync(file_path).isFile() ||
1430-
fs.lstatSync(file_path).isSymbolicLink()) &&
1399+
fs.lstatSync(file_path).isFile() &&
14311400
(() => {
14321401
const workspace_root = this.get_workspace_root_for_file(file_path)
14331402
return workspace_root
@@ -1461,30 +1430,39 @@ export class WorkspaceProvider
14611430
for (const file_path of file_paths) {
14621431
if (!fs.existsSync(file_path)) continue
14631432

1464-
const workspace_root = this.get_workspace_root_for_file(file_path)
1465-
if (!workspace_root) continue
1433+
try {
1434+
const stats = fs.lstatSync(file_path)
1435+
if (stats.isSymbolicLink()) continue
14661436

1467-
const stats = fs.lstatSync(file_path)
1468-
const is_directory = stats.isDirectory()
1437+
const workspace_root = this.get_workspace_root_for_file(file_path)
1438+
if (!workspace_root) continue
14691439

1470-
const relative_path = path.relative(workspace_root, file_path)
1471-
if (
1472-
this.is_excluded(is_directory ? relative_path + '/' : relative_path)
1473-
) {
1474-
continue
1475-
}
1440+
const is_directory = stats.isDirectory()
14761441

1477-
if (is_directory) {
1478-
this._checked_items.set(file_path, vscode.TreeItemCheckboxState.Checked)
1479-
await this._update_directory_check_state(
1480-
file_path,
1481-
vscode.TreeItemCheckboxState.Checked,
1482-
false
1483-
)
1484-
} else {
1485-
if (!this.is_ignored_by_patterns(file_path)) {
1486-
all_files_to_check.push(file_path)
1442+
const relative_path = path.relative(workspace_root, file_path)
1443+
if (
1444+
this.is_excluded(is_directory ? relative_path + '/' : relative_path)
1445+
) {
1446+
continue
1447+
}
1448+
1449+
if (is_directory) {
1450+
this._checked_items.set(
1451+
file_path,
1452+
vscode.TreeItemCheckboxState.Checked
1453+
)
1454+
await this._update_directory_check_state(
1455+
file_path,
1456+
vscode.TreeItemCheckboxState.Checked,
1457+
false
1458+
)
1459+
} else {
1460+
if (!this.is_ignored_by_patterns(file_path)) {
1461+
all_files_to_check.push(file_path)
1462+
}
14871463
}
1464+
} catch (e) {
1465+
// Ignore file access errors
14881466
}
14891467
}
14901468

@@ -1526,21 +1504,13 @@ export class WorkspaceProvider
15261504
})
15271505

15281506
for (const entry of entries) {
1507+
if (entry.isSymbolicLink()) continue
1508+
15291509
if (entry.name == '.git' || entry.name == 'node_modules') continue
15301510

15311511
const full_path = path.join(dir_path, entry.name)
15321512

1533-
let is_directory = entry.isDirectory()
1534-
if (entry.isSymbolicLink()) {
1535-
try {
1536-
const stats = await fs.promises.stat(full_path)
1537-
is_directory = stats.isDirectory()
1538-
} catch {
1539-
continue
1540-
}
1541-
}
1542-
1543-
if (is_directory) {
1513+
if (entry.isDirectory()) {
15441514
await walk(full_path)
15451515
} else if (
15461516
['.gitignore', '.cursorignore', '.codeiumignore'].includes(
@@ -1736,6 +1706,10 @@ export class WorkspaceProvider
17361706
})
17371707

17381708
for (const entry of entries) {
1709+
if (entry.isSymbolicLink()) {
1710+
continue
1711+
}
1712+
17391713
const full_path = path.join(dir_path, entry.name)
17401714

17411715
const file_workspace_root =
@@ -1754,24 +1728,7 @@ export class WorkspaceProvider
17541728
continue
17551729
}
17561730

1757-
let is_directory = entry.isDirectory()
1758-
const is_symbolic_link = entry.isSymbolicLink()
1759-
let is_broken_link = false
1760-
1761-
if (is_symbolic_link) {
1762-
try {
1763-
const stats = await fs.promises.stat(full_path)
1764-
is_directory = stats.isDirectory()
1765-
} catch {
1766-
is_broken_link = true
1767-
}
1768-
}
1769-
1770-
if (is_broken_link) {
1771-
continue
1772-
}
1773-
1774-
if (is_directory) {
1731+
if (entry.isDirectory()) {
17751732
await walk(full_path)
17761733
} else if (!this.is_ignored_by_patterns(full_path)) {
17771734
files.push(full_path)

0 commit comments

Comments
 (0)