-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Print the value in error: cannot coerce
messages
#9754
Print the value in error: cannot coerce
messages
#9754
Conversation
d57b43f
to
71ec998
Compare
It seems that some comments from this review apply here as well. |
@9999years do you also have patches for the error message "attempt to call something which is not a function" lying around? Otherwise I'd be happy to write something for that as well :) |
No but that sounds great. Go for it! |
71ec998
to
489f190
Compare
Low-hanging fruit in the spirit of NixOS#9753 and NixOS#9754 (means 9999years did all the hard work already). This basically prints out what was attempted to be called as function, i.e. map (import <nixpkgs> {}) [ 1 2 3 ] now gives the following error message: error: … while calling the 'map' builtin at «string»:1:1: 1| map (import <nixpkgs> {}) [ 1 2 3 ] | ^ … while evaluating the first argument passed to builtins.map error: expected a function but found a set: { _type = "pkgs"; AAAAAASomeThingsFailToEvaluate = «thunk»; AMB-plugins = «thunk»; ArchiSteamFarm = «thunk»; BeatSaberModManager = «thunk»; CHOWTapeModel = «thunk»; ChowCentaur = «thunk»; ChowKick = «thunk»; ChowPhaser = «thunk»; CoinMP = «thunk»; «18783 attributes elided»}
Low-hanging fruit in the spirit of NixOS#9753 and NixOS#9754 (means 9999years did all the hard work already). This basically prints out what was attempted to be called as function, i.e. map (import <nixpkgs> {}) [ 1 2 3 ] now gives the following error message: error: … while calling the 'map' builtin at «string»:1:1: 1| map (import <nixpkgs> {}) [ 1 2 3 ] | ^ … while evaluating the first argument passed to builtins.map error: expected a function but found a set: { _type = "pkgs"; AAAAAASomeThingsFailToEvaluate = «thunk»; AMB-plugins = «thunk»; ArchiSteamFarm = «thunk»; BeatSaberModManager = «thunk»; CHOWTapeModel = «thunk»; ChowCentaur = «thunk»; ChowKick = «thunk»; ChowPhaser = «thunk»; CoinMP = «thunk»; «18783 attributes elided»}
489f190
to
0e822c0
Compare
This extends the `error: cannot coerce a TYPE to a string` message to print the value that could not be coerced. This helps with debugging by making it easier to track down where the value is being produced from, especially in errors with deep or unhelpful stack traces.
0e822c0
to
83bb494
Compare
Thanks again @9999years for another nice improvement. |
This pull request has been mentioned on NixOS Discourse. There might be relevant details there: https://discourse.nixos.org/t/2024-01-22-nix-team-meeting-minutes-117/38838/1 |
I recently encountered this error attempting to build a derivation: ``` error: … while calling the 'derivationStrict' builtin at /builtin/derivation.nix:9:12: (source not available) … while evaluating derivation 'infra-shell' whose name attribute is located at /nix/store/ppm74s0slima7385piksmdcnvcawgs1x-source/pkgs/stdenv/generic/make-derivation.nix:353:7 … while evaluating attribute 'nativeBuildInputs' of derivation 'infra-shell' at /nix/store/ppm74s0slima7385piksmdcnvcawgs1x-source/pkgs/stdenv/generic/make-derivation.nix:397:7: 396| depsBuildBuild = elemAt (elemAt dependencies 0) 0; 397| nativeBuildInputs = elemAt (elemAt dependencies 0) 1; | ^ 398| depsBuildTarget = elemAt (elemAt dependencies 0) 2; error: Dependency is not of a valid type: element 23 of nativeBuildInputs for infra-shell ``` This isn't very helpful; it doesn't tell me what the type of the dependency is. I'm also not directly constructing `stdenv.mkDerivation { nativeBuildInputs = [...]; }`, so it's not obvious what "element 23" would be (the derivation is constructed through several layers of helpers, like many derivations in nixpkgs are). Ultimately, element 23 was a function, which surfaces another issue: ``` nix-repl> throw "Dependency is not of a valid type: ${a: a}" error: … while evaluating a path segment at «string»:1:43: 1| throw "Dependency is not of a valid type: ${a: a}" | ^ error: cannot coerce a function to a string ``` (`builtins.toString` produces similar results.) The Nix language doesn't provide a safe value printing function, so we can't really construct a good error message here. The good news is that we can remove this error checking code from nixpkgs entirely without losing usability. Here's the error message on Nix 2.19.3: ``` error: … while calling the 'derivationStrict' builtin at /derivation-internal.nix:9:12: 8| 9| strict = derivationStrict drvAttrs; | ^ 10| … while evaluating derivation 'my-derivation' whose name attribute is located at /Users/wiggles/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:347:7 … while evaluating attribute 'nativeBuildInputs' of derivation 'my-derivation' at /Users/wiggles/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:391:7: 390| depsBuildBuild = elemAt (elemAt dependencies 0) 0; 391| nativeBuildInputs = elemAt (elemAt dependencies 0) 1; | ^ 392| depsBuildTarget = elemAt (elemAt dependencies 0) 2; error: cannot coerce a function to a string ``` Thanks to PRs like NixOS/nix#9754, these error messages will improve in coming Nix releases: ``` … while evaluating attribute 'nativeBuildInputs' of derivation 'my-derivation' at /Users/wiggles/nixpkgs/pkgs/stdenv/generic/make-derivation.nix:391:7: 390| depsBuildBuild = elemAt (elemAt dependencies 0) 0; 391| nativeBuildInputs = elemAt (elemAt dependencies 0) 1; | ^ 392| depsBuildTarget = elemAt (elemAt dependencies 0) 2; … while evaluating one element of the list error: cannot coerce a function to a string: «lambda @ /Users/wiggles/nixpkgs/xxx-derivation.nix:7:28» ```
Motivation
This extends the
error: cannot coerce a <TYPE> to a string
message to print the value that could not be coerced. This helps with debugging by making it easier to track down where the value is being produced from, especially in errors with deep or unhelpful stack traces.Context
Take two of #9553 now that #9606 is merged.
See #561.
Here's an example of a mistake I made that would have been much easier to fix with this patch. I encountered this error while converting a NixOS configuration that relied on
builtins.currentSystem
to work in a flake. There were several overlays which pinned packages to specific versions, like this:My first try added an
inherit (config.nixpkgs) localSystem;
to theimport
arguments, but that gave me an unhelpful error:Note that the stack trace and error message don't make it clear which set couldn't be coerced to a string, or where the coercion failed. It's fairly easy to figure out what's gone wrong here, but in a repository with 14kloc it was significantly more difficult.
With this patch, the error message makes it clear what's happened (a system attrset has been used in place of a system string):
Priorities
Add 👍 to pull requests you find important.