gastownhall/beads · error

failed to resolve current directory: %w

Error message

failed to resolve current directory: %w

What it means

prepareWorktreeRemoval resolves the current working directory with `os.Getwd` to determine which worktree the user is in before planning removal. This error means `os.Getwd` itself failed — the process cannot determine its CWD — typically because the directory was deleted or is unreachable.

Source

Thrown at cmd/bd/worktree_cmd.go:1222

	force            bool
	gitignoreCleanup *gitignoreCleanupPlan
	prepareFacts     worktreeremove.PrepareFacts
	prepareErr       error
}

func prepareWorktreeRemoval(
	ctx context.Context,
	name string,
	options *worktreeRemoveOptions,
	afterTargetResolution func() error,
) (*worktreeRemovalPlan, error) {
	gitRunner, err := newWorktreeRemovalGit()
	if err != nil {
		return nil, err
	}
	currentDirectory, err := os.Getwd()
	if err != nil {
		return nil, fmt.Errorf("failed to resolve current directory: %w", err)
	}
	currentRootOutput, err := gitRunner.output(ctx, currentDirectory, "rev-parse", "--show-toplevel")
	if err != nil {
		return nil, fmt.Errorf("not in a git worktree: %w", err)
	}
	currentRoot := filepath.Clean(strings.TrimSpace(string(currentRootOutput)))

	worktrees, err := listRegisteredWorktrees(ctx, gitRunner, currentRoot)
	if err != nil {
		return nil, err
	}
	mainWorktree := worktrees[0]
	if mainWorktree.bare {
		return nil, fmt.Errorf("cannot remove a worktree when the primary worktree is bare")
	}
	targetEntry, err := resolveRegisteredWorktree(name, currentRoot, mainWorktree.path, worktrees)
	if err != nil {
		return nil, err

View on GitHub (pinned to 71377f2769)

Solutions

  1. `cd` to a directory that exists (e.g. the main repository root) and rerun the command
  2. If your shell's CWD is stale, open a fresh shell or `cd "$PWD"` after the directory was recreated
  3. In scripts, ensure the checkout directory exists before invoking bd worktree commands

Example fix

// before: CWD deleted
rm -rf ../mywt && bd worktree remove mywt   # getwd fails
// after
cd /repo && bd worktree remove mywt
Defensive patterns

Strategy: try-catch

Validate before calling

if _, err := os.Stat("."); err != nil {
    return fmt.Errorf("current directory no longer exists; cd to repo root first")
}

Try / catch

if strings.Contains(err.Error(), "failed to resolve current directory") {
    // recover: cd to an existing directory (main repo root) and rerun
}

Prevention

When it happens

Trigger: Running `bd worktree remove` while the shell sits in a directory that has been deleted (or renamed) on disk, or under permission/caged-environment conditions where the OS cannot resolve the CWD.

Common situations: Removing a worktree from inside that worktree's deleted sibling; container/CI steps that `rm -rf` the checkout before running bd; stale shell after `git worktree remove` in another terminal (getwd returns ENOENT).

Related errors


AI-assisted analysis of gastownhall/beads@71377f2769 (2026-08-30). Data as JSON: /api/errors/c59b4f4d6b3aac7f. Report an issue: GitHub.