Print the value in error: cannot coerce messages#9754
Merged
roberth merged 1 commit intoNixOS:masterfrom Jan 24, 2024
Merged
Conversation
d57b43f to
71ec998
Compare
Member
|
It seems that some comments from this review apply here as well. |
Member
|
@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 :) |
Contributor
Author
No but that sounds great. Go for it! |
71ec998 to
489f190
Compare
Ma27
added a commit
to Ma27/nix
that referenced
this pull request
Jan 20, 2024
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»}
Ma27
added a commit
to Ma27/nix
that referenced
this pull request
Jan 22, 2024
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
roberth
reviewed
Jan 23, 2024
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
roberth
approved these changes
Jan 24, 2024
Member
|
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 |
Closed
13 tasks
9999years
added a commit
to 9999years/nixpkgs
that referenced
this pull request
Mar 7, 2024
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»
```
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
This extends the
error: cannot coerce a <TYPE> to a stringmessage 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.currentSystemto 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 theimportarguments, 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.