From 2508facd82e1ed2856632c704fb0f9d33b010842 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Fri, 31 Jan 2025 07:38:11 +0100 Subject: [PATCH] feat: Improve EvalState::require_attrs_select error message (cherry picked from commit 220b6f4123966d818064f4114477ce8676214b90) --- rust/nix-expr/src/eval_state.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/rust/nix-expr/src/eval_state.rs b/rust/nix-expr/src/eval_state.rs index 99e60cf..64111b5 100644 --- a/rust/nix-expr/src/eval_state.rs +++ b/rust/nix-expr/src/eval_state.rs @@ -268,8 +268,21 @@ impl EvalState { v.raw_ptr(), self.eval_state.as_ptr(), attr_name.as_ptr() - ))?; - Ok(Value::new(v2)) + )); + match v2 { + Ok(v2) => Ok(Value::new(v2)), + Err(e) => { + // As of Nix 2.26, the error message is not helpful when it + // is simply missing, so we provide a better one. (Note that + // missing attributes requested by Nix expressions OTOH is a + // different error message which works fine.) + if e.to_string() == "missing attribute" { + bail!("attribute `{}` not found", attr_name.to_string_lossy()); + } else { + Err(e) + } + } + } } } @@ -896,8 +909,7 @@ mod tests { Ok(_) => panic!("expected an error"), Err(e) => { let s = format!("{e:#}"); - // TODO: bad error message from Nix - if !s.contains("missing attribute") { + if !s.contains("attribute `c` not found") { eprintln!("unexpected error message: {}", s); assert!(false); }