From 53a70d854582bcab77e770d56e75b780b2ed8aeb Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Thu, 1 Oct 2026 11:23:30 -0400 Subject: [PATCH] fix(files): skip broken symlinks during directory scans --- backend/src/services/file-operations.ts | 16 ++++-- backend/test/services/skills.test.ts | 68 ++++++++++++++++++++++++- 2 files changed, 80 insertions(+), 4 deletions(-) diff --git a/backend/src/services/file-operations.ts b/backend/src/services/file-operations.ts index 2be0c07b..cb566c49 100644 --- a/backend/src/services/file-operations.ts +++ b/backend/src/services/file-operations.ts @@ -135,13 +135,23 @@ export async function listDirectory(dirPath: string): Promise> + try { + stats = await fs.stat(entryPath) + } catch (error) { + const code = (error as NodeJS.ErrnoException).code + if (code === 'ENOENT' || code === 'ENOTDIR' || code === 'ELOOP') { + logger.warn(`Skipping unresolvable directory entry ${entryPath}:`, error) + continue + } + throw error + } result.push({ name: entry.name, path: entryPath, - isDirectory: entry.isDirectory(), - size: entry.isDirectory() ? 0 : stats.size, + isDirectory: stats.isDirectory(), + size: stats.isDirectory() ? 0 : stats.size, lastModified: stats.mtime }) } diff --git a/backend/test/services/skills.test.ts b/backend/test/services/skills.test.ts index bd5117e0..4ab5ae3e 100644 --- a/backend/test/services/skills.test.ts +++ b/backend/test/services/skills.test.ts @@ -1,7 +1,8 @@ import { describe, expect, test, beforeEach, afterEach, vi } from 'vitest' import { join } from 'path' import { tmpdir } from 'os' -import { mkdir, mkdtemp, rm, readFile, writeFile } from 'fs/promises' +import { mkdir, mkdtemp, rm, readFile, writeFile, symlink } from 'fs/promises' +import { promises as fs } from 'fs' import type { OpenCodeClient } from '../../src/services/opencode/client' import type { Repo } from '../../src/types/repo' @@ -341,6 +342,71 @@ describe('SkillService', () => { } }) + test('keeps healthy skills when dangling symlinks sit beside them', async () => { + const { listManagedSkills } = await import('../../src/services/skills') + const projectPath = join(tempDir, 'project-symlinks') + const skillsRoot = join(projectPath, '.opencode', 'skills') + + const regularDir = join(skillsRoot, 'regular-skill') + await mkdir(regularDir, { recursive: true }) + await writeFile( + join(regularDir, 'SKILL.md'), + '---\nname: regular-skill\ndescription: Regular skill\n---\nBody', + ) + + const symlinkTargetDir = join(tempDir, 'symlink-target') + await mkdir(symlinkTargetDir, { recursive: true }) + await writeFile( + join(symlinkTargetDir, 'SKILL.md'), + '---\nname: linked-skill\ndescription: Linked skill\n---\nBody', + ) + await symlink(symlinkTargetDir, join(skillsRoot, 'linked-skill'), 'dir') + + await symlink(join(tempDir, 'missing-target'), join(skillsRoot, 'dangling-skill'), 'dir') + + const plainFile = join(tempDir, 'plain-file.txt') + await writeFile(plainFile, 'not a directory') + await symlink(join(plainFile, 'child'), join(skillsRoot, 'notdir-skill'), 'dir') + + await symlink(join(skillsRoot, 'loop-b'), join(skillsRoot, 'loop-a'), 'dir') + await symlink(join(skillsRoot, 'loop-a'), join(skillsRoot, 'loop-b'), 'dir') + + const skills = await listManagedSkills(mockDb, createMockClient([]), undefined, projectPath) + + expect(skills).toContainEqual(expect.objectContaining({ + name: 'regular-skill', + description: 'Regular skill', + scope: 'project', + })) + expect(skills).toContainEqual(expect.objectContaining({ + name: 'linked-skill', + description: 'Linked skill', + scope: 'project', + })) + expect(skills.map(s => s.name)).not.toContain('dangling-skill') + }) + + test('listDirectory rejects when the root directory is missing', async () => { + const { listDirectory } = await import('../../src/services/file-operations') + await expect(listDirectory(join(tempDir, 'missing-root'))).rejects.toThrow('Failed to list directory') + }) + + test('listDirectory propagates unexpected stat errors', async () => { + const { listDirectory } = await import('../../src/services/file-operations') + const dir = join(tempDir, 'stat-error-root') + await mkdir(dir, { recursive: true }) + await writeFile(join(dir, 'entry.txt'), 'content') + + const statSpy = vi.spyOn(fs, 'stat').mockRejectedValue( + Object.assign(new Error('EACCES: permission denied'), { code: 'EACCES' }), + ) + try { + await expect(listDirectory(dir)).rejects.toThrow('Failed to list directory') + } finally { + statSpy.mockRestore() + } + }) + test('preserves body content containing --- (horizontal rules)', async () => { const { createSkill, getSkill, deleteSkill } = await import('../../src/services/skills') const name = `hr-test-${Date.now()}`