Skip to content

Commit b1b3ed3

Browse files
committed
refactor: use existing resolveSymLink function for AGENTS.md symlink support
- Remove duplicate inline symlink resolution logic - Reuse existing resolveSymLink function with MAX_DEPTH protection - Adapt loadAgentRulesFile to work with resolveSymLink's fileInfo interface - Fix test to properly mock fs.stat for resolved symlink targets - All tests pass (36/36)
1 parent 3ad7a2d commit b1b3ed3

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/core/prompts/sections/__tests__/custom-instructions.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,18 @@ describe("addCustomInstructions", () => {
687687
return Promise.reject({ code: "ENOENT" })
688688
})
689689

690+
// Mock stat to indicate the resolved target is a file
691+
statMock.mockImplementation((filePath: PathLike) => {
692+
const pathStr = filePath.toString()
693+
const normalizedPath = pathStr.replace(/\\/g, "/")
694+
if (normalizedPath.endsWith("actual-agents-file.md")) {
695+
return Promise.resolve({
696+
isFile: vi.fn().mockReturnValue(true),
697+
})
698+
}
699+
return Promise.reject({ code: "ENOENT" })
700+
})
701+
690702
// Mock readFile to return content from the resolved path
691703
readFileMock.mockImplementation((filePath: PathLike) => {
692704
const pathStr = filePath.toString()

src/core/prompts/sections/custom-instructions.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,19 +224,27 @@ async function loadAgentRulesFile(cwd: string): Promise<string> {
224224
const agentsPath = path.join(cwd, "AGENTS.md")
225225
let resolvedPath = agentsPath
226226

227-
// Check if the file is a symlink and resolve it if necessary
227+
// Check if AGENTS.md exists and handle symlinks
228228
try {
229229
const stats = await fs.lstat(agentsPath)
230230
if (stats.isSymbolicLink()) {
231-
const linkTarget = await fs.readlink(agentsPath)
232-
resolvedPath = path.resolve(path.dirname(agentsPath), linkTarget)
231+
// Create a temporary fileInfo array to use with resolveSymLink
232+
const fileInfo: Array<{ originalPath: string; resolvedPath: string }> = []
233+
234+
// Use the existing resolveSymLink function to handle symlink resolution
235+
await resolveSymLink(agentsPath, fileInfo, 0)
236+
237+
// Extract the resolved path from fileInfo
238+
if (fileInfo.length > 0) {
239+
resolvedPath = fileInfo[0].resolvedPath
240+
}
233241
}
234242
} catch (err) {
235-
// If we can't check symlink status, just use the original path
236-
resolvedPath = agentsPath
243+
// If lstat fails (file doesn't exist), return empty
244+
return ""
237245
}
238246

239-
// Read the file content
247+
// Read the content from the resolved path
240248
const content = await safeReadFile(resolvedPath)
241249
if (content) {
242250
return `# Agent Rules Standard (AGENTS.md):\n${content}`

0 commit comments

Comments
 (0)