From ece334b53284c2718515d35a81862712c9df06df Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 14 Jul 2024 13:42:12 +0200 Subject: [PATCH 1/3] tests/functional/repl: Characterize side effecting print behavior Reported on matrix by aleksana: https://matrix.to/#/!VRULIdgoKmKPzJZzjj:nixos.org/$7wZp5lUDTd-_u6MYo8kWWcysjtqTiQqP8dLI0RDNVVM?via=nixos.org&via=matrix.org&via=nixos.dev --- .../repl/pretty-print-idempotent.expected | 33 +++++++++++++++++++ .../repl/pretty-print-idempotent.in | 9 +++++ .../repl/pretty-print-idempotent.nix | 19 +++++++++++ 3 files changed, 61 insertions(+) create mode 100644 tests/functional/repl/pretty-print-idempotent.expected create mode 100644 tests/functional/repl/pretty-print-idempotent.in create mode 100644 tests/functional/repl/pretty-print-idempotent.nix diff --git a/tests/functional/repl/pretty-print-idempotent.expected b/tests/functional/repl/pretty-print-idempotent.expected new file mode 100644 index 000000000..e74239564 --- /dev/null +++ b/tests/functional/repl/pretty-print-idempotent.expected @@ -0,0 +1,33 @@ +Nix +Type :? for help. +Added variables. + +{ + homepage = "https://example.com"; +} + +{ homepage = "https://example.com"; } + +{ + layerOne = { ... }; +} + +{ + layerOne = { ... }; +} + +[ + "https://example.com" +] + +[ "https://example.com" ] + +[ + [ ... ] +] + +[ + [ ... ] +] + + diff --git a/tests/functional/repl/pretty-print-idempotent.in b/tests/functional/repl/pretty-print-idempotent.in new file mode 100644 index 000000000..5f865316f --- /dev/null +++ b/tests/functional/repl/pretty-print-idempotent.in @@ -0,0 +1,9 @@ +:l pretty-print-idempotent.nix +oneDeep +oneDeep +twoDeep +twoDeep +oneDeepList +oneDeepList +twoDeepList +twoDeepList diff --git a/tests/functional/repl/pretty-print-idempotent.nix b/tests/functional/repl/pretty-print-idempotent.nix new file mode 100644 index 000000000..68929f387 --- /dev/null +++ b/tests/functional/repl/pretty-print-idempotent.nix @@ -0,0 +1,19 @@ +{ + oneDeep = { + homepage = "https://" + "example.com"; + }; + twoDeep = { + layerOne = { + homepage = "https://" + "example.com"; + }; + }; + + oneDeepList = [ + ("https://" + "example.com") + ]; + twoDeepList = [ + [ + ("https://" + "example.com") + ] + ]; +} From a0635a80b2c3bc89baffd64b1d91d7efa2d2fd3a Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 14 Jul 2024 14:28:21 +0200 Subject: [PATCH 2/3] printAttrs: Force item before determining whether to print multi-line --- src/libexpr/print.cc | 6 ++++++ tests/functional/repl/pretty-print-idempotent.expected | 4 +--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libexpr/print.cc b/src/libexpr/print.cc index 2f377e588..6167abee8 100644 --- a/src/libexpr/print.cc +++ b/src/libexpr/print.cc @@ -299,6 +299,9 @@ private: output << ANSI_NORMAL; } + /** + * @note This may force items. + */ bool shouldPrettyPrintAttrs(AttrVec & v) { if (!options.shouldPrettyPrint() || v.empty()) { @@ -315,6 +318,9 @@ private: return true; } + // It is ok to force the item(s) here, because they will be printed anyway. + state.forceValue(*item, item->determinePos(noPos)); + // Pretty-print single-item attrsets only if they contain nested // structures. auto itemType = item->type(); diff --git a/tests/functional/repl/pretty-print-idempotent.expected b/tests/functional/repl/pretty-print-idempotent.expected index e74239564..949d7a0e6 100644 --- a/tests/functional/repl/pretty-print-idempotent.expected +++ b/tests/functional/repl/pretty-print-idempotent.expected @@ -2,9 +2,7 @@ Nix Type :? for help. Added variables. -{ - homepage = "https://example.com"; -} +{ homepage = "https://example.com"; } { homepage = "https://example.com"; } From da3eff60bc35763270f57c3cc17e613a19513709 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Sun, 14 Jul 2024 14:31:20 +0200 Subject: [PATCH 3/3] printList: Force item before determining whether to print multi-line --- src/libexpr/print.cc | 6 ++++++ tests/functional/repl/pretty-print-idempotent.expected | 4 +--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/libexpr/print.cc b/src/libexpr/print.cc index 6167abee8..bc17d6bfe 100644 --- a/src/libexpr/print.cc +++ b/src/libexpr/print.cc @@ -377,6 +377,9 @@ private: } } + /** + * @note This may force items. + */ bool shouldPrettyPrintList(std::span list) { if (!options.shouldPrettyPrint() || list.empty()) { @@ -393,6 +396,9 @@ private: return true; } + // It is ok to force the item(s) here, because they will be printed anyway. + state.forceValue(*item, item->determinePos(noPos)); + // Pretty-print single-item lists only if they contain nested // structures. auto itemType = item->type(); diff --git a/tests/functional/repl/pretty-print-idempotent.expected b/tests/functional/repl/pretty-print-idempotent.expected index 949d7a0e6..f38b9b569 100644 --- a/tests/functional/repl/pretty-print-idempotent.expected +++ b/tests/functional/repl/pretty-print-idempotent.expected @@ -14,9 +14,7 @@ Added variables. layerOne = { ... }; } -[ - "https://example.com" -] +[ "https://example.com" ] [ "https://example.com" ]