fix(server): file links relative to a gitignored git worktree open the file #34831
Mergedjuliusmarmingemerged 0 commits into
main fromrefs/pull/34831/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- @@ -275,0 +294,21 @@294+ ) {295+ const relativePath = query.trim().replaceAll("\\", "/");296+ if (297+ !relativePath.includes("/") ||298+ relativePath.endsWith("/") ||299+ path.isAbsolute(relativePath) ||300+ isWindowsAbsolutePath(relativePath) ||301+ relativePath.split("/").some((segment) => segment === "..") ||302+ indexed.some(303+ (entry) =>304+ entry.kind === "file" &&305+ (entry.path === relativePath || entry.path.endsWith(`/${relativePath}`)),306+ )307+ ) {308+ return [];309+ }310+ const listed = yield* vcsProcess311+ .run({312+ operation: "WorkspaceEntries.listWorktrees",313+ command: "git",314+ args: ["worktree", "list", "--porcelain"],
- @@ -273,3 +275,21 @@275+ normalizedCwd,276+ input.query,277+ result.entries,278+ );279+ if (worktreeFiles.length === 0) return result;280+ const entries = [...worktreeFiles, ...result.entries];281+ return {282+ entries: entries.slice(0, input.limit),283+ truncated: result.truncated || entries.length > input.limit,284+ };273285 },274286 );275287288+ // The index skips gitignored folders, where agents often put worktrees (`.worktrees/x`).289+ // A path relative to such a worktree is found only when it names an existing file exactly.290+ const findNestedWorktreeFiles = Effect.fn("WorkspaceEntries.findNestedWorktreeFiles")(function* (291+ cwd: string,292+ query: string,293+ indexed: ReadonlyArray<ProjectEntry>,294+ ) {295+ const relativePath = query.trim().replaceAll("\\", "/");@@ -273,3 +285,21 @@273285 },274286 );275287288+ // The index skips gitignored folders, where agents often put worktrees (`.worktrees/x`).289+ // A path relative to such a worktree is found only when it names an existing file exactly.290+ const findNestedWorktreeFiles = Effect.fn("WorkspaceEntries.findNestedWorktreeFiles")(function* (291+ cwd: string,292+ query: string,293+ indexed: ReadonlyArray<ProjectEntry>,294+ ) {295+ const relativePath = query.trim().replaceAll("\\", "/");296+ if (297+ !relativePath.includes("/") ||298+ relativePath.endsWith("/") ||299+ path.isAbsolute(relativePath) ||300+ isWindowsAbsolutePath(relativePath) ||301+ relativePath.split("/").some((segment) => segment === "..") ||302+ indexed.some(303+ (entry) =>304+ entry.kind === "file" &&305+ (entry.path === relativePath || entry.path.endsWith(`/${relativePath}`)),@@ -275,0 +321,21 @@321+ const realCwd = yield* Effect.promise(() => NodeFSP.realpath(cwd).catch(() => cwd));322+ const worktreeRoots = listed.stdout.split(/\n\s*\n/).flatMap((record) => {323+ const lines = record.split("\n");324+ const worktree = lines.find((line) => line.startsWith("worktree "));325+ // A `prunable` record's directory is gone; another checkout may now sit there.326+ if (worktree === undefined || lines.some((line) => line.startsWith("prunable"))) return [];327+ const root = worktree.slice("worktree ".length);328+ const relativeRoot = path.relative(realCwd, root);329+ return relativeRoot === "" ||330+ relativeRoot === ".." ||331+ relativeRoot.startsWith(`..${path.sep}`) ||332+ path.isAbsolute(relativeRoot)333+ ? []334+ : [relativeRoot.split(path.sep).join("/")];335+ });336+ const matches = yield* Effect.promise(() =>337+ Promise.all(338+ worktreeRoots.map(async (relativeRoot) => {339+ const candidate = `${relativeRoot}/${relativePath}`;340+ const stat = await NodeFSP.stat(path.join(realCwd, candidate)).catch(() => null);341+ return stat?.isFile() ? candidate : null;
juliusmarminge merged this3c68642
juliusmarminge closed this
Sign in to join this conversation.


macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed