Skip to content

Comments

builtins.genericClosure: improve errors#14499

Merged
Ericson2314 merged 3 commits intoNixOS:masterfrom
roberth:genericClosure-errors
Nov 7, 2025
Merged

builtins.genericClosure: improve errors#14499
Ericson2314 merged 3 commits intoNixOS:masterfrom
roberth:genericClosure-errors

Conversation

@roberth
Copy link
Member

@roberth roberth commented Nov 6, 2025

Motivation

The messages weren't too helpful.

Review commit by commit:

  1. Add tests to explicit the behavior (not too interesting)
  2. The change

Context


Add 👍 to pull requests you find important.

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

Covers error conditions for:
- Invalid argument types (not an attrset)
- Missing required attributes (startSet, operator)
- Type mismatches (startSet/operator not correct type)
- Element validation (elements not attrsets, missing key attribute)
- Key comparison errors (incompatible types, uncomparable types)
- Operator return value validation (not a list)
@roberth roberth requested a review from edolstra as a code owner November 6, 2025 20:45
@github-actions github-actions bot added the with-tests Issues related to testing. PRs with tests have some priority label Nov 6, 2025
Show which element(s) are involved at each error point:

- When an element is missing the "key" attribute, show the element
- When an element is not an attribute set, show the element
- When comparing keys fails, show both elements being compared
- When calling operator fails, show which element was being processed

This provides concrete context using ValuePrinter with errorPrintOptions.

Note: errorPrintOptions uses maxDepth=10 by default, which may print
quite deeply nested structures in error messages. This could potentially
be overwhelming, but follows the existing default for error contexts.
We now have functional tests for these. The unit tests added negligible
value while imposing a much higher maintenance cost.

The maintenance cost is high:
  - No automatic accept option
  - They broke 5+ times during this session due to implementation changes (trace count, ordering)
  - They require understanding ANSI escape codes, Uncolored() wrappers, trace reversal
  - They test empty traces HintFmt("") from withTrace(pos, "") - pure implementation detail
  - They're fragile: adding any trace anywhere breaks the exact count assertions

The additional value over functional tests is minimal:
  - Functional tests already verify the error message
  - Functional tests already show trace order and content (as users see it, helps review)
  - Unit tests verify "exactly 3 traces, not 2 or 4" - but users don't count traces
  - Unit tests verify empty traces exist - but users never see them

The white-box testing catches the wrong things:
  - It catches "you added helpful context" as a failure
  - It doesn't catch "the context is confusing" (which functional tests would show)
  - It enforces implementation details that should be allowed to evolve
@roberth roberth force-pushed the genericClosure-errors branch from 4aec461 to 3ee8e45 Compare November 6, 2025 23:29
@roberth roberth added error-messages Confusing messages and better diagnostics language The Nix expression language; parser, interpreter, primops, evaluation, etc labels Nov 7, 2025
ASSERT_TRACE2(
"genericClosure { startSet = [ true ]; operator = item: [{ key = ''a''; }]; }",
TypeError,
HintFmt("expected a set but found %s: %s", "a Boolean", Uncolored(ANSI_CYAN "true" ANSI_NORMAL)),
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW I did add HasSubstrIgnoreANSIMatcher for similar cases so that one doesn't have to worry about exact colors in unit tests. That could alleviate some of the issues.

(Posting this primarily for visibility. I've been using that utility in unit tests where I needed to validate error messages in a somewhat concise manner)

But yeah, it's not worth keeping these tests considering how low-value and fragile they are.

@Ericson2314 Ericson2314 added this pull request to the merge queue Nov 7, 2025
Merged via the queue into NixOS:master with commit 5a97c00 Nov 7, 2025
20 checks passed
@edolstra edolstra mentioned this pull request Dec 9, 2025
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 language The Nix expression language; parser, interpreter, primops, evaluation, etc 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.

3 participants