Repository navigation
fix(host-service): workspace delete no longer times out on node_modules heavy worktrees #7654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sahiljagtap08
wants to merge
3
commits into
ParadevAI:main
Choose a base branch
from
sahiljagtap08:fix/worktree-remove-app-side-delete
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ec61910
fix(host-service): delete worktree trees in the app, let git only unr…
sahiljagtap08 1bdc59a
fix(host-service): app-side worktree delete only touches what git wou…
sahiljagtap08 346847c
fix(host-service): refuse a symlinked stored worktree path outright
sahiljagtap08 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
packages/host-service/src/workers/tasks/git-worktree-remove.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import { afterEach, beforeEach, describe, expect, test } from "bun:test"; | ||
| import { execFileSync } from "node:child_process"; | ||
| import * as fs from "node:fs"; | ||
| import * as os from "node:os"; | ||
| import * as path from "node:path"; | ||
| import { gitWorktreeRemoveTask } from "./git"; | ||
|
|
||
| /** | ||
| * Worktree removal used to hand the whole recursive delete to git, whose | ||
| * single threaded walk cannot finish a node_modules heavy tree inside the | ||
| * task budget (GH #6887). The task now deletes the tree itself first, under | ||
| * the same managed root guard the destroy saga uses, and calls git only to | ||
| * unregister. These tests run the real handler against real repos. | ||
| */ | ||
| describe("gitWorktreeRemoveTask", () => { | ||
| let tmp: string; | ||
| let repoPath: string; | ||
| let worktreesBase: string; | ||
| const project = { id: "p-1", name: "demo" }; | ||
| const gitEnv = { | ||
| PATH: process.env.PATH ?? "", | ||
| HOME: process.env.HOME ?? "", | ||
| GIT_OPTIONAL_LOCKS: "0", | ||
| LC_ALL: "C", | ||
| }; | ||
|
|
||
| function git(args: string[], cwd: string): void { | ||
| execFileSync("git", args, { cwd, env: { ...process.env, ...gitEnv } }); | ||
| } | ||
|
|
||
| function addWorktree(worktreePath: string, branch: string): void { | ||
| git(["worktree", "add", "-b", branch, worktreePath], repoPath); | ||
| const nested = path.join(worktreePath, "node_modules", "dep", "sub"); | ||
| fs.mkdirSync(nested, { recursive: true }); | ||
| fs.writeFileSync(path.join(nested, "index.js"), "module.exports = 1;\n"); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| tmp = fs.mkdtempSync(path.join(os.tmpdir(), "worktree-remove-task-")); | ||
| repoPath = path.join(tmp, "repo"); | ||
| worktreesBase = path.join(tmp, "worktrees"); | ||
| fs.mkdirSync(repoPath, { recursive: true }); | ||
| git(["init"], repoPath); | ||
| git(["config", "user.email", "test@example.com"], repoPath); | ||
| git(["config", "user.name", "test"], repoPath); | ||
| git(["config", "commit.gpgsign", "false"], repoPath); | ||
| fs.writeFileSync(path.join(repoPath, "a.txt"), "hello\n"); | ||
| git(["add", "."], repoPath); | ||
| git(["commit", "-m", "initial"], repoPath); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| fs.rmSync(tmp, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| test("deletes the tree itself when the worktree is inside the managed root", async () => { | ||
| const worktreePath = path.join(worktreesBase, "demo", "wt-1"); | ||
| addWorktree(worktreePath, "feature/inside"); | ||
|
|
||
| const result = await gitWorktreeRemoveTask.handler( | ||
| { | ||
| repoPath, | ||
| worktreePath, | ||
| gitEnv, | ||
| project, | ||
| worktreeBaseDir: worktreesBase, | ||
| }, | ||
| undefined, | ||
| ); | ||
|
|
||
| expect(result.removedByApp).toBe(true); | ||
| expect(result.stillRegistered).toBe(false); | ||
| expect(fs.existsSync(worktreePath)).toBe(false); | ||
| }); | ||
|
|
||
| test("leaves the delete to git when the path is outside the managed root", async () => { | ||
| const worktreePath = path.join(tmp, "elsewhere", "wt-2"); | ||
| addWorktree(worktreePath, "feature/outside"); | ||
|
|
||
| const result = await gitWorktreeRemoveTask.handler( | ||
| { | ||
| repoPath, | ||
| worktreePath, | ||
| gitEnv, | ||
| project, | ||
| worktreeBaseDir: worktreesBase, | ||
| }, | ||
| undefined, | ||
| ); | ||
|
|
||
| expect(result.removedByApp).toBe(false); | ||
| expect(result.stillRegistered).toBe(false); | ||
| expect(fs.existsSync(worktreePath)).toBe(false); | ||
| }); | ||
|
|
||
| test("refuses a symlinked stored path instead of removing what it resolves to", async () => { | ||
| const realWorktree = path.join(worktreesBase, "demo", "wt-real"); | ||
| addWorktree(realWorktree, "feature/real"); | ||
| const linkPath = path.join(worktreesBase, "demo", "wt-link"); | ||
| fs.symlinkSync(realWorktree, linkPath); | ||
|
|
||
| const result = await gitWorktreeRemoveTask.handler( | ||
| { | ||
| repoPath, | ||
| worktreePath: linkPath, | ||
| gitEnv, | ||
| project, | ||
| worktreeBaseDir: worktreesBase, | ||
| }, | ||
| undefined, | ||
| ); | ||
|
|
||
| // Neither the app nor git may delete the neighboring worktree the | ||
| // link resolves to; the caller sees a blocked removal with a reason. | ||
| expect(result.removedByApp).toBe(false); | ||
| expect(result.stillRegistered).toBe(true); | ||
| expect(result.removeError).toContain("symbolic link"); | ||
| expect(fs.existsSync(path.join(realWorktree, "node_modules"))).toBe(true); | ||
| }); | ||
|
|
||
| test("leaves an unregistered folder inside the managed root to git", async () => { | ||
| const impostor = path.join(worktreesBase, "demo", "not-a-worktree"); | ||
| fs.mkdirSync(impostor, { recursive: true }); | ||
| fs.writeFileSync(path.join(impostor, "precious.txt"), "do not delete\n"); | ||
|
|
||
| const result = await gitWorktreeRemoveTask.handler( | ||
| { | ||
| repoPath, | ||
| worktreePath: impostor, | ||
| gitEnv, | ||
| project, | ||
| worktreeBaseDir: worktreesBase, | ||
| }, | ||
| undefined, | ||
| ); | ||
|
|
||
| expect(result.removedByApp).toBe(false); | ||
| expect(fs.existsSync(path.join(impostor, "precious.txt"))).toBe(true); | ||
| }); | ||
|
|
||
| test("unregisters a worktree whose directory is already gone", async () => { | ||
| const worktreePath = path.join(worktreesBase, "demo", "wt-3"); | ||
| addWorktree(worktreePath, "feature/gone"); | ||
| fs.rmSync(worktreePath, { recursive: true, force: true }); | ||
|
|
||
| const result = await gitWorktreeRemoveTask.handler( | ||
| { | ||
| repoPath, | ||
| worktreePath, | ||
| gitEnv, | ||
| project, | ||
| worktreeBaseDir: worktreesBase, | ||
| }, | ||
| undefined, | ||
| ); | ||
|
|
||
| expect(result.stillRegistered).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win
Propagate unexpected errors from the symlink check.
If
lstatSync(worktreePath)throwsEACCES, this catch continues without establishing whether the stored path is a symlink. If access recovers before normalization, a symlink to another registered worktree inside the managed root can pass the Git-list check and cause deletion of that worktree. Ignore only expected absence errors such asENOENTandENOTDIR; propagate other errors before either removal path runs. (nodejs.org)Based on learnings: propagate filesystem existence-check errors other than expected absence errors.
🤖 Prompt for AI Agents
Source: Learnings