can1357/oh-my-pi · error · LsError

2

2

Error message

invalid line width: '{0}'

What it means

The ls builtin's InvalidLineWidth variant fires when the value supplied for line width (e.g. via a width/line-width option) cannot be parsed as a valid width. The offending string is echoed back so the user can see what was rejected. The process exits with code 2, matching GNU ls's usage-error convention.

Source

Thrown at crates/pi-builtins/src/ls.rs:3677

fn os_str_starts_with(haystack: &OsStr, needle: &[u8]) -> bool {
	os_bytes_lossy(haystack).starts_with(needle)
}

fn write_os_str<W: Write>(writer: &mut W, string: &OsStr) -> std::io::Result<()> {
	writer.write_all(&os_bytes_lossy(string))
}
}

use colors::StyleManager;
pub use config::{Config, options};
use config::{Dereference, Files, Sort, options::QUOTING_STYLE};
use dired::DiredOutput;
pub use display::Format;
use display::{display_items, display_size, should_display, show_dir_name};

#[derive(Error, Debug)]
enum LsError {
	#[error("invalid line width: '{0}'")]
	InvalidLineWidth(String),

	#[error("general io error: {0}")]
	IOError(#[from] std::io::Error),

	#[error("{}", match .1.kind() {
		ErrorKind::NotADirectory => format!("cannot access {}: Not a directory", .0.quote()),
		ErrorKind::NotFound => format!("cannot access {}: No such file or directory", .0.quote()),
		ErrorKind::PermissionDenied => match .1.raw_os_error().unwrap_or(1) {
			1 => format!("cannot access {}: Operation not permitted", .0.quote()),
			_ => if *.3 {
				format!("cannot open directory {}: Permission denied", .0.quote())
			} else {
				format!("cannot open file {}: Permission denied", .0.quote())
			},
		},
		_ => if 9 == .1.raw_os_error().unwrap_or(1) {
			format!("cannot open directory {}: Bad file descriptor", .0.quote())

View on GitHub (pinned to 9690622007)

Solutions

  1. Pass a plain positive integer for the width option
  2. Validate/trim the width value before passing it
  3. Remove the width option to use the terminal's default width

Example fix

// before
ls(&host, &["--width=80ch", "."])?;
// after
ls(&host, &["--width=80", "."])?;
Defensive patterns

Strategy: validation

Validate before calling

fn validate_width(w: &str) -> Result<u32, String> {
    let t = w.trim();
    t.parse::<u32>().map_err(|_| format!("invalid line width: '{w}'")).and_then(|n| if n > 0 { Ok(n) } else { Err(format!("invalid line width: '{w}'")) })
}

Try / catch

match ls(&host, args) {
    Err(e) if e.to_string().starts_with("invalid line width") => {
        eprintln!("width must be a positive integer; using default");
        ls(&host, &["."])?;
    }
    other => other?,
}

Prevention

When it happens

Trigger: Passing a non-numeric or malformed value to the line-width option, e.g. `ls --width=abc` or an empty string.

Common situations: Config files or env vars supplying a width with units ('80ch') or whitespace; scripts interpolating an unset variable producing an empty width.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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