hashicorp/nomad · error
failed to write to file: %w
Error message
failed to write to file: %w
What it means
writeNDJSON serializes a slice of Nomad API objects as newline-delimited JSON into the debug bundle archive file. This error wraps any failure from writeJSON (encoding/marshaling an object) encountered during that loop. It indicates the bundle collection could not persist one of the API payloads it gathered.
Source
Thrown at command/operator_debug.go:1566
buf, err = json.Marshal(errorWrapper{Error: err.Error()})
if err != nil {
return fmt.Errorf("could not serialize our own error: %v", err)
}
}
n, err := writer.Write(buf)
if err != nil {
return fmt.Errorf("write error, wrote %d bytes of %d: %v", n, len(buf), err)
}
return nil
}
// writeNDJSON writes a slice of Nomad API objects to the archive file as
// newline-delimited JSON objects.
func writeNDJSON[T any](data []T, writer io.Writer) error {
for _, obj := range data {
err := writeJSON(obj, writer)
if err != nil {
return fmt.Errorf("failed to write to file: %w", err)
}
_, err = writer.Write([]byte{'\n'})
if err != nil {
return fmt.Errorf("failed to write to file: %w", err)
}
}
return nil
}
// writeError writes a JSON error object to capture errors in the debug bundle without
// reporting
func (c *OperatorDebugCommand) writeError(dir, file string, err error) error {
bytes, err := json.Marshal(errorWrapper{Error: err.Error()})
if err != nil {
return err
}
return c.writeBytes(dir, file, bytes)View on GitHub (pinned to 482b49bf1a)
Solutions
- Inspect the wrapped %w error to see whether it is a marshal failure or an I/O failure on the writer
- Verify the target file/directory for the debug bundle is writable and the disk is not full
- Re-run `nomad operator debug`; transient API responses can cause one-off marshal/I/O failures
- Update Nomad; if a specific API object type fails to marshal, it may be a fixed client bug
Defensive patterns
Strategy: try-catch
Validate before calling
// Go: ensure the output location is writable before collecting
if f, err := os.Create(path.Join(outDir, ".writecheck")); err != nil {
return fmt.Errorf("output dir not writable: %w", err)
} else {
f.Close()
os.Remove(f.Name())
} Type guard
func isMarshalErr(err error) bool {
var je *json.MarshalTypeError
return errors.As(err, &je)
} Try / catch
if err := writeNDJSON(objs, w); err != nil {
var wrapped error
errors.As(err, &wrapped) // unwrap the %w cause
log.Printf("ndjson write failed: %v", err)
// fall back to stderr or skip the section
} Prevention
- Check disk space on the node before running nomad operator debug
- Ensure the -output directory exists and is writable by the CLI user
- Keep Nomad CLI and server versions in sync
- Inspect errors.Unwrap results to distinguish marshal vs I/O causes
When it happens
Trigger: writeJSON returns an error for one of the objects in the data slice while collecting nomad operator debug output (e.g. json.Marshal failing via an unsupported type or an underlying writer failure surfaced by the encoder).
Common situations: Debugging a Nomad cluster with `nomad operator debug`; a custom writer/pipe backing the file handle is closed or broken, or a marshal failure on an unexpected API response shape.
Understand the failure class
Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.
Related errors
- write error, wrote %d bytes of %d: %v
- %v
- unable to read rooted allocation directory
- Request body is empty
- unexpected ExpirationLeeway type: %v
AI-assisted analysis of hashicorp/nomad@482b49bf1a (2026-09-04).
Data as JSON: /api/errors/aa96eaee06cb0b1d.
Report an issue: GitHub.