can1357/oh-my-pi · error · SortError

write failed: {}: {}

Error message

write failed: {}: {}

What it means

`SortError::WriteFailed` occurs when the sort builtin cannot write to a file (typically a temporary chunk file during external merge sorting or the output sink). The message quotes the path and the errno-stripped OS error. This usually means disk, permissions, or space issues.

Source

Thrown at crates/pi-builtins/src/sort.rs:2593

	}
}

const NEGATIVE: &u8 = &b'-';
const POSITIVE: &u8 = &b'+';

// The automatic buffer heuristics clamp to this range to avoid
// over-committing memory on constrained systems while still keeping
// reasonably large chunks for typical workloads.
const MIN_AUTOMATIC_BUF_SIZE: usize = 512 * 1024; // 512 KiB
const FALLBACK_AUTOMATIC_BUF_SIZE: usize = 32 * 1024 * 1024; // 32 MiB
const MAX_AUTOMATIC_BUF_SIZE: usize = 1024 * 1024 * 1024; // 1 GiB

#[derive(Debug, Error)]
pub enum SortError {
	#[error("{0}")]
	Message(String),

	#[error("write failed: {}: {}", .path.maybe_quote(), strip_errno(.error))]
	WriteFailed { path: OsString, error: std::io::Error },

	#[error("{}", format_disorder(.file, .line_number, .line, .silent))]
	Disorder { file: OsString, line_number: usize, line: String, silent: bool },

	#[error("open failed: {}: {}", .path.maybe_quote(), strip_errno(.error))]
	OpenFailed { path: PathBuf, error: std::io::Error },

	#[error("cannot read: {}: {}", .path.maybe_quote(), strip_errno(.error))]
	ReadFailed { path: PathBuf, error: std::io::Error },

	#[error("failed to open temporary file: {}", strip_errno(.error))]
	OpenTmpFileFailed { error: std::io::Error },

	#[error("could not run compress program '{}': {}", .prog, strip_errno(.error))]
	CompressProgExecutionFailed { prog: String, error: std::io::Error },

	#[error("{} terminated abnormally", .prog.quote())]

View on GitHub (pinned to 9690622007)

Solutions

  1. Check free disk space (`df -h`) and free space if ENOSPC.
  2. Verify the output/temp directory exists and is writable (`ls -ld`, check $TMPDIR).
  3. Confirm write permissions on the target file/directory for the running user.
  4. Point TMPDIR to a writable location with enough capacity.

Example fix

// before
TMPDIR=/nonexistent sort --compress-program=gz big.txt

// after
TMPDIR=/tmp sort --compress-program=gz big.txt
Defensive patterns

Strategy: try-catch

Validate before calling

// before sorting
let out = std::fs::metadata(&out_dir)?;
if !out.is_dir() { eprintln!("output target must be a directory"); }
let free = available_space(&out_dir); // check ENOSPC risk
if free < required_estimate { eprintln!("insufficient disk space"); }

Try / catch

match sort_result {
    Err(SortError::WriteFailed { path, error }) => {
        eprintln!("write to {} failed: {error}; check disk space and permissions", path.display());
    }
    Err(e) => eprintln!("sort: {e}"),
    Ok(_) => {}
}

Prevention

When it happens

Trigger: Sorting data large enough to spill to temporary files, where writing a chunk to disk fails (EACCES, ENOSPC, EROFS, deleted temp dir), or failing to write the final output file.

Common situations: Full disk or quota while sorting huge files; TMPDIR pointing to a read-only or non-existent location; permissions on the output path.

Related errors


AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31). Data as JSON: /api/errors/6a3b186c35b19c78. Report an issue: GitHub.