When working with nullable/optional values or external/runtime-provided fields, never assume presence or a specific concrete type/spelling. Instead: (1) explicitly treat inputs as optional, (2) provide deterministic fallbacks (often empty string/None/previous-state), (3) avoid dereferencing missing values, and (4) add tests for both “value present” and “value absent / alternate field present” cases.
Apply this especially to:
"") rather than constructing partial commands/paths.Example (runtime field fallback):
fn nu_home_path(env: &HashMap<String, String>) -> String {
// Prefer $nu.home-path when exposed; fall back to $nu.home-dir, then finally $HOME.
env.get("nu.home-path")
.or_else(|| env.get("nu.home-dir"))
.or_else(|| env.get("HOME"))
.cloned()
.unwrap_or_else(|| "".to_string())
}
Checklist for PRs:
Enter the URL of a public GitHub repository