{"record":{"id":"729d3fe3ec59371e","repo":"wavetermdev/waveterm","slug":"marshaling-directory-listing-for-s-w","errorCode":null,"errorMessage":"marshaling directory listing for %s: %w","messagePattern":"marshaling directory listing for (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/wsh/cmd/wshcmd-ai.go","lineNumber":124,"sourceCode":"\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)\n\t\t\t\t}\n\t\t\t\tfileName = absPath\n\t\t\t\tmimeType = detectMimeType(data)\n\t\t\t}\n\t\t}\n\n\t\tisPDF := mimeType == \"application/pdf\"\n\t\tisImage := strings.HasPrefix(mimeType, \"image/\")\n\t\tisDirectory := mimeType == \"directory\"\n","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/wavetermdev/waveterm/blob/a4447c1563b2df285ab89e76c82f91e1a1a49c1e/cmd/wsh/cmd/wshcmd-ai.go#L106-L142","documentation":"Wraps json.Marshal failure on the directory listing produced by fileutil.ReadDir during `wsh ai <dir>`. This is essentially unreachable in practice — the listing result contains only plain strings/metadata — so hitting it indicates a malformed/unsupported entry returned by the filesystem layer. The wrapped error is preserved via %w.","triggerScenarios":"json.Marshal returning an error for the ReadDir result — only plausible with a corrupted or non-standard fileutil.ReadDir result (e.g. unsupported value types from a custom/patched filesystem listing), since normal DirEntry data marshals fine.","commonSituations":"Practically never seen by end users; would indicate a bug in fileutil.ReadDir or a filesystem returning pathological metadata.","solutions":["Retry the command — a transient filesystem anomaly may resolve it.","Update Wave Terminal; if reproducible, report a bug with the wrapped error message.","Work around by attaching individual files instead of the directory.","Inspect the wrapped error to identify which entry failed to marshal."],"exampleFix":"// before\n$ wsh ai ./weird-dir\n// after (workaround)\n$ wsh ai ./weird-dir/file1.txt ./weird-dir/file2.txt","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := runAI(args); err != nil {\n    if strings.Contains(err.Error(), \"marshaling directory listing\") {\n        // unexpected: DirEntry data should always marshal; retry once, then report a bug\n    }\n    return err\n}","preventionTips":["Keep Wave Terminal updated; a reproducible case indicates a fileutil.ReadDir bug worth reporting.","Prefer attaching individual files, which bypass directory-listing marshaling entirely."],"tags":["json","serialization","directory","wsh"],"backgroundTag":"json-marshal-failed","analyzedSha":"a4447c1563b2df285ab89e76c82f91e1a1a49c1e","analyzedAt":"2026-09-01T15:26:23.972Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}