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
Fix derivation load assertion errors #8760
Merged
Ericson2314
merged 1 commit into
NixOS:master
from
iFreilicht:fix-json-load-assertion-errors
Aug 7, 2023
Merged
Fix derivation load assertion errors #8760
Ericson2314
merged 1 commit into
NixOS:master
from
iFreilicht:fix-json-load-assertion-errors
Aug 7, 2023
Conversation
This file contains 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
8 tasks
iFreilicht
force-pushed
the
fix-json-load-assertion-errors
branch
from
July 30, 2023 20:48
2057dc1
to
b72be3b
Compare
roberth
added
error-messages
Confusing messages and better diagnostics
store
Issues and pull requests concerning the Nix store
labels
Jul 31, 2023
Ericson2314
reviewed
Jul 31, 2023
Ericson2314
reviewed
Jul 31, 2023
thufschmitt
added
the
idea approved
The given proposal has been discussed and approved by the Nix team. An implementation is welcome.
label
Jul 31, 2023
Ericson2314
reviewed
Jul 31, 2023
iFreilicht
force-pushed
the
fix-json-load-assertion-errors
branch
from
July 31, 2023 18:19
b72be3b
to
c02e6e6
Compare
github-actions
bot
removed
the
store
Issues and pull requests concerning the Nix store
label
Jul 31, 2023
iFreilicht
force-pushed
the
fix-json-load-assertion-errors
branch
from
July 31, 2023 18:25
c02e6e6
to
03b676f
Compare
iFreilicht
force-pushed
the
fix-json-load-assertion-errors
branch
from
August 2, 2023 19:57
03b676f
to
45f490b
Compare
Ericson2314
reviewed
Aug 3, 2023
Ericson2314
reviewed
Aug 3, 2023
Ericson2314
reviewed
Aug 3, 2023
Ericson2314
reviewed
Aug 3, 2023
Ericson2314
reviewed
Aug 3, 2023
Ericson2314
reviewed
Aug 3, 2023
When loading a derivation from a JSON, malformed input would trigger cryptic "assertion failed" errors. Simply replacing calls to `operator []` with calls to `.at()` was not enough, as this would cause json.execptions to be printed verbatim. Display nice error messages instead and give some indication where the error happened. *Before:* ``` $ echo 4 | nix derivation add error: [json.exception.type_error.305] cannot use operator[] with a string argument with number $ nix derivation show nixpkgs#hello | nix derivation add Assertion failed: (it != m_value.object->end()), function operator[], file /nix/store/8h9pxgq1776ns6qi5arx08ifgnhmgl22-nlohmann_json-3.11.2/include/nlohmann/json.hpp, line 2135. $ nix derivation show nixpkgs#hello | jq '.[] | .name = 5' | nix derivation add error: [json.exception.type_error.302] type must be string, but is object $ nix derivation show nixpkgs#hello | jq '.[] | .outputs = { out: "/nix/store/8j3f8j-hello" }' | nix derivation add error: [json.exception.type_error.302] type must be object, but is string ``` *After:* ``` $ echo 4 | nix derivation add error: Expected JSON of derivation to be of type 'object', but it is of type 'number' $ nix derivation show nixpkgs#hello | nix derivation add error: Expected JSON object to contain key 'name' but it doesn't $ nix derivation show nixpkgs#hello | jq '.[] | .name = 5' | nix derivation add error: Expected JSON value to be of type 'string' but it is of type 'number' $ nix derivation show nixpkgs#hello | jq '.[] | .outputs = { out: "/nix/store/8j3f8j-hello" }' | nix derivation add error: … while reading key 'outputs' error: Expected JSON value to be of type 'object' but it is of type 'string' ```
iFreilicht
force-pushed
the
fix-json-load-assertion-errors
branch
from
August 4, 2023 23:35
45f490b
to
3fefc2b
Compare
Ericson2314
approved these changes
Aug 7, 2023
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! This looks quite good now.
BTW see nlohmann/json#4100 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
error-messages
Confusing messages and better diagnostics
idea approved
The given proposal has been discussed and approved by the Nix team. An implementation is welcome.
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.
Display nice error messages instead and give some indication where the error happened.
Before:
After:
Motivation
When loading a derivation from a JSON, malformed input would trigger cryptic "assertion failed" errors. Simply replacing calls to
operator []
with calls to.at()
was not good enough IMO, as this would cause json.execptions to be printed verbatim.Context
Fixes the complaint about bad error messages in #8747 as requested in this comment.
The PR adds a new method
valueOfTypeAt
that accesses the value via a key in a JSON object, ensures it is of a certain type and errors with a decent explanation text if the key does not exist or it is of the wrong type.It specifically does not check whether
map
is actually an object. This should be done at the callsite, where information about the context ofmap
is available.Checklist for maintainers
Maintainers: tick if completed or explain if not relevant
tests/**.sh
src/*/tests
tests/nixos/*
Priorities
Add 👍 to pull requests you find important.