{"record":{"id":"56124dc66d63b80f","repo":"charmbracelet/crush","slug":"failed-to-change-directory-v","errorCode":null,"errorMessage":"failed to change directory: %v","messagePattern":"failed to change directory: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/root.go","lineNumber":974,"sourceCode":"\t\t}\n\t}\n\n\tswitch len(matches) {\n\tcase 0:\n\t\treturn session.Session{}, fmt.Errorf(\"session not found: %s\", id)\n\tcase 1:\n\t\treturn matches[0], nil\n\tdefault:\n\t\treturn session.Session{}, fmt.Errorf(\"session ID %q is ambiguous (%d matches)\", id, len(matches))\n\t}\n}\n\nfunc ResolveCwd(cmd *cobra.Command) (string, error) {\n\tcwd, _ := cmd.Flags().GetString(\"cwd\")\n\tif cwd != \"\" {\n\t\terr := os.Chdir(cwd)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to change directory: %v\", err)\n\t\t}\n\t\treturn cwd, nil\n\t}\n\tcwd, err := os.Getwd()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to get current working directory: %v\", err)\n\t}\n\treturn cwd, nil\n}\n\nfunc createDotCrushDir(dir string) error {\n\tif err := os.MkdirAll(dir, 0o700); err != nil {\n\t\treturn fmt.Errorf(\"failed to create data directory: %q %w\", dir, err)\n\t}\n\n\tgitIgnorePath := filepath.Join(dir, \".gitignore\")\n\tcontent, err := os.ReadFile(gitIgnorePath)\n","sourceCodeStart":956,"sourceCodeEnd":992,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/cmd/root.go#L956-L992","documentation":"Wraps os.Chdir failure when the --cwd flag is provided. The OS refused to switch into the given directory - typically because it does not exist, the path is a file, or the process lacks search (execute) permission on a path component.","triggerScenarios":"`crush --cwd /some/path` where the path was deleted/renamed, is a regular file, or the user lacks permission; relative --cwd values resolved against an unexpected base directory.","commonSituations":"Hard-coded paths in scripts/aliases pointing at moved repositories; containers with restricted mounts; typos in the flag value; removed symlink targets.","solutions":["Verify the path exists and is a directory: `test -d /some/path`","Correct the --cwd value or use an absolute path","Fix permissions on the directory and its parents (chmod/chown)","Remove the --cwd flag and cd manually before running crush"],"exampleFix":"// before\ncrush --cwd ~/projects/app run \"build\"\n// after: validate first\ntest -d ~/projects/app && crush --cwd ~/projects/app run \"build\"","handlingStrategy":"validation","validationCode":"func dirExists(p string) bool {\n\tinfo, err := os.Stat(p)\n\treturn err == nil && info.IsDir()\n}\n// before invoking: crush --cwd <dir>\nif !dirExists(target) {\n\treturn fmt.Errorf(\"--cwd target is not a directory: %s\", target)\n}","typeGuard":null,"tryCatchPattern":"cwd, err := ResolveCwd(cmd)\nif err != nil {\n\tif strings.Contains(err.Error(), \"change directory\") {\n\t\tslog.Error(\"--cwd path invalid; verify it exists and is a directory\")\n\t}\n\treturn err\n}","preventionTips":["Use absolute paths for --cwd in scripts","Stat the directory before chdir","Avoid pointing --cwd at paths under active renames/symlink churn","Check parent-directory execute permissions"],"tags":["filesystem","cwd","cli"],"backgroundTag":"directory-not-found","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}