lib.customisation: fix error message when running in nix repl

This code was more careful before
<dd435697b3>
(it didn't assume that `unsafeGetAttrPos` always returns a non-null
location). Unfortunately, `unsafeGetAttrPos` *does* return `null` when
dealing with `nix repl`:

```
nix-repl> f = {foo}: foo

nix-repl> builtins.unsafeGetAttrPos "foo" (builtins.functionArgs f)
null
```

Here's how to reproduce the issue.

*Before* this fix:

```
nix-repl> f = {foo}: foo

nix-repl> myCallPackage = lib.callPackageWith {}

nix-repl> myCallPackage f {}
error:
       … while calling the 'abort' builtin
         at /home/jeremy/src/github.com/NixOS/nixpkgs/lib/customisation.nix:323:7:
          322|     else
          323|       abort "lib.customisation.callPackageWith: ${error}";
             |       ^
          324|

       … while selecting an attribute
         at /home/jeremy/src/github.com/NixOS/nixpkgs/lib/customisation.nix:310:14:
          309|         "Function called without required argument \"${arg}\" at "
          310|         + "${loc.file}:${toString loc.line}${prettySuggestions (getSuggestions arg)}";
             |              ^
          311|

       error: expected a set but found null: null

```

*After*:

```
nix-repl> f = {foo}: foo

nix-repl> myCallPackage = lib.callPackageWith {}

nix-repl> myCallPackage f {}
error:
       … while calling the 'abort' builtin
         at /home/jeremy/src/github.com/NixOS/nixpkgs/lib/customisation.nix:332:7:
          331|     # Inputs
          332|
             |       ^
          333|     `autoArgs`

       error: evaluation aborted with the following error message: 'lib.customisation.callPackageWith: Function called without required argument "foo" at <unknown location>'
```
This commit is contained in:
Jeremy Fleischman
2025-10-16 08:21:59 -04:00
parent 5d1f6df5e1
commit 775ce27666

View File

@@ -305,9 +305,10 @@ rec {
arg: arg:
let let
loc = unsafeGetAttrPos arg fargs; loc = unsafeGetAttrPos arg fargs;
loc' = if loc != null then loc.file + ":" + toString loc.line else "<unknown location>";
in in
"Function called without required argument \"${arg}\" at " "Function called without required argument \"${arg}\" at "
+ "${loc.file}:${toString loc.line}${prettySuggestions (getSuggestions arg)}"; + "${loc'}${prettySuggestions (getSuggestions arg)}";
# Only show the error for the first missing argument # Only show the error for the first missing argument
error = errorForArg (head (attrNames missingArgs)); error = errorForArg (head (attrNames missingArgs));