{"record":{"id":"c59b4f4d6b3aac7f","repo":"gastownhall/beads","slug":"failed-to-resolve-current-directory-w","errorCode":null,"errorMessage":"failed to resolve current directory: %w","messagePattern":"failed to resolve current directory: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/bd/worktree_cmd.go","lineNumber":1222,"sourceCode":"\tforce            bool\n\tgitignoreCleanup *gitignoreCleanupPlan\n\tprepareFacts     worktreeremove.PrepareFacts\n\tprepareErr       error\n}\n\nfunc prepareWorktreeRemoval(\n\tctx context.Context,\n\tname string,\n\toptions *worktreeRemoveOptions,\n\tafterTargetResolution func() error,\n) (*worktreeRemovalPlan, error) {\n\tgitRunner, err := newWorktreeRemovalGit()\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tcurrentDirectory, err := os.Getwd()\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to resolve current directory: %w\", err)\n\t}\n\tcurrentRootOutput, err := gitRunner.output(ctx, currentDirectory, \"rev-parse\", \"--show-toplevel\")\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"not in a git worktree: %w\", err)\n\t}\n\tcurrentRoot := filepath.Clean(strings.TrimSpace(string(currentRootOutput)))\n\n\tworktrees, err := listRegisteredWorktrees(ctx, gitRunner, currentRoot)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tmainWorktree := worktrees[0]\n\tif mainWorktree.bare {\n\t\treturn nil, fmt.Errorf(\"cannot remove a worktree when the primary worktree is bare\")\n\t}\n\ttargetEntry, err := resolveRegisteredWorktree(name, currentRoot, mainWorktree.path, worktrees)\n\tif err != nil {\n\t\treturn nil, err","sourceCodeStart":1204,"sourceCodeEnd":1240,"githubUrl":"https://github.com/gastownhall/beads/blob/71377f276968b452ee607177637970a4ff888584/cmd/bd/worktree_cmd.go#L1204-L1240","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["`cd` to a directory that exists (e.g. the main repository root) and rerun the command","If your shell's CWD is stale, open a fresh shell or `cd \"$PWD\"` after the directory was recreated","In scripts, ensure the checkout directory exists before invoking bd worktree commands"],"exampleFix":"// before: CWD deleted\nrm -rf ../mywt && bd worktree remove mywt   # getwd fails\n// after\ncd /repo && bd worktree remove mywt","handlingStrategy":"try-catch","validationCode":"if _, err := os.Stat(\".\"); err != nil {\n    return fmt.Errorf(\"current directory no longer exists; cd to repo root first\")\n}","typeGuard":null,"tryCatchPattern":"if strings.Contains(err.Error(), \"failed to resolve current directory\") {\n    // recover: cd to an existing directory (main repo root) and rerun\n}","preventionTips":["Run worktree removal from the main repository root, not from inside a worktree you are deleting","Refresh stale shells after deleting directories (cd \"$PWD\" or open a new shell)","In CI, verify the checkout exists before invoking bd commands"],"tags":["filesystem","cwd","git"],"backgroundTag":"cwd-deleted-or-unresolvable","analyzedSha":"71377f276968b452ee607177637970a4ff888584","analyzedAt":"2026-08-30T18:55:39.744Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}