Skip to content

feat(worktree): add .worktreeinclude support for copying gitignored files to new worktrees - #25379

Closed
colus001 wants to merge 4 commits into
anomalyco:devfrom
colus001:dev
Closed

colus001 wants to merge 4 commits into
anomalyco:devfrom
colus001:dev

Conversation

@colus001

@colus001 colus001 commented May 2, 2026

Copy link
Copy Markdown

Issue for this PR

Closes #

Type of change

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

What does this PR do?

When creating a new git worktree (workspace), gitignored files like `.env` or `.env.local` are not checked out since they are untracked. This PR adds support for a `.worktreeinclude` file in the project root that lists which files should be copied into every new worktree — using the same `.gitignore` syntax already familiar to users (and compatible with Claude Code's own `.worktreeinclude` feature).

How it works:

  1. After `git reset --hard` populates the new worktree, `WorktreeInclude.apply` reads `.worktreeinclude` from the source repo root.
  2. It walks the source tree, matching entries against the patterns using the `ignore` npm package (already a dependency).
  3. Standard build/cache directories (`node_modules`, `dist`, `.next`, `target`, `vendor`, `bin`, `.turbo`, etc.) are automatically pruned during the walk so large monorepos don't incur a costly traversal. Users can override this by listing a directory explicitly in `.worktreeinclude`.
  4. Matched files, directories (copied recursively), and symlinks are copied to the new worktree directory. File modes are preserved.
  5. Any copy failure is logged as a warning and skipped — it never blocks the rest of the boot sequence.
  6. The same logic runs in `reset()` after `git clean -ffdx`, so gitignored files are restored when a worktree is reset.

Why `fs` and `pathSvc` are passed as arguments instead of yielded as services:

The desktop app calls `Worktree.create`, which forks `boot()` into a background fiber via `Effect.forkIn(scope)`. In that forked context the ambient Effect service registry no longer contains `effect/Path`, causing a `Service not found` die. Passing the already-resolved services from the outer layer closure avoids this entirely.

Example `.worktreeinclude`:
```
.env
.env.local
config/secrets.json
```

How did you verify your code works?

  • Added 8 unit tests in `packages/opencode/test/worktree/include.test.ts` covering: no-op when file is missing or empty, file copy with parent directory creation, recursive directory copy, negation patterns, hard-skip of `.git`, prune of `node_modules`/`dist`, and explicit user override of pruned directories.
  • Tested locally via `bun dev:desktop` against a real monorepo (`jjalbot`) with a `.worktreeinclude` containing `.env`. Confirmed `.env` is copied into the new worktree directory and the previous `Service not found: effect/Path` error no longer appears in the logs.
  • All tests pass: `bun test test/worktree/include.test.ts` → 8/8.
  • Full repo typecheck passes via the pre-push hook (`bun turbo typecheck`).

Screenshots / recordings

No UI changes.

Checklist

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

colus001 added 4 commits May 2, 2026 13:52
…iles

Reads .worktreeinclude from the project root (same syntax as .gitignore)
and copies matched files into newly created worktrees. Standard build
directories (node_modules, dist, .next, target, …) are pruned during
the walk so monorepos don't incur a large traversal cost. Users can
override the prune by explicitly listing a directory in .worktreeinclude.
Calls WorktreeInclude.apply in boot() after git reset --hard and in
reset() after git clean, so gitignored files (e.g. .env) are present
before startup scripts run. Passes fs and pathSvc explicitly to avoid
a Service-not-found die in the forked boot fiber used by the desktop.
- Move all diagnostic logs ("applied", "partial failure") inside
  WorktreeInclude.apply so the boot/reset call sites only swallow
  unexpected causes.
- Extract a single applyInclude(source, destination) helper in the
  Worktree layer, replacing two ~30-line copies in boot() and reset().
- Inline the trivial walk() wrapper into apply(); drop the redundant
  makeDirectory(parent) in copyOne's directory branch since
  copyDirectoryRecursive already handles it.
- Replace per-test makeTmp/write/exists boilerplate with a setup()
  helper that returns scoped temp dirs (auto-cleanup on test scope
  end) plus write/read/exists/apply closures bound to those dirs.
  Cleanup now runs even when assertions throw.
@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Automated PR Cleanup

Thank you for contributing to opencode.

Due to the high volume of PRs from users and AI agents, we periodically close older PRs using automated criteria so maintainers can focus review time on the most active and community-supported contributions.

This PR was closed because it matched the following cleanup criteria:

  • The PR was created more than 1 month ago
  • The PR had fewer than 2 positive reactions
  • Positive reactions are counted as thumbs-up, heart, celebration, or rocket reactions on the PR

PRs created within the last month are not affected by this cleanup.

If you believe this PR was closed incorrectly, or if you are still actively working on it, please leave a comment explaining why it should be reopened. A maintainer can review and reopen it if appropriate.

Thanks again for taking the time to contribute.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant