From 2a14c2d53b96c17ff6acf571da5824c26468b4aa Mon Sep 17 00:00:00 2001 From: Ivan Petkov Date: Thu, 30 Dec 2021 15:08:45 -0800 Subject: [PATCH] Update crateNameFromCargoToml with better fallback error messages * If a derivation is created without a name *and* we cannot infer a name from a Cargo.toml file, we'll throw a descriptive error message which hints towards the remediation * Otherwise nix can throw a pretty obtuse "derivation has no name" error with no error trace --- lib/crateNameFromCargoToml.nix | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/crateNameFromCargoToml.nix b/lib/crateNameFromCargoToml.nix index 86844ea..f2b5e58 100644 --- a/lib/crateNameFromCargoToml.nix +++ b/lib/crateNameFromCargoToml.nix @@ -2,15 +2,27 @@ { src ? null , cargoToml ? src + /Cargo.toml -, cargoTomlContents ? builtins.readFile cargoToml +, cargoTomlContents ? null , ... }: -let - toml = fromTOML cargoTomlContents; - p = toml.package; -in -{ - inherit (p) version; - pname = p.name; -} +if cargoTomlContents == null && (src == null || !builtins.pathExists cargoToml) +then + throw '' + unable to infer crate name and version. Please make sure the src directory + contains a valid Cargo.toml file, or consider setting a derivation name explicitly + '' +else + let + cargoTomlRealContents = + if cargoTomlContents != null + then cargoTomlContents + else builtins.readFile cargoToml; + + toml = fromTOML cargoTomlRealContents; + p = toml.package; + in + { + inherit (p) version; + pname = p.name; + }