Skip to content

fix: handle is_bare() error in repo_path() for bare repos - #1225

Closed
worktrunk-bot wants to merge 3 commits into
mainfrom
fix/repo-path-bare-timeout-fallback
Closed

worktrunk-bot wants to merge 3 commits into
mainfrom
fix/repo-path-bare-timeout-fallback

Conversation

@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #1221 (suggested by @bendrucker in #1221 (comment)).

  • Moves the --show-toplevel check before is_bare() in repo_path() so submodules are handled first regardless of is_bare() success
  • Replaces unwrap_or(false) with a match that treats Err(_) as bare — if is_bare() fails (e.g., git timeout), we return git_common_dir instead of parent(), which is the correct path for bare repos and avoids caching a nonsensical value

Test plan

  • All bare repo integration tests pass (cargo test --test integration bare — 21 tests)
  • All repo_path tests pass (bare repo path, submodule path)
  • Unit tests and clippy clean
  • CI (linux, macos, windows)

🤖 Generated with Claude Code

Move `--show-toplevel` check before `is_bare()` in `repo_path()` and
treat `is_bare()` errors as bare. Previously `unwrap_or(false)` meant a
git timeout on a bare repo would fall through to `parent()`, caching
a nonsensical path. Now `Err(_)` maps to `git_common_dir`, which is
correct for bare repos. For normal repos a git timeout is a bigger
problem anyway, so the wrong cached path is moot.

Follow-up to #1221.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@max-sixty

Copy link
Copy Markdown
Owner

I'm slightly rethinking the original issue. I agree it was wrong to panic. But should we raise an error if we can't successfully run is_bare?

Are other commands likely to succeed? What are the conditions in which the fs / the repo is in a good enough state to serve us generally, while being unable to run is_bare?

Or is it a code simplicity thing; that it's going to cause every command to require running Result through the chain?

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Good question. You're right that if is_bare() fails (e.g., git timeout), other git commands will likely fail too. The question is whether to fail early with a clear error vs. cache a best-effort path and let downstream commands fail on their own.

The reason I went with the fallback approach rather than raising an error:

  1. repo_path() returns &Path, not Result — it's cached via OnceCell::get_or_init and has ~20 call sites across 15 files. Changing to Result would mean threading Result through all of them, which felt disproportionate for a transient-timeout edge case.

  2. The fallback is reasonable — if is_bare() errors, returning git_common_dir is correct for bare repos (the likely case when --show-toplevel already failed) and for normal repos it gives .git, which is wrong but those subsequent commands will also fail with clear errors.

  3. is_bare() uses get_or_try_init — so errors aren't cached. If the timeout was transient, the next command's call to is_bare() will retry and succeed.

That said, if you'd prefer to raise an error eagerly, I'm happy to change repo_path() to return Result<&Path> and propagate through callers — it's mechanical but not complex. The tradeoff is just code churn vs. a clearer failure mode for a rare edge case.

@bendrucker

bendrucker commented Mar 5, 2026 •

Copy link
Copy Markdown
Contributor

In my case, this was a transient issue, and so I can't offer a clear answer to what the cause was. git commands worked by the time I read the error and had time to run something. It was a one-off crash.

I'd say it's likely that any other git command run during whatever caused the timeout (presumably I/O contention from something) would have failed. Your and wt-bot's characterizations are accurate, returning a Result would be totally reasonable here too, just more code churn.

max-sixty pushed a commit that referenced this pull request Mar 5, 2026
## Summary

- Moves "Setup build tools" step **before** PR branch checkout in
`claude-mention` and `claude-review` workflows
- The composite action (`.github/actions/claude-setup`) only exists on
`main` — checking out a PR branch created before #1273 removes it,
causing the step to fail
- Also adds `if: steps.verify.outcome != 'failure'` guard to the setup
step in `claude-mention` (was missing)

**Failed runs:**
[22703855403](https://github.com/max-sixty/worktrunk/actions/runs/22703855403),
[22703814015](https://github.com/max-sixty/worktrunk/actions/runs/22703814015)
(both triggered on PR #1225)

Fixes #1275

## Test plan

- [ ] Trigger `claude-mention` on PR #1225 (which predates #1273) and
verify the setup step succeeds
- [ ] Verify `claude-review` `pull_request_review` path still works

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: worktrunk-bot <[email protected]>
Co-authored-by: Claude Opus 4.6 <[email protected]>
@max-sixty

Copy link
Copy Markdown
Owner

That said, if you'd prefer to raise an error eagerly, I'm happy to change repo_path() to return Result<&Path> and propagate through callers — it's mechanical but not complex. The tradeoff is just code churn vs. a clearer failure mode for a rare edge case.

please could you draft this change in a PR so we can compare?

worktrunk-bot added a commit that referenced this pull request Mar 5, 2026
Alternative to #1225. Changes `repo_path()` to return `anyhow::Result<&Path>`
using `get_or_try_init`, propagating `is_bare()` failures to callers instead
of silently caching a fallback value.

This surfaces git failures (e.g., timeouts) early with clear errors rather
than caching a potentially wrong path. The tradeoff is more code churn
(18 files, ~20 call sites) but a cleaner failure mode.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

Drafted the alternative approach in #1280. That PR changes repo_path() to return Result<&Path> using get_or_try_init, propagating the is_bare() error to callers instead of falling back.

The diff is 18 files / ~20 call sites — mostly mechanical ? additions. Two functions needed signature changes: build_hook_context() (returns Result<HashMap> now) and format_path() (returns anyhow::Result<String> instead of Result<String, TemplateExpandError>). Best-effort contexts like the deprecation warning and log formatter silently swallow the error.

@max-sixty

Copy link
Copy Markdown
Owner

@worktrunk-bot anything useful remaining here?

@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

This PR is superseded by #1280 (merged), which took the alternative approach of propagating repo_path() errors via Result instead of falling back. Closing in favor of that.

@max-sixty
max-sixty deleted the fix/repo-path-bare-timeout-fallback branch June 18, 2026 06:00
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.

3 participants