Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Absolute path in projectRootFile breaks formatting #209

Open
tsandrini opened this issue Aug 5, 2024 · 0 comments
Open

Absolute path in projectRootFile breaks formatting #209

tsandrini opened this issue Aug 5, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@tsandrini
Copy link

Describe the bug

Disclaimer: I'd like to report something that's probably not an important bug and an edge case for most projects, however, this worked pre < v2.0.0 and doesn't anymore hence it might be a good idea to put some note in the docs or a warning directly in the code.


Bug: Using absolute paths in projectRootFile seems to break passing arguments to some formatters, for example nixfmt, nixfmt-rfc-style

To Reproduce

Steps to reproduce the behavior:

  1. Simple flakeModule in the following format
    { config, inputs, ... }:
    {
      imports = with inputs; [ treefmt-nix.flakeModule ];
    
      perSystem =
        { pkgs, ... }:
        {
          treefmt = {
            package = pkgs.treefmt;
            flakeCheck = true;
            flakeFormatter = true;
            projectRootFile = config.flake-root + "/flake.nix";
    
            programs = {
              deadnix.enable = true; # Find and remove unused code in .nix source files
              statix.enable = true; # Lints and suggestions for the nix programming language
              nixfmt.enable = true; # An opinionated formatter for Nix
            };
          };
        };
    }
    where
      options.flake-root = lib.mkOption {
        type = lib.types.path;
        description = ''
          Provides `config.flake-root` with the path to the flake root.
        '';
        default = ../.;
      };
    NOTE: The same thing happens even with directly specifying projectRootFile = ../. + "/flake.nix";
  2. And running nix fmt produces errors like these
    ❯ nix fmt
    ...
    nixfmt error:
    nixfmt: ome/tsandrini/ProjectBundle/tsandrini/tensorfiles/age/secrets/secrets.nix: openFile: does not exist (No such file or directory)
    ....
    and also tries to format stuff that should be ignored, such as
    ❯ nix fmt
    ....
    WARN format: no formatter for path: ome/tsandrini/ProjectBundle/tsandrini/tensorfiles/.direnv/bin/nix-direnv-reload
    WARN format: no formatter for path: ome/tsandrini/ProjectBundle/tsandrini/tensorfiles/.direnv/flake-profile-9f4ae1dfa18f64f9620b50ba3609c21d070f3a5f.rc
    WARN format: no formatter for path: ome/tsandrini/ProjectBundle/tsandrini/tensorfiles/.git/config
    WARN format: no formatter for path: ome/tsandrini/ProjectBundle/tsandrini/tensorfiles/.git/hooks/commit-msg
    WARN format: no formatter for path: ome/tsandrini/ProjectBundle/tsandrini/tensorfiles/.git/hooks/pre-commit
    WARN format: no formatter for path: ome/tsandrini/ProjectBundle/tsandrini/tensorfiles/.git/index
    WARN format: no formatter for path: ome/tsandrini/ProjectBundle/tsandrini/tensorfiles/.git/objects/03/466bd59a39888b932526fb36ae2e42dfed6b2c
    WARN format: no formatter for path: ome/tsandrini/ProjectBundle/tsandrini/tensorfiles/.git/objects/0c/fe02053860d76afe18757e9d0804a340d3e4b6
    WARN format: no formatter for path: ome/tsandrini/ProjectBundle/tsandrini/tensorfiles/.git/objects/0f/866697204f521a6192754f6a5f11aa80f4508c
    ...
    

Expected behavior

Setting projectRootFile = "flake.nix"; fixes this and produces expected behaviour, however, since projectRootFile = config.flake-root + "/flake.nix"; worked in the previous versions, this can be confusing to some folk, especially when the error is part of pre-commit hooks or some CI workflow I initially assumed this was due to the nixfmt-rfc-style -> nixfmt renaming in nixpkgs and didn't suspect treefmt at first.

This is why adding a warning or a note in the documentation might be a good idea.

System information

❯ nix-shell -p nix-info --run "nix-info -m"
 - system: `"x86_64-linux"`
 - host os: `Linux 6.10.2, NixOS, 24.11 (Vicuna), 24.11.20240801.dirty`
 - multi-user?: `yes`
 - sandbox: `yes`
 - version: `nix-env (Nix) 2.23.3`
 - nixpkgs: `/nix/store/920a6ivyd50598z8djw9x3mr33gys0j5-source`
cd $FLAKE && nix flake metadata --json | jq '.locks.nodes."treefmt-nix"'
{
  "inputs": {
    "nixpkgs": [
      "nixpkgs-wayland",
      "nix-eval-jobs",
      "nixpkgs"
    ]
  },
  "locked": {
    "lastModified": 1720930114,
    "narHash": "sha256-VZK73b5hG5bSeAn97TTcnPjXUXtV7j/AtS4KN8ggCS0=",
    "owner": "numtide",
    "repo": "treefmt-nix",
    "rev": "b92afa1501ac73f1d745526adc4f89b527595f14",
    "type": "github"
  },
  "original": {
    "owner": "numtide",
    "repo": "treefmt-nix",
    "type": "github"
  }
}

where $FLAKE points to my personal configuration in which I discovered this after running an update.

Additional context

@tsandrini tsandrini added the bug Something isn't working label Aug 5, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant