sxyazi/yazi · error · anyhow::Error

--target must be valid UTF-8

Error message

--target must be valid UTF-8

What it means

yazi-build's CLI parser reads --target as an OsString and converts it to String with into_string(). On failure (the OS string contains bytes that are not valid UTF-8) it bails with "--target must be valid UTF-8". Target triples are forwarded to cargo/rustc, which expect UTF-8, so the value cannot be used as-is.

Source

Thrown at yazi-build/src/args.rs:54

				Ok(Self::Dest(target))
			}
			Some("install") => {
				ensure!(target.is_empty(), "--target is not valid for the install task");
				Ok(Self::Install(bin_dir))
			}
			Some("--help") => Ok(Self::Help),
			_ => bail!("unknown task: {}", task.display()),
		}
	}

	fn value(args: &mut impl Iterator<Item = OsString>, option: &str) -> Result<OsString> {
		args.next().with_context(|| format!("missing value for {option}"))
	}

	fn target(args: &mut impl Iterator<Item = OsString>) -> Result<String> {
		Self::value(args, "--target")?
			.into_string()
			.map_err(|_| anyhow!("--target must be valid UTF-8"))
	}
}

View on GitHub (pinned to 94abcfa92f)

Solutions

  1. Retype the target cleanly as plain ASCII, e.g. --target x86_64-unknown-linux-gnu
  2. If the string looks correct but still fails, check the shell/locale encoding and re-run under a UTF-8 locale

Example fix

# before (target.txt has invalid bytes)
yazi-build build --target $(cat target.txt)

# after (plain ASCII triple)
yazi-build build --target x86_64-unknown-linux-gnu
Defensive patterns

Strategy: validation

Validate before calling

// Validate before invoking the build tool:
if let Some(t) = &target_os_string {
    ensure!(t.to_str().is_some(), "--target must be valid UTF-8");
}

Type guard

fn is_valid_target(s: &std::ffi::OsStr) -> bool { s.to_str().is_some() }

Prevention

When it happens

Trigger: Invoking the build tool with a --target value containing non-UTF-8 bytes — a mangled locale, a triple generated from unvalidated external data, or hidden bytes pasted into the shell.

Common situations: Broken terminal/shell encoding; scripts composing --target from file contents or environment variables that are not guaranteed UTF-8.

Related errors


AI-assisted analysis of sxyazi/yazi@94abcfa92f (2026-08-16). Data as JSON: /api/errors/453abfe2ac04157d. Report an issue: GitHub.