Skip to content
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

Make @nix json structured build log parsing warn instead of fail #11921

Merged
merged 7 commits into from
Dec 9, 2024

Conversation

roberth
Copy link
Member

@roberth roberth commented Nov 20, 2024

Motivation

A build may log with @nix something without the intent to write Nix structured logs, resulting in an obscure error.
This makes it easy to understand and continues the build.

Example:

echo '@nix ]['
bad JSON log message from builder: [json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected ']'; expected '[', '{', or a literal
@nix ][

Context

Cherry-picked from https://gerrit.lix.systems/c/lix/+/2057
Author @lheckemann

Priorities and Process

Add 👍 to pull requests you find important.

The Nix maintainer team uses a GitHub project board to schedule and track reviews.

@roberth roberth added bug derivation-build The process of building an individual derivation (see also sandbox label) labels Nov 20, 2024
@roberth roberth requested a review from edolstra as a code owner November 20, 2024 10:31
@github-actions github-actions bot added the with-tests Issues related to testing. PRs with tests have some priority label Nov 20, 2024
@roberth roberth added backport 2.24-maintenance Automatically creates a PR against the branch backport 2.25-maintenance Automatically creates a PR against the branch labels Nov 20, 2024
return true;
} catch (nlohmann::json::exception &e) {
warn(
"warning: Unable to handle a JSON message from the builder: %s",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't like how this makes assumptions about where this function is being used.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spot on. That also fixes a technically incorrect message, although it was probably never seen.
Now:

            if (handleJSONLogMessage(s, worker.act, worker.hook->activities, "the build hook", true))

Copy link
Member

@Ericson2314 Ericson2314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on the idea, just don't like the anti-modular way the warning is done.

@roberth
Copy link
Member Author

roberth commented Nov 21, 2024

Interestingly, the post-build-hook does not have @nix parsing logic, so that's what that's not in the diff.

@roberth roberth requested a review from Ericson2314 November 21, 2024 15:09
Copy link
Member

@Ericson2314 Ericson2314 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great now!

@Mic92
Copy link
Member

Mic92 commented Nov 24, 2024

@roberth CI fails.

@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2024-11-20-nix-team-meeting-minutes-196/56688/1

@pwaller
Copy link
Contributor

pwaller commented Dec 6, 2024

Forgive me if this is off topic, but this might be of interest here: right now if I run nix develop -f . hello with nix 2.24.10 on nixos/nixpkgs@staging I get:

error: [json.exception.parse_error.101] parse error at line 38, column 445: syntax error while parsing value - invalid string: control character U+001E (RS) must be escaped to \u001E; last read: '" \n    local sep=\"$1\";\n    local name=\"$2\";\n    local type oldifs;\n    if type=$(declare -p \"$name\" 2> /dev/null); then\n        local -n nameref=\"$name\";\n        case \"${type#* }\" in \n            -A*)\n                echo \"concatStringsSep(): ERROR: trying to use concatStringsSep on an associative array.\" 1>&2;\n                return 1\n            ;;\n            -a*)\n                local IFS='<U+001E>'

With --verbose --debug the line that precedes the error is "reading environment file '/nix/store/bjazykbgvidfl19ma7gkm2jsxp0wkckn-hello-2.12.1-env'", and this seems to occur buliding any package I have tried.

roberth and others added 7 commits December 9, 2024 15:59
Before this change, expressions like:

with import <nixpkgs> {};
runCommand "foo" {} ''
  echo '@nix {}' >&$NIX_LOG_FD
''

would result in Lix crashing, because accessing nonexistent fields of
a JSON object throws an exception.

Rather than handling each field individually, we just catch JSON
exceptions wholesale. Since these log messages are an unusual
circumstance, log a warning when this happens.

Fixes #544.

Change-Id: Idc2d8acf6e37046b3ec212f42e29269163dca893
(cherry picked from commit e55cd3beea710db727fd966f265a1b715b7285f3)
@wolfgangwalther
Copy link

Forgive me if this is off topic, but this might be of interest here: right now if I run nix develop -f . hello with nix 2.24.10 on nixos/nixpkgs@staging I get:

error: [json.exception.parse_error.101] parse error at line 38, column 445: syntax error while parsing value - invalid string: control character U+001E (RS) must be escaped to \u001E; last read: '" \n    local sep=\"$1\";\n    local name=\"$2\";\n    local type oldifs;\n    if type=$(declare -p \"$name\" 2> /dev/null); then\n        local -n nameref=\"$name\";\n        case \"${type#* }\" in \n            -A*)\n                echo \"concatStringsSep(): ERROR: trying to use concatStringsSep on an associative array.\" 1>&2;\n                return 1\n            ;;\n            -a*)\n                local IFS='<U+001E>'

With --verbose --debug the line that precedes the error is "reading environment file '/nix/store/bjazykbgvidfl19ma7gkm2jsxp0wkckn-hello-2.12.1-env'", and this seems to occur buliding any package I have tried.

This has been fixed in nixpkgs in NixOS/nixpkgs#363672.

@Ericson2314 Ericson2314 merged commit 747256c into master Dec 9, 2024
20 checks passed
@Ericson2314 Ericson2314 deleted the log-json-tolerant branch December 9, 2024 21:39
Mic92 added a commit that referenced this pull request Dec 10, 2024
…1921

Make @nix json structured build log parsing warn instead of fail (backport #11921)
Mic92 added a commit that referenced this pull request Dec 10, 2024
…1921

Make @nix json structured build log parsing warn instead of fail (backport #11921)
@nixos-discourse
Copy link

This pull request has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/2024-12-09-nix-team-meeting-minutes-201/57280/1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 2.24-maintenance Automatically creates a PR against the branch backport 2.25-maintenance Automatically creates a PR against the branch bug derivation-build The process of building an individual derivation (see also sandbox label) with-tests Issues related to testing. PRs with tests have some priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants