{"record":{"id":"f44e294b292f556f","repo":"wavetermdev/waveterm","slug":"getting-absolute-path-for-s-w","errorCode":null,"errorMessage":"getting absolute path for %s: %w","messagePattern":"getting absolute path for (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/wsh/cmd/wshcmd-ai.go","lineNumber":114,"sourceCode":"\t\t\tif stdinUsed {\n\t\t\t\treturn fmt.Errorf(\"stdin (-) can only be used once\")\n\t\t\t}\n\t\t\tstdinUsed = true\n\n\t\t\tdata, err = io.ReadAll(os.Stdin)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"reading from stdin: %w\", err)\n\t\t\t}\n\t\t\tfileName = \"stdin\"\n\t\t\tmimeType = \"text/plain\"\n\t\t} else {\n\t\t\tfileInfo, err := os.Stat(filePath)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"accessing file %s: %w\", filePath, err)\n\t\t\t}\n\t\t\tabsPath, err := filepath.Abs(filePath)\n\t\t\tif err != nil {\n\t\t\t\treturn fmt.Errorf(\"getting absolute path for %s: %w\", filePath, err)\n\t\t\t}\n\n\t\t\tif fileInfo.IsDir() {\n\t\t\t\tresult, err := fileutil.ReadDir(filePath, 500)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn fmt.Errorf(\"reading directory %s: %w\", filePath, err)\n\t\t\t\t}\n\t\t\t\tjsonData, err := json.Marshal(result)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn fmt.Errorf(\"marshaling directory listing for %s: %w\", filePath, err)\n\t\t\t\t}\n\t\t\t\tdata = jsonData\n\t\t\t\tfileName = absPath\n\t\t\t\tmimeType = \"directory\"\n\t\t\t} else {\n\t\t\t\tdata, err = os.ReadFile(filePath)\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn fmt.Errorf(\"reading file %s: %w\", filePath, err)","sourceCodeStart":96,"sourceCodeEnd":132,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/cmd/wsh/cmd/wshcmd-ai.go#L96-L132","documentation":"Wraps filepath.Abs(filePath) failure while processing a `wsh ai` file argument. filepath.Abs rarely fails — it only errors when os.Getwd() fails, i.e. the current working directory has been deleted or is otherwise unresolvable. The wrapped error is preserved via %w.","triggerScenarios":"Running `wsh ai <file>` after the shell's cwd directory was removed or renamed (common when a build/temp dir is deleted out from under the process); getwd returning ENOENT/EACCES.","commonSituations":"Long-lived shells whose project directory was deleted/re-created (cwd now stale); working inside a removed tmpfs or container volume; scripts that `cd` into ephemeral dirs.","solutions":["cd back to a valid directory (e.g. `cd ~` then `cd /path/to/project`) and rerun wsh ai.","Restart the shell/terminal session if its cwd is gone.","Pass an absolute file path — Abs on an absolute path skips Getwd, avoiding the failure.","Recreate the deleted working directory before running the command."],"exampleFix":"// before (cwd deleted)\n$ rm -rf /tmp/build && wsh ai out.txt\n// after\n$ cd /home/user/project && wsh ai /home/user/project/out.txt","handlingStrategy":"validation","validationCode":"if ! pwd >/dev/null 2>&1; then\n  echo \"current working directory is gone; cd to a valid directory first\" >&2\n  exit 1\nfi","typeGuard":null,"tryCatchPattern":"if err := runAI(args); err != nil {\n    if strings.Contains(err.Error(), \"getting absolute path\") {\n        // os.Getwd() failed: cd back to a valid directory or restart the shell, then retry\n    }\n    return err\n}","preventionTips":["Avoid deleting/renaming the directory your shell is currently in.","Use absolute file paths with wsh ai so filepath.Abs never needs Getwd.","Re-create project dirs rather than deleting them out from under running shells (rm -rf + mkdir loses the cwd)."],"tags":["filesystem","working-directory","path","wsh"],"backgroundTag":"getwd-failed-cwd-deleted","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}