libexpr: accurately report string interpolation in eval traces (fixes #16253) - #16522
Open
akashhg2007 wants to merge 1 commit into
Open
akashhg2007 wants to merge 1 commit into
akashhg2007 wants to merge 1 commit into
Conversation
…ixOS#16253) Previously, evaluating an interpolated string or string concatenation with '+' (such as 'builtins.mapAttrs (x: y: x + 1)') would report: … while evaluating a path segment in evaluation error traces. Root cause: `ExprConcatStrings::eval` in `src/libexpr/eval.cc` is reused for: 1. String interpolation (`forceString == true`) 2. Path interpolation (`firstType == nPath` after evaluating `path_start`) 3. Binary `+` expressions (which can be integers, floats, paths, or strings) However, when coercing components to string via `state.coerceToString()`, the error context was unconditionally hardcoded to "while evaluating a path segment". Furthermore, `i->eval()` was called with hardcoded "in an operand of '+'". Fix: - Dynamically determine the error context for both `i->eval()` and `state.coerceToString()`: * When `firstType == nPath`, report "while evaluating a path segment". * When `forceString` is true, report "while evaluating a string interpolation". * Otherwise (e.g. `+` concatenation), report "in an operand of '+'". - Update existing characterisation tests that were asserting the old, incorrect `while evaluating a path segment` trace for string interpolation and `+` concatenation. - Add test cases verifying that actual path interpolation and path concatenation continue to correctly report "while evaluating a path segment".
This branch has not been deployed
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.
Summary
Fixes #16253.
When evaluating Nix expressions that encounter a coercion error or evaluation error in string interpolation (e.g.
"${{ __toString = self: null; }}"or"${x: x}") or binary+concatenation with strings (e.g."foo" + 1orbuiltins.mapAttrs (x: y: x + 1) ...), Nix was inaccurately reporting:in the evaluation trace.
Root Cause
In
src/libexpr/eval.cc, the AST nodeExprConcatStrings::evalis used for:forceString == true, e.g.,"${...}"and''${...}'')firstType == nPath, e.g../foo/${...}or./foo + ...)+operator expressions (which can be integers, floats, paths, or strings)In
ExprConcatStrings::eval, the error context passed tostate.coerceToString()was unconditionally hardcoded as"while evaluating a path segment":and
i->eval()was called with"in an operand of '+'"regardless of whether the expression was a string interpolation or a path interpolation.Because of this hardcoded trace context:
forceString == true), any coercion failure (such as returning a non-coercible value from__toString) printed… while evaluating a path segment.+string concatenation (firstType == nString), any coercion failure of the right-hand operand also printed… while evaluating a path segment.Fix
Dynamically choose the appropriate trace context depending on whether the expression is string interpolation, a path segment, or an operand of
+:std::string_view evalErrorCtx = forceString ? "while evaluating a string interpolation" : (!first && firstType == nPath) ? "while evaluating a path segment" : "in an operand of '+'"; i->eval(state, env, vTmp, evalErrorCtx);and for
coerceToString:std::string_view traceContext = firstType == nPath ? "while evaluating a path segment" : forceString ? "while evaluating a string interpolation" : "in an operand of '+'"; auto part = state.coerceToString( i_pos, vTmp, context, traceContext, false, firstType == nString, !first);Trace Comparisons
1. String Interpolation
Before:
After:
2. Binary
+String Concatenation ("foo" + 1)Before:
After:
Test Coverage
while evaluating a path segmenttrace for string interpolation and+concatenation:eval-fail-bad-string-interpolation-1eval-fail-bad-string-interpolation-3eval-fail-bad-string-interpolation-4eval-fail-interpolation-listeval-fail-toString-returns-non-coercibleeval-fail-mapAttrs-4eval-fail-zipAttrsWith-4eval-fail-nested-list-itemswhile evaluating a path segment:eval-fail-bad-path-interpolation(./foo/${x: x})eval-fail-bad-path-concat(./foo + (x: x))eval-fail-bad-string-concat("foo" + (x: x))