Skip to content

fix: wt refuses a shared path that is not in the worktree - #24

Merged
lorenzolfm merged 11 commits into
mainfrom
issue-21
Nov 1, 2026
Merged

lorenzolfm merged 11 commits into
mainfrom
issue-21

Conversation

@lorenzolfm

@lorenzolfm lorenzolfm commented Nov 1, 2026 •

Copy link
Copy Markdown
Owner

Closes #21.

wt read each path in the config as a plain string. A person can edit the file, and the README asks for that. The hook, wt sync and wt ls join each path to the store and to each worktree, and seed_entry removes what it finds there. An absolute path, "", ., .. and ../x give the same file two times, the worktree, or a path outside it.

A / at the end is worse, and the issue does not have it. With link = [".auth/"] and a correct link in the worktree, a plain wt sync (no --force) removed the content of shared/.auth. The system follows a link before a / at the end, so lstat saw a directory, same_content compared the store with itself, and remove_dir_all emptied it. The check in the issue (components().all(Normal)) accepts .auth/ and .auth/., bc Path::components drops both.

  • SharedPath in src/config.rs. Config::load parses each path into one, so a config with a bad path does not load, and the error names the path: "../x" is not a path in the worktree. give it from the top of the worktree, with no `.` or `..`.
  • It refuses an empty or absolute path, and a path with the name . or .. anywhere in it, so also ./.env and a/./b. It keeps only the names, with / between them, as RepoKey::from_url keeps one form of a URL. .auth/ and .auth// are .auth, and the next save writes .auth.
  • RepoConfig::link is Vec<SharedPath>. seed_entry, inspect, the hook, wt ls and wt share take a &SharedPath. config is a private module, so luneta sees no change.
  • relative_entry in src/cmd/share.rs returns a SharedPath. It keeps its own check for the top of the worktree, which the issue moves into the constructor: the error of the constructor says to give the path from the top of the worktree, and wt share . would print error: .: "" is not a path in the worktree.
  • The completion of wt share uses the same parse, which also drops the / that git gives after a directory.
  • The README says it.

A link in a directory above a shared path gives the same failure, and the names do not show it. With link = [".auth/token"] and .auth a link to shared/.auth (the config had .auth before, and the old links stay), a plain wt sync removed shared/.auth/token and made a link from it to itself. The bytes are lost. wt share .auth also wrote [".auth", ".auth/token"] when the config from the dotfiles had .auth/token from a different machine, and the next wt add did the same. With terraform a link out of the worktree, wt sync --force replaced the file there.

  • inspect reads each directory above the path first, and it gives BelowLink when one is a link. seed_entry then changes nothing, also with --force, and wt ls, wt sync, wt share and the hook name the path. The hook matched the other outcomes with _, so it names each one now, and Outcome::is_change is gone.
  • RepoConfig::link is a SharedPaths: a set that refuses a path inside a different one, at load and at insert. The error names both paths: ".auth/token" is inside ".auth". keep one of them. The names decide, so .auth-old is not inside .auth. The same path two times is one path, and before [".auth", ".auth/"] gave 2/2 links. wt share tests the list before it moves anything.
  • The completion of wt share reads git ls-files -z. With no -z, git puts a name that is not ASCII in quotes, and the completion gave "\303\251.env".
  • wt share refuses a path that is not UTF-8. to_string_lossy gave a different path.
  • Config::save writes a temporary file and renames it, so the hook never reads a part of the config. A config that is a link into the dotfiles stays a link, bc save writes the target.

wt makes no link in the store, so find <common>/shared -type l finds each file that an older wt lost this way.

A config with a path that is not in the worktree, or with a path inside a different one, stops loading after the upgrade, for each repository in it, as with two keys that differ only in case. The user corrects it by hand.

Test

The four CI gates (nix build .#wt .#wt-clippy .#wt-test .#wt-fmt) pass. cargo test runs 82 tests.

  • One test loads a config with each of /home/you/project/main/.env, /, "", ., .., ../.env, ./.env, .auth/. and a/../.env. Each is refused, and the error names the path. With no check, each one loads. With the check in the issue, .auth/. loads.
  • One test loads .auth/ and terraform//vars.tfvars. They are .auth and terraform/vars.tfvars. With the check in the issue, they stay as they are.
  • Two tests put a link above the path, one to the store and one out of the worktree, and call seed_entry with --force. The store and the file outside keep their bytes. With no check, both fail.
  • One test loads [".auth", ".auth/token"] in both orders, and each is refused. One loads .auth with .auth-old, and one loads a path two times.
  • One test writes the config through a link, and the link stays.
  • One test gives wt share a current directory that is not UTF-8.

Manual test: the binary from this branch, with HOME and XDG_CONFIG_HOME in a temp dir, on a wt clone of a local origin with .env and .auth shared.

Case Result
.auth/, wt sync each worktree is correct, and the store keeps .auth/a.json. On main, the same seed_entry call empties the store and fails with wt cannot remove.
An absolute path, wt sync and wt ls Refused, and the error names the path.
An absolute path, git worktree add The hook prints the error. git succeeds, and .env stays.
"", wt sync --force Refused. The worktree keeps each file.
../x, wt share .env Refused before it moves anything.
wt share . wt cannot share the top directory of the worktree, as before.
.auth narrowed to .auth/token, the old links stay, wt sync skipped, and the store keeps .auth/token. On main, the file in the store becomes a link to itself.
The dotfiles have .auth/token, wt share .auth Refused before it moves anything, and the error names .auth/token. On main, the next wt add loses the file in the store.
terraform a link out of the worktree, wt sync --force skipped, and the file outside stays.
[".auth", ".auth/token"], wt sync Refused, and the error names both paths.
é.env ignored, wt share <TAB> é.env, with no quotes.

wt read each path in the config as a plain string, and a person can
edit the file. The hook, wt sync and wt ls join each path to the store
and to each worktree, and seed_entry removes the file that it finds
there. An absolute path gave the same file two times, so the hook
removed it and linked the path to itself. An empty path gave the
worktree, so wt sync --force removed each file in it that git does not
have. `.`, `..` and `../x` gave the worktree or a path outside it.

A `/` at the end removed the store, also with no --force. The system
follows a link before such a `/`, so a correct link to a directory in
the store looked like a copy of the store. wt sync then removed the
content of the store and failed.

Config::load now parses each path into a SharedPath. It refuses a path
that is empty or absolute, or that has the name `.` or `..`, and the
error names the path. It keeps only the names, so `.auth/` is `.auth`.
A config with such a path no longer loads, and the user corrects it.

wt share keeps its own error for the top of the worktree: the error of
the config says to give the path from the top of the worktree, and wt
share reads a path from the current directory.
The names of a shared path are in the worktree, but its file can be
outside it. seed_entry read the path with lstat, which does not follow
the last name. The system follows each name above it. With `.auth` a
link to the store, `.auth/token` was the file in the store: same_content
compared the store with itself, and seed_entry removed the file and made
a link from it to itself. A plain wt sync did it, with no --force, and
the bytes were lost.

A config gets such a path in three ways. The user narrows `.auth` to
`.auth/token`, and the old links stay. The config in the dotfiles has
`.auth/token` from a different machine, and wt share .auth adds `.auth`.
A hand edit gives `terraform/vars.tfvars`, and `terraform` is a link
out of the worktree: wt sync --force then replaced the file there.

inspect now reads each directory above the path first, and it gives
BelowLink when one is a link. seed_entry then changes nothing, also with
--force, and wt ls, wt sync, wt share and the hook name the path. The
hook matched the other outcomes with `_`, so it now names each one, and
Outcome::is_change has no caller.

wt makes no link in the store, so `find <common>/shared -type l` finds
each file that an older wt lost this way.
The config kept the paths of a repository in a list, and
RepoConfig::insert compared a new path only with the same path. wt share
.auth therefore added `.auth` to a config that had `.auth/token`, for
example from the dotfiles of a different machine. A link at `.auth`
makes `.auth/token` the file in the store. Before the last commit, the
hook then removed the file in the store at the next wt add. Now each
worktree reports the path below a link, at each command.

A hand edit also gave `.auth` and `.auth/` two times, and wt ls counted
`2/2 links` for one path.

RepoConfig::link is now a SharedPaths: a set that refuses a path inside
a different one, at Config::load and at insert, and the error names both
paths. The names decide, so `.auth-old` is not inside `.auth`. The same
path two times loses nothing, so the set keeps it one time. wt share
tests the list before it moves anything, and the completion of wt share
no longer gives a path inside a shared one.

A config with such two paths no longer loads, for each repository in
it, and the user corrects it by hand.
With no -z, git ls-files puts a name that is not ASCII in quotes, for
example `"\303\251.env"`. SharedPath accepted the quotes as part of the
name, and the completion gave a path that is not present. The completion
now reads the names with -z.
relative_entry changed a name that is not UTF-8 with to_string_lossy,
so the path became a different path, and wt share then said that the
path is not in the worktree. The config file holds only UTF-8, so wt
share now says that. clap reads each argument as UTF-8, so such a name
comes from the current directory.
Config::save wrote the file in place. A reader at the same time, for
example the hook of a checkout in a different terminal, could read a
part of the file and fail. save now writes a temporary file beside the
config and renames it, so a reader gets the old file or the new file.

The README asks for the file in the dotfiles, so it can be a link. A
rename over the link would put a plain file in its place, and the
dotfiles would no longer get the changes. save therefore writes the
target of the link.

Two wt share at the same time can still lose the path of one of them.
wt share of that path again records it, because the store has it.
The last save renamed a temporary file over the result of canonicalize.
canonicalize fails on a link to a file that the dotfiles do not have
yet, so save put a plain file in place of the link. A rename onto a
file that is a mount point, for example in a container, fails with
EBUSY, and wt share then moved the path into the store but did not
record it. A rename also gave the new text to one name of a hard link
only, and the new file got the mode of the umask.

link_target now follows each link, and the target does not need to
exist. The temporary file gets the mode of the old file. For a hard
link and for a mount point, save writes the file in place, as before
the last commit.
below_link returned each error except NotFound. Below a plain file the
system gives ENOTDIR, so a path such as a/b/x.env with a file at a
stopped wt sync, the hook and wt share with an error, before the next
path. Before below_link, wt reported the path as missing when the
store did not have it. below_link now reads a file above the path as
no link, and the next tests report the path as before.
@lorenzolfm
lorenzolfm merged commit eb50a47 into main Nov 1, 2026
4 checks passed
@lorenzolfm
lorenzolfm deleted the issue-21 branch November 9, 2026 16:54
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.

A path in the config that is not in the worktree makes wt delete files

1 participant