Skip to content

fix(core): index symlinked files in the @ file picker - #59540

Open
swdevbali wants to merge 3 commits into
anomalyco:devfrom
getecosphere:index-file-symlinks
Open

swdevbali wants to merge 3 commits into
anomalyco:devfrom
getecosphere:index-file-symlinks

Conversation

@swdevbali

@swdevbali swdevbali commented Oct 27, 2026 •

Copy link
Copy Markdown

Issue for this PR

Closes #59539

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

As a solo developer I work across many git repositories, and I keep one "super" AGENTS.md (with its rule files) in a repository of its own, symlinked into the folder that holds all the other repos. OpenCode loads that symlinked file as instructions, but the @ picker never lists it: in a non-git folder the index is rg --files without --follow, and ripgrep drops every symlink in that mode. Details and repro in the issue.

The fix is deliberately narrower than #18291 / #29102: it indexes symlinks to files only and never enters symlinked directories.

How

  • FileSystemSearch ripgrep layer: the existing rg --files scan is unchanged. Afterwards, the directories that scan walked are listed through the Location environment (Environment.files.list, which tags entries with their own symlink type). For each directory that contains symlinks, one rg --files --follow --max-depth 1 runs in that directory. A link is added to the index, under its link path, only when that listing prints it.
    • The first scan lists every walked directory. The layer remembers which directories held visible links, so a refresh lists only the root, directories that are new since the last scan, and directories that held links (to catch links that were added, removed or now broken there). A directory found without links is not listed again. The known gap: a link added later to an existing directory that had none (other than the root) appears only after the next session.
    • This leaves ripgrep to decide, exactly as in the main scan, whether a link is visible: hidden names, .gitignore/.ignore, and excluded or unwalked directories (such as the home Protected excludes) are handled the same way. Directories under an exclude are never walked, so they are never listed.
    • Depth 1 means a symlinked directory is never descended into, so cycles are not possible. Broken links print nothing.
    • It goes through the environment's Files and spawner, so workspace-backed Locations keep working; nothing calls node:fs directly.
    • It is skipped when the main scan already hit its limit.
  • Ripgrep.find gains an optional maxDepth (--max-depth).
  • Cost: the first scan does one directory listing per walked directory, plus one rg process per directory that contains symlinks. On a non-git workspace with about 24k files in 4.3k directories, the first @ search went from about 300 ms to about 440 ms locally. A refresh no longer grows with the tree: on the same workspace it does 9 listings and 9 rg processes instead of 4,739 listings, and the symlink step takes about 55 ms instead of about 170-360 ms. It avoids a stat per directory on purpose: on workspace environments every stat or list is a process, so an mtime check would cost about as much as the listing it saves.

The fff layer (git repos) has the same gap: a symlink to a file is not indexed by @ff-labs/fff-bun, whether it is tracked (mode 120000) or untracked. That scanner is native code in an external package, so this PR does not change it.

How did you verify your code works?

  • New test in packages/core/test/filesystem/search.test.ts. A temp folder (not a git repo) holds a root file link AGENTS.md -> .agents/AGENTS.md, a nested file link, a link that .ignore excludes, a symlinked directory, a broken link and a loop -> . cycle. Only AGENTS.md and app/AGENTS.md are added, nothing appears under linked/ or loop/, and the scan finishes. The test fails on dev (it gets [] for AGENTS) and passes with this change. It is skipped on Windows, because creating symlinks there needs elevated privileges.
  • A second test counts files.list calls through a wrapped environment: after the first scan, a refresh lists only the root, a new directory and the directory that already held a link, and finds the links added to all three; the unchanged link-free directories are not listed again (this test fails on the first commit, which re-listed app and app/sub).
  • bun test test/filesystem/search.test.ts: 11 pass. bun test test/filesystem: 66 pass, 0 fail. bun run check (lint + typecheck) at the repo root passes.
  • Manual check: a real non-git workspace with a root AGENTS.md symlink now lists it in the @ picker results.

Visual

Not included: the change is a missing entry in the @ picker results.

Screenshots / recordings

Not a UI change.

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Added by the opencode bot (AI-generated).

Related: #53958, #55937

@github-actions github-actions Bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Oct 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works as described. In a non-git project, @AGENTS now lists the root AGENTS.md symlink next to app-a/AGENTS.md; on the base branch only the latter appears. The new test fails on the base branch and passes here, and typecheck:tsc is clean. I also checked that the depth-1 rg --follow listing run in a subdirectory still honours ignore rules from parent folders, so links excluded by a parent .ignore stay out of the index.

The public API is unchanged: Ripgrep.FindInput.maxDepth is a new optional field on an internal interface.

One inline note on refresh cost in large home folders. It doesn't block this change.

Related: #53958, #55937

Before

After

Comment thread packages/core/src/filesystem/search.ts Outdated
// Look for links only in the directories this scan walked, and let a depth-1 `--follow` listing of
// those directories decide which links are visible files. It applies the scan's ignore and hidden
// rules, and a symlinked directory or a broken link yields nothing, so no link is ever traversed.
if (next.files.size < limit)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This runs a files.list for every directory found by the main scan, on every refresh (at most once per 10s while searching). In the home-folder case, where up to 100k files are indexed, that can mean thousands of directory listings each time just to find symlinks, even though most folders have none (see #55937 for how expensive home folders already are). Since later refreshes run in the background it's not a correctness problem. Still, it may be worth limiting this to the first scan, or only re-listing directories whose mtime changed, so the steady-state cost doesn't grow with the size of the tree.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, thanks. I changed it so only the first scan lists every walked directory. The layer now remembers which directories held visible symlinked files, and a refresh lists only the root, directories that are new since the last scan, and the ones that held links. I skipped the mtime idea because on workspace environments each stat is a process too, so it would cost about the same as the listing it replaces. On a non-git folder with 4.3k directories, a refresh now does 9 listings instead of 4,739, and the symlink step drops from about 170-360 ms to about 55 ms. The trade-off is that a link added later to an existing folder without links (other than the root) shows up only in the next session; there is a test for the refresh behaviour.

The ripgrep-backed index (non-git, home and workspace Locations) runs rg --files without --follow, which omits every symlink, so a workspace AGENTS.md linked from a rules repo never showed up in the @ picker.

After the scan, list the directories it walked through the Location environment and, for each one that holds symlinks, run a depth-1 rg --files --follow there. Only links that resolve to regular files are added, under their link path, with the same ignore and hidden rules. Symlinked directories are never traversed and broken links are skipped.
…symlink index

A refresh of the ripgrep-backed index listed every directory the scan walked to find symlinks, so its cost grew with the tree (thousands of listings per refresh in a home folder).

Remember which listed directories held visible symlinked files. A refresh lists the root, directories that are new since the last scan and directories that held links; a directory found without links is not listed again. This keeps per-directory I/O off the steady state, which matters most on workspace environments where every stat or list is a process.
@swdevbali
swdevbali force-pushed the index-file-symlinks branch from 1ff80b7 to 1133ce6 Compare October 27, 2026 10:26

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still works on the new head: running the #59539 repro in a non-git project, @AGENTS now offers the root AGENTS.md symlink, while the base branch doesn't (screenshots below). search.test.ts passed 11/11 in three runs and typecheck:tsc is clean.

The earlier point about refresh cost is addressed: a refresh now re-lists only the root, new folders and folders that had visible links last time.

The public API is unchanged: Ripgrep.FindInput.maxDepth is a new optional field on an internal interface.

One small inline note about folders whose symlinks are all broken or ignored. It doesn't block the change.

Before

After

Comment thread packages/core/src/filesystem/search.ts Outdated
}),
})
files.forEach(add)
found.set(directory, files)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a folder's symlinks were all broken or ignored, files is empty, so the folder is saved exactly like one with no symlinks and is skipped on every later refresh. If such a link starts working later (for example, its target is cloned or created after OpenCode starts), it won't show up until restart, even though this folder does contain symlinks. Saving "had symlinks" separately from "had visible links" (e.g. skip only when links.size === 0) would keep the saving for folders without any symlinks and close this gap.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6c6a29d: a folder now stays on the refresh list when it holds any symlink, broken or ignored ones included, so a link that starts resolving later shows up on the next refresh. Folders without symlinks are still skipped. The test now has a broken link whose target is created after the first scan.

@opencode-agent opencode-agent Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The broken or ignored symlink point is addressed. A folder is now skipped on refresh only when it had no symlinks at all, and the new test case covers a broken link that starts resolving later. The #59539 repro still passes on this head: @AGENTS offers the root AGENTS.md symlink here but not on the base branch (screenshots below). search.test.ts passed 11/11 in three runs and typecheck:tsc is clean.

The public API is unchanged: Ripgrep.FindInput.maxDepth is a new optional field on an internal interface.

No further concerns.

Before

After

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@ file picker skips symlinked files, e.g. a workspace AGENTS.md shared from a rules repo

1 participant