wavetermdev/waveterm · error

marshaling directory listing for %s: %w

Error message

marshaling directory listing for %s: %w

What it means

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.

Source

Thrown at cmd/wsh/cmd/wshcmd-ai.go:124

			mimeType = "text/plain"
		} else {
			fileInfo, err := os.Stat(filePath)
			if err != nil {
				return fmt.Errorf("accessing file %s: %w", filePath, err)
			}
			absPath, err := filepath.Abs(filePath)
			if err != nil {
				return fmt.Errorf("getting absolute path for %s: %w", filePath, err)
			}

			if fileInfo.IsDir() {
				result, err := fileutil.ReadDir(filePath, 500)
				if err != nil {
					return fmt.Errorf("reading directory %s: %w", filePath, err)
				}
				jsonData, err := json.Marshal(result)
				if err != nil {
					return fmt.Errorf("marshaling directory listing for %s: %w", filePath, err)
				}
				data = jsonData
				fileName = absPath
				mimeType = "directory"
			} else {
				data, err = os.ReadFile(filePath)
				if err != nil {
					return fmt.Errorf("reading file %s: %w", filePath, err)
				}
				fileName = absPath
				mimeType = detectMimeType(data)
			}
		}

		isPDF := mimeType == "application/pdf"
		isImage := strings.HasPrefix(mimeType, "image/")
		isDirectory := mimeType == "directory"

View on GitHub (pinned to a4447c1563)

Solutions

  1. Retry the command — a transient filesystem anomaly may resolve it.
  2. Update Wave Terminal; if reproducible, report a bug with the wrapped error message.
  3. Work around by attaching individual files instead of the directory.
  4. Inspect the wrapped error to identify which entry failed to marshal.

Example fix

// before
$ wsh ai ./weird-dir
// after (workaround)
$ wsh ai ./weird-dir/file1.txt ./weird-dir/file2.txt
Defensive patterns

Strategy: retry

Try / catch

if err := runAI(args); err != nil {
    if strings.Contains(err.Error(), "marshaling directory listing") {
        // unexpected: DirEntry data should always marshal; retry once, then report a bug
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: Practically never seen by end users; would indicate a bug in fileutil.ReadDir or a filesystem returning pathological metadata.

Understand the failure class

Background: json.Marshal / "failed to marshal" errors in Go: why "unsupported type" happens and how to fix it — this error's family across 22 libraries.

Related errors


AI-assisted analysis of wavetermdev/waveterm@a4447c1563 (2026-09-01). Data as JSON: /api/errors/729d3fe3ec59371e. Report an issue: GitHub.