nixfmt_rs 0.5.1

Rust implementation of nixfmt with exact Haskell compatibility
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
//! Consolidated regression tests

use crate::oracle_tests;
use crate::tests_common::{assert_parse_error_contains, assert_parse_rejected};

oracle_tests! {
    test_ast_format;

    regression_string_interpolation_selectors => [
        r#"x."y""#,
        r#"x.${"y"}"#,
        r"x.${foo}",
    ],

    regression_or_as_identifier => ["or"],

    // nixfmt a061bd5: a `/* lang */` block comment is only a language
    // annotation when at most one newline separates it from the string.
    // LanguageAnnotation must be wrapped in brackets in --ast output.
    regression_language_annotation => [
        "/* python */\n\n\"x\"",
        "/* python */\n\"x\"",
        "/* python */ '' ''",
    ],

    // nixfmt 1f3fa2e / https://github.com/NixOS/nixfmt/issues/351
    regression_chained_prefix_operators => [
        "(--1)",
        "(---1)",
        "(!!a)",
        "(!!!!a)",
    ],

    // Float lexer edge cases (also covered by fixtures/nixfmt/correct/numbers.nix
    // for idempotency, pinned here for --ast parity).
    regression_float_literals => [
        ".5",
        "5.",
        "1.0e2",
        ".5e2",
        "00.5",
    ],

    regression_attrset_string_interpolated_key => [
        r#"{"a" = 1;}"#,
        r#"{${"a"} = 1;}"#,
    ],

    // Paths starting with identifiers should be parsed as paths, not applications/division
    // Bug 1: "import common/file.nix" was parsed as application of import to common
    // Bug 2: "x = common/file.nix" was parsed as division "x = common / file.nix"
    // Bug 3: "x = foo-bar/baz.nix" was parsed as division with selection
    // Bug 4: "metaCommon // { ... }" was incorrectly detected as path "metaCommon//"
    // Bug 5: "(a / b)" was incorrectly detected as path starting with "a/"
    regression_import_relative_path => [
        r#"{
  a = import common/acme/server/snakeoil-certs.nix;
  b = common/file.nix;
  c = foo-bar/baz.nix;
  d = metaCommon // { mainProgram = "gopeed"; };
  e = (a / b);
  f = ((targetPodcastSize + lameMp3FileAdjust) / (lameMp3Bitrate / 8));
}
"#,
    ],

    regression_let_string_interpolated_key => [
        r#"let "foo" = 1; in foo"#,
        r#"let ${"foo"} = 1; in foo"#,
    ],

    regression_import_path_application => ["import ./foo.nix self"],

    regression_multiline_string_indentation => ["''\n  case\n    ;;\n''\n"],

    regression_trailing_comment => ["{ test = foo; # trailing comment\n}"],

    // Closing bracket should be on line 3, not line 2.
    test_sourceline_multiline_list => ["[\n  \"foo\"\n]"],

    // Comments before && operators are dropped when expressions contain
    // interpolation selectors like self.packages.${system}.isLinux
    // The third && operator is missing its preTrivia comment
    regression_comment_before_and_with_selectors => [
        r#"{
  x =
    lib.optionalAttrs
      (
        self.packages.${system}.isLinux
        # comment 1
        && self.packages.${system}.isPower64
        # comment 2
        && system != "armv6l-linux"
        # comment 3
        && system != "riscv64-linux"
      )
      {
        tests = {};
      };
}"#,
    ],

    regression_emptyline_pretrivia_inline => ["\n\nlet x = 1; in x"],

    // `?` binds tighter than `!`: Not(HasAttr(a)...).
    regression_not_member_check => ["!a ? b"],

    // `->` binds looser than `||`: (a || b) -> c.
    regression_implies_precedence => ["a || b -> c"],

    // Right-associative `+` restructuring must not apply across `-`: ((1 + 2) - 3).
    regression_mixed_add_sub_associativity => ["1 + 2 - 3"],

    // From nixpkgs/nixos/modules/config/resolvconf.nix lines 18-37
    regression_chained_string_concatenation => [
        r"''
  line1
''
+ lib.optionalString cond1 ''
  line2
''
+ lib.optionalString cond2 ''
  line3
''
+ lib.optionalString cond3 ''
  line4
''
+ cfg.extra",
    ],

    // Comments inside otherwise-empty sets / lists / let-bindings should be
    // separate Comments items, not folded into preTrivia of the close token.
    regression_empty_container_with_comment => [
        "{\n  # comment\n}",
        "[\n  # comment\n]",
        "let\n  # comment\nin x",
    ],

    // Literal ESC (0x1b) must be preserved in the AST, not stripped.
    // nixfmt outputs \x9 for tab, not \x09 (nixpkgs/lib/generators.nix).
    regression_ansi_escape_codes_in_strings => [
        "\"\x1b[1;31mtest\x1b[0m\"",
        "\"\t\"",
    ],

    // From nixpkgs/nixos/release.nix line 262-267
    regression_dot_selector_on_newline => [
        r"{
  armv6l-linux = ./foo.nix;
}
.${system}",
    ],

    // From nixpkgs/lib/generators.nix line 729
    regression_context_parameter_variants => [
        "{ }@args: args",
        "{...}@args: args",
    ],

    // The lexer used to hit '#' raw after manually parsing these constructs
    // and fail with "unexpected character: '#'".
    // From nixpkgs/nixos/tests/public-inbox.nix line 97
    regression_inline_comments_after_strings_and_paths => [
        r#"[
  "simple" # comment after simple string
  ''
    indented
  '' # comment after indented string
  ./path # comment after path
  "end"
]"#,
    ],

    regression_old_style_let => ["let { body = 1; }"],

    // Zero-width space (U+200B) should be displayed as \x200b in AST output
    // From nixpkgs/pkgs/by-name/li/libcaca/package.nix line 68
    // Soft hyphen (U+00AD) is a Format character (Cf category) and should be escaped as \xad
    // From nixpkgs/pkgs/tools/graphics/diagrams-builder/default.nix line 10
    regression_unicode_escape_in_string => [
        "\"famous \u{200B}AAlib library\"",
        "\"\u{00AD}~~~\"",
    ],

    // Apply(mkDefault, /tmp), not Path("mkDefault/tmp").
    // From nixpkgs/nixos/modules/services/monitoring/prometheus/exporters.nix line 353
    regression_identifier_slash_path => ["mkDefault /tmp"],

    // "http://example.com" was tokenized as "http:" followed by Update ("//").
    // From nix/tests/functional/lang/parse-okay-regression-20041027.nix line 6
    regression_unquoted_url => ["{ url = http://example.com/path; }"],

    // From nix/tests/functional/lang/eval-okay-comments.nix lines 42-45
    regression_decorated_multiline_comment => [
        r#"/*
 * Multiline, decorated comments
 * # This ain't a nest'd comm'nt
 */
"x""#,
    ],

    // From nix/tests/functional/lang/parse-fail-dup-attrs-2.nix
    regression_trailing_empty_line_before_close => [
        "let {\n  x = 1;\n  \n}\n",
        "{\n  foo = 1;\n\n}\n",
    ],

    // `2 > 1 == 1 < 2` parses as `(2 > 1) == (1 < 2)`; the chain ban is per-precedence.
    // From nix/tests/functional/lang/eval-okay-arithmetic.nix line 50
    regression_chained_comparison_operators => ["2 > 1 == 1 < 2"],

    // Line numbers after multi-byte chars in `''..''` once diverged from nixfmt.
    // From nix/tests/functional/nar-access.nix lines 6-20
    regression_multiline_string_unicode_line_numbers => [
        r#"{
  x = ''
    line1
ä"§
  '';
}"#,
    ],

    // nixfmt 1.2.0: single-line indented strings without `"` or `\` become SimpleString,
    // with `''$` -> `\$` and `'''` -> `''` escape conversion.
    // Kept as IndentedString when content contains `"` or `\`.
    regression_indented_string_to_simple => [
        "''hello ${x} '''quoted''' ''$var''",
        r#"''has"quote''"#,
        r"''back\slash''",
    ],

    // ========================================================================
    // Migrated from legacy hand-written unit tests (parser/tests.rs,
    // string_path_tests.rs, parameter_tests.rs, operator_associativity_test.rs,
    // coverage_test.rs). Kept only inputs not already covered above or in
    // ast_format_tests.rs.
    // ========================================================================

    regression_empty_simple_string => [r#""""#],

    regression_string_multiple_interpolations => [r#""${a} and ${b}""#],

    regression_string_dollar_dollar => [r#""$$test""#],

    regression_empty_indented_string => ["''''"],

    regression_indented_string_escape_sequences => [r"''test ''$ and ''' and ''\ ''"],

    regression_path_relative_dotdot => ["../foo"],

    regression_path_with_interpolation => ["./foo/${bar}/baz"],

    // `f -5` must parse as subtraction, not `f(-5)`
    regression_subtraction_not_application => ["f -5"],

    regression_application_with_parenthesized_negation => ["f (-5)"],

    regression_inherit_multiple_names => ["{ inherit pkgs lib stdenv; }"],

    regression_concat_right_associative => ["[1] ++ [2] ++ [3]"],

    regression_update_right_associative => ["{a=1;} // {b=2;} // {c=3;}"],

    regression_plus_right_associative => ["1 + 2 + 3"],

    regression_minus_left_associative => ["1 - 2 - 3"],

    regression_empty_set_parameter => ["{}: 42"],

    regression_pipe_forward_operator => ["a |> b"],

    regression_pipe_backward_operator => ["a <| b"],

    regression_member_check_on_operation => ["(x + y) ? foo"],

    regression_crlf_between_tokens => ["x\r\n+\r\ny"],
}

// ---------------------------------------------------------------------------
// Tests with custom logic / non-`test_ast_format` assertions
// ---------------------------------------------------------------------------

#[test]
fn regression_or_operator_deprecated_syntax() {
    // From nix/tests/functional/lang/eval-okay-deprecate-cursed-or.nix line 3
    // In `[ (x: x) or ]`, the `or` is actually the binary `or` operator,
    // not a standalone identifier. Nix parses this as [(x: x) or <lookup-or>].
    // This is deprecated/ambiguous syntax that Nix accepts with warnings.
    // Known limitation: we parse this as 2 list items instead of 1; the
    // formatted output is still semantically valid, so only acceptance is
    // asserted here.
    assert!(
        crate::parse("let or = 1; in [ (x: x) or ]").is_ok(),
        "we currently accept this but parse it incorrectly"
    );
}

#[test]
fn regression_comparison_chain_should_fail() {
    assert_parse_rejected("a == b == c");
}

/// Unterminated block comments are rejected, matching Nix.
#[test]
fn regression_unclosed_block_comment_rejected() {
    assert_parse_rejected("t /*b*");
    assert_parse_rejected("/* unclosed");
    assert!(crate::parse("t /* b */").is_ok());
}
/// `or` is a soft keyword in Nix: it cannot be used as a lambda parameter
/// or set-pattern formal, but works in attribute paths and binding names.
#[test]
fn regression_or_rejected_as_lambda_param() {
    assert_parse_rejected("or: or");
    assert_parse_rejected("{ or }: or");
    assert_parse_rejected("{ or ? 1 }: or");
    assert!(crate::parse("{ or = 1; }").is_ok());
    assert!(crate::parse("{ or = 1; }.or").is_ok());
    assert!(crate::parse("x.a or 1").is_ok());
    // TODO: Nix also rejects `or` in expression position (e.g. `[ or ]`,
    // `let or = 1; in or`), but we still accept it.  Fixing this requires
    // changes to term parsing that may interact with the deprecated
    // or-as-identifier support.
}

/// Nix lexes `ident.<pathchars>/` as a path (e.g. `a.b/c`, `pkg.org/x`).
/// Without special handling, the parser consumed `a.b` as an attrpath
/// and treated `/c` as division.
#[test]
fn regression_ident_dot_path() {
    assert!(crate::parse("a.b/c").is_ok());
    assert!(crate::parse("x.y.z/w").is_ok());
    assert!(crate::parse("ple.org/a").is_ok());
    // Space breaks the path: `a` is a separate identifier
    // (.b/c is still a valid path on its own, so the whole
    // expression parses as function application)
    assert!(crate::parse("a .b/c").is_ok());
}

/// `//` in the middle of a path terminates the path; Nix treats `//`
/// as the update operator.
#[test]
fn regression_double_slash_ends_path() {
    // a/b is a path, a//b is `a // b` (update)
    assert!(crate::parse("a/b").is_ok());
    assert!(crate::parse("a/b//c").is_ok()); // path `a/b` then `// c`
}

/// Nix treats `.<pathchars>/` as a relative path, not just `./` and `../`.
#[test]
fn regression_dot_pathchars_slash_is_path() {
    assert!(crate::parse(".foo/bar").is_ok());
    assert!(crate::parse(".lceif/nix").is_ok());
    assert!(crate::parse(".123/x").is_ok());
    // Bare `.a` without `/` is not a path
    assert_parse_rejected(".a");
}

/// `~` is only valid at the start of a path (`~/...`), not in the middle.
#[test]
fn regression_tilde_not_path_char() {
    assert_parse_rejected("./a~b");
    assert!(crate::parse("~/a").is_ok());
}

/// Ellipsis after a formal requires a preceding comma.
#[test]
fn regression_ellipsis_requires_comma() {
    assert_parse_rejected("{ a ... }: a");
    assert_parse_rejected("{ b ? 1 ... }: b");
    assert!(crate::parse("{ a, ... }: a").is_ok());
    assert!(crate::parse("{ ... }: 1").is_ok());
}

/// A dot-path after a term must not be consumed as postfix selection.
/// `"f:l" .o/b` is application of string to path, not `("f:l").o / b`.
#[test]
fn regression_dot_path_not_selection() {
    let src = "\"f:l\" .o/b";
    let first = crate::format(src).unwrap();
    let second = crate::format(&first).unwrap();
    assert_eq!(first, second, "not idempotent: {first:?} vs {second:?}");
}

/// `/*` inside a string after an interpolation must not be parsed as
/// a block comment start.
#[test]
fn regression_slash_star_in_string_after_interp() {
    assert!(crate::parse(r"''${x}/*.tar''").is_ok());
    assert!(crate::parse(r#""${x}/*foo""#).is_ok());
}

/// Integer literals that overflow signed 64-bit are rejected at lex time,
/// matching `nix-instantiate --parse` behaviour.
#[test]
fn regression_integer_overflow_rejected() {
    // i64::MAX is fine
    assert!(crate::parse("9223372036854775807").is_ok());
    // i64::MAX + 1 overflows
    assert_parse_rejected("9223372036854775808");
    // Clearly out of range
    assert_parse_rejected("55555555555555555555555555555");
    // Leading zeros still overflow if the value does
    assert_parse_rejected("00009223372036854775808");
}

/// Incomplete `<path>` forms lex as `Less` and are then rejected by the
/// parser, as in Nix and Haskell nixfmt.
#[test]
fn regression_incomplete_env_path_rejected() {
    assert_parse_rejected("[ a <b ]");
    assert_parse_rejected("<a//b>");
    assert_parse_rejected("<>");
}

#[test]
fn regression_path_trailing_slash_current() {
    assert_parse_rejected("./");
}

#[test]
fn regression_crlf_line_endings() {
    // From nix/tests/functional/lang/parse-okay-crlf.nix. Bare CR after a comment
    // is rejected by Haskell nixfmt but accepted here for cross-platform input.
    let input = "rec {\n  x =\n  # Comment\r  y;\n}\n";
    let result = crate::parse(input);
    assert!(
        result.is_ok(),
        "Failed to parse input with CRLF/bare CR: {:?}",
        result.err()
    );
}

#[test]
fn regression_or_operator_with_application() {
    // `or` after a non-selection term is the (deprecated) identifier `or`,
    // not the selection-default operator. Upstream Haskell nixfmt silently
    // drops the `or <term>` clause here; we deliberately do not.
    // From nix/tests/functional/lang/eval-okay-attrs5.nix line 20.
    let out = crate::format("(fold or [] [true false false])").unwrap();
    assert!(
        out.contains("or") && out.contains("[ ]"),
        "`or [ ]` must not be dropped, got: {out}"
    );
}

#[test]
fn regression_utf8_identifier() {
    // Nix identifiers are ASCII-only: [a-zA-Z_][a-zA-Z0-9_'-]*
    // From nix/tests/functional/lang/parse-fail-utf8.nix
    assert_parse_rejected("123 é 4");
}

#[test]
fn regression_duplicate_function_formals() {
    // From nix/tests/functional/lang/parse-fail-dup-formals.nix
    assert_parse_rejected("{x, y, x}: x");
}

#[test]
fn regression_pattern_shadows_formal() {
    // From nix/tests/functional/lang/parse-fail-patterns-1.nix
    assert_parse_rejected("args@{args, x, y, z}: x");
}

#[test]
fn regression_at_without_colon_error() {
    // Matches `nix-instantiate --parse`: after `id @` only a set pattern may follow.
    assert_parse_error_contains("x @ y", "expected '{', found 'y'");
    assert_parse_error_contains("x @ { }", "@ is only valid in lambda parameters");
}

/// Nix only allows `id @ { ... }` or `{ ... } @ id`; we used to accept nested
/// `@` and `id @ id` because `validate_context_parameter` fell through on the
/// catch-all arm. Mirrors `nix-instantiate --parse` rejection.
#[test]
fn regression_context_parameter_shape() {
    assert_parse_error_contains("a@b@{ }: 1", "expected '{'");
    assert_parse_error_contains("{ }@a@b: 1", "expected ':'");
    assert_parse_error_contains("a@{ }@b: 1", "expected ':'");
    assert_parse_error_contains("a@b: 1", "expected '{'");
    assert_parse_error_contains("{ }@{ }: 1", "expected identifier");
}

/// After at least one `.selector`, `or` is the selection-default operator and
/// *must* be followed by a default expression. We used to backtrack and treat
/// it as the deprecated identifier `or`, accepting `a.b or ]`. Nix and
/// upstream nixfmt both reject. (`a or`, with no selectors, remains the
/// identifier and is handled in the application layer.)
#[test]
fn regression_or_after_selector_requires_default() {
    assert_parse_error_contains("[ a.b or ]", "expected expression");
    assert_parse_error_contains("{ x = a.b or; }", "expected expression");
    // No selectors: still the identifier.
    crate::parse("[ a or ]").expect("`or` without preceding selector is the identifier");
    crate::parse("a or").expect("`or` without preceding selector is the identifier");
}

#[test]
fn regression_single_ampersand_error() {
    assert_parse_error_contains("a & b", "expected '&&', found '&'");
}

#[test]
fn regression_single_pipe_error() {
    assert_parse_error_contains("a | b", "expected one of '||', '|>', found '|'");
}

#[test]
fn regression_ellipsis_without_colon_error() {
    assert_parse_error_contains("{ ... }", "{ ... } must be followed by ':' or '@'");
}

#[test]
fn regression_set_parameter_without_colon_error() {
    assert_parse_rejected("{ x, y }");
}

#[test]
fn regression_single_dollar_error() {
    let err = crate::parse("$x").unwrap_err().to_string();
    assert!(err.contains("unexpected '$'") || err.contains("expected '${'"));
}

#[test]
fn regression_unexpected_character_error() {
    let err = crate::parse("x ^ y").unwrap_err().to_string();
    assert!(err.contains("unexpected character") || err.contains("'^'"));
}

#[test]
fn regression_non_utf8_input() {
    // From nix/tests/functional/lang/eval-fail-toJSON-non-utf-8.nix.
    // Actual non-UTF-8 handling lives in main.rs; here exercise the parser on the
    // replacement char produced by lossy decoding.
    let result = crate::parse("builtins.toJSON \"_invalid UTF-8: �_\"");
    assert!(
        result.is_ok(),
        "Parser should handle Unicode replacement character"
    );
}

/// `inherit` names written as `${…}` are only valid when the body is a plain
/// string literal. Haskell: `Nixfmt.Parser.interpolationRestricted`.
/// Rejection cases are covered by `rejects_invalid_fixture_corpus`.
#[test]
fn regression_inherit_interpolation_restricted() {
    assert!(crate::parse(r#"{ inherit ${"ok"}; }"#).is_ok());
    assert_parse_rejected(r"{ inherit ${bar}; }");
}