numtide / treefmt-nix

treefmt nix configuration
https://numtide.github.io/treefmt/
MIT License
247 stars 75 forks source link

nickel: fix formatting subcommand syntax #133

Closed VTimofeenko closed 10 months ago

VTimofeenko commented 11 months ago

Current nickel version automatically formats the file in place and produces an error if the flags are provided

VTimofeenko commented 11 months ago

Sample code:

# flake.nix
{
  inputs.treefmt-nix.url = "github:numtide/treefmt-nix";

  outputs = { self, nixpkgs, systems, treefmt-nix }:
    let
      eachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f nixpkgs.legacyPackages.${system});
      treefmtEval = eachSystem (pkgs: treefmt-nix.lib.evalModule pkgs (_: {
        projectRootFile = "flake.nix";
        programs.nickel.enable = true;
      }));
    in
    {
      formatter = eachSystem (pkgs: treefmtEval.${pkgs.system}.config.build.wrapper);
      checks = eachSystem (pkgs: {
        formatting = treefmtEval.${pkgs.system}.config.build.check self;
      });
    };
}

test.ncl with multiple spaces. Should be reformatted to {}:

{   }

This produces:

❯ nix fmt
[WARN ] cache: failed to load the manifest due to: No such file or directory (os error 2)
[WARN ] Error using formatter #nickel:
    • [STDOUT]:

    • [STDERR]:
    error: unexpected argument '--in-place' found

      tip: to pass '--in-place' as a value, use '-- --in-place'

    Usage: nickel format [OPTIONS] [FILES]...

    For more information, try '--help'.

[ERROR] #nickel's formatter failed: exit status 2
VTimofeenko commented 11 months ago

Testing with this fix:

{
  inputs.treefmt-nix.url = "github:VTimofeenko/treefmt-nix?ref=nickel-syntax-fix";

  outputs = { self, nixpkgs, systems, treefmt-nix }:
    let
      eachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f nixpkgs.legacyPackages.${system});
      treefmtEval = eachSystem (pkgs: treefmt-nix.lib.evalModule pkgs (_: {
        projectRootFile = "flake.nix";
        programs.nickel.enable = true;
      }));
    in
    {
      formatter = eachSystem (pkgs: treefmtEval.${pkgs.system}.config.build.wrapper);
      checks = eachSystem (pkgs: {
        formatting = treefmtEval.${pkgs.system}.config.build.check self;
      });
    };
}
[INFO ] #nickel: 1 files processed in 6.80ms
1 files changed in 8ms (found 4, matched 1, cache misses 1)
zimbatm commented 10 months ago

Looking for feedback from @gkze who added this formatter

gkze commented 10 months ago

Sorry for the delay. I'm able to reproduce this, so I think I was pulling in an outdated nickel version. I'm comfortable to merge, and can follow up with fixes if needed

zimbatm commented 10 months ago
$ nix run .#nickel -- format --help
Format Nickel files

Usage: nickel format [OPTIONS] [FILES]...

Arguments:
  [FILES]...  Input files, omit to read from stdin

Options:
      --color <COLOR>  Configure when to output messages in color [default: auto] [possible
                       values: auto, always, never]
  -h, --help           Print help

It seems that the wrapper can even be dropped.

zimbatm commented 10 months ago

fixed in d06b70e5163a903f19009c3f97770014787a080f