git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: "add worktree" fails with "fatal: Invalid path" error

From
SHShaheed Haque <shaheedhaque@gmail.com>
Date
May 13, 2019, 12:55 UTC
Message-ID
<CAHAc2jf2Ojve=NaEshXx9qk8rtD4NHxqLEpqZq8c9t0yE4m_Qw@mail.gmail.com>
In-Reply-To
<CACsJy8C++Ds4kfs_Wc8UiVQgni-ypbyJ+0bFg1m5brt+s0Tfig@mail.gmail.com>
Hi Duy,
On Mon, 13 May 2019 at 10:21, Duy Nguyen <pclouds@gmail.com> wrote:
Show 11 quoted lines
>
> On Sun, May 12, 2019 at 5:14 PM Shaheed Haque <shaheedhaque@gmail.com> wrote:
> >
> > Hi,
> >
> > I'm running git v.2.20.1 on Ubuntu from a program which follows the pattern:
> >
> > ============
> > 1. create a temporary directory /tmp/tmpabc
>
> When is this directory deleted? After step 3a?

Yes. I note that I screwed up the numbering in my note - the order is ["git branch -D", "git worktree prune", delete directory].

Show 46 quoted lines
> > 2. in a loop:
> >     2a. create a second level of temporary directory /tmp/tmpabc/tmpworktree123
> >     2b. use "git worktree add" on the second level directory
> >     2c. do something
> > 3. cleanup
> >     3b. "git branch -D" on each basename(second level directory)
> >     3a. "git worktree prune"
> > ============
> >
> > The loop size is of the order of 8-20. In step 2b, I often get errors
> > like this (from a Bash reproducer):
> >
> > ============
> > $ git worktree add /tmp/tmpgtxug4y9/git_worktree.gBGqnfnU
> > Preparing worktree (new branch 'git_worktree.gBGqnfnU')
> > fatal: Invalid path '/tmp/tmp1q9ysvyl': No such file or directory
> > ============
> >
> > I can see that the problematic path exists in the "gitdir" file of
> > what must be an earlier worktree from an older run (the branch is
> > gone, but the tree is still there). The path appear to relate to the
> > older run's first level directory:
> >
> > ============
> > $ grep -r /tmp/tmp1q9ysvyl ../.git/worktrees/
> > ../.git/worktrees/git_worktree.frcwtjt_/gitdir:/tmp/tmp1q9ysvyl/git_worktree.frcwtjt_/.git
> > $ git worktree list
> > ...
> > /tmp/tmp1q9ysvyl/git_worktree.frcwtjt_  edde3f25 (detached HEAD)
> > ...
> > $ git branch | grep frcwtjt_
> > <no matches>
> > ============
>
> Yeah I think I know where that "Invalid path" comes from and it should
> not be there (at least it should not be a fatal error). I'll need to
> reproduce this first. But I'm certain you've given me enough
> information to do so.
>
> > NOTE: I've not yet had to try deleting the worktree, since "add
> > worktree" does appear to work some of the time, so I am able to limp
> > along.
>
> It's probably best to stay clean and delete things after you're done.
> At least you should be able to avoid this problem this way until it's
> fixed.
Ack.
Show 8 quoted lines
> > I have these questions:
> >
> > 1. There is no branch or first level directory, but "git prune" has
> > not deleted the worktree, is this expected?
>
> I assume you meant "git worktree prune", not "git prune". See
> gc.worktreePruneExpire. Dead worktree info stays for a while until
> it's deleted, so that you can recover stuff if you need to.
Yes, sorry for typo.
Show 5 quoted lines
> > 2. Is there something wrong with the sequence of steps I am following?
>
> Nope. I mean, you could try "git worktree remove" to be on the safe
> side. But it should work even without that. To me this looks very much
> like a bug.

The original code used the more obvious "git worktree remove" rather than "git worktree prune" but I switched partly because remove seemed slow (I cannot now quantify what caused me to think that), and partly because I was having other issues which, I now realise, you probably addressed in your recent "stat versus mkdir race" change.

BTW: I *love* worktrees. I have used/wrapped/developed multiple source
control systems over the years, and IMHO, this is one of the killer
features of git.
Thanks, Shaheed
Show 5 quoted lines
> > Thanks, Shaheed
> >
> > P.S. I have an strace of a failing worktree add if needed.
> --
> Duy
Previous: Duy NguyenNext: Duy Nguyen
Message 3 of 10 in “"add worktree" fails with "fatal: Invalid path" error”
  1. Shaheed HaqueMay 12, 2019
  2. Duy NguyenMay 13, 2019
  3. Shaheed HaqueMay 13, 2019
  4. Duy NguyenMay 14, 2019
  5. Shaheed HaqueMay 14, 2019
  6. worktree add: be tolerant of corrupt worktreesNguyễn Thái Ngọc Duy, May 13, 2019
  7. Shaheed HaqueMay 13, 2019
  8. Eric SunshineMay 17, 2019
  9. Duy NguyenMay 18, 2019
  10. Shaheed HaqueJun 1, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.