worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Node.js symlink recipe in copy-ignored docs lets a merge replace the primary worktree's node_modules with a self-link

Open worktrunk-bot opened this issue 1 week ago • 1 comments

The Node.js recipe in wt step copy-ignored --help (and the generated step docs) symlinks each worktree's node_modules to the primary worktree's. With the common .gitignore pattern node_modules/, the symlink is untracked rather than ignored: a trailing-slash pattern matches only a directory. wt step commit stages everything by default, so it commits the symlink, and wt merge then checks it out in the primary worktree, where Git replaces the real (ignored) node_modules directory with a symlink pointing at itself. Every worktree's link breaks and the installed dependencies are gone.

Reported by armelhbobdad while evaluating Worktrunk (commit): "a merge replaced the primary worktree's real node_modules with a symlink pointing at itself, breaking every worktree's link."

Reproduction (main at 2de291c06, debug build, scratch HOME)

git init -q -b main repo && cd repo
printf 'node_modules/\n' > .gitignore
mkdir -p .config node_modules/pkg && echo real > node_modules/pkg/index.js
printf '[pre-start]\ndeps = "ln -sf {{ primary_worktree_path }}/node_modules ."\n' > .config/wt.toml
git add -A && git commit -qm init
wt switch --create feat --yes --no-cd && cd ../repo.feat
git status --short            # ?? node_modules
echo change > a.txt && wt step commit --yes
git show --stat HEAD          # a.txt | 1 +   node_modules | 1 +
wt merge --yes main
cd ../repo && ls -la | grep node_modules
# node_modules -> /…/repo/node_modules
ls node_modules/
# ls: cannot access 'node_modules/': Too many levels of symbolic links

Where it comes from

  • The recipe: src/cli/step.rs#L398-L404, mirrored to docs/src/content/docs/step.md.
  • Git replacing an ignored path when a tracked file arrives is Git's behavior (AGENTS.md, Data Safety), so the recipe is what sets up the loss. A .gitignore of node_modules (no slash) ignores the symlink and avoids it; the recipe doesn't mention this.

worktrunk-bot avatar Nov 22 '26 09:11 worktrunk-bot

#4887 adds a warning under the Node.js recipe in wt step copy-ignored --help and the generated step docs. It says .gitignore needs node_modules without a trailing slash, because node_modules/ matches only directories and leaves the symlink untracked. Plain Git confirms the cause: the same symlink shows as ?? node_modules under node_modules/ and is ignored under node_modules. The PR's CI and codecov checks are green, and it is waiting on review and a maintainer's merge.

The PR only changes the docs. Users who keep the slashed pattern can still commit the link. A code-level guard, such as wt step commit declining to stage a symlink that points into the primary worktree, would close that gap, but whether to add one is a product decision left open here.

worktrunk-bot avatar Nov 22 '26 10:11 worktrunk-bot