Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

rustfmt and clippy fail with "No such file or directory" #126

Open
ambroisie opened this issue Nov 8, 2021 · 9 comments · Fixed by #156 · May be fixed by #396
Open

rustfmt and clippy fail with "No such file or directory" #126

ambroisie opened this issue Nov 8, 2021 · 9 comments · Fixed by #156 · May be fixed by #396

Comments

@ambroisie
Copy link
Contributor

Steps to reproduce:

With the following flake.nix:

{
  description = "Testing pre-commits";

  inputs = {
    flake-utils = {
      type = "github";
      owner = "numtide";
      repo = "flake-utils";
      ref = "master";
    };

    naersk = {
      type = "github";
      owner = "nix-community";
      repo = "naersk";
      ref = "master";
      inputs = {
        nixpkgs.follows = "nixpkgs";
      };
    };

    nixpkgs = {
      type = "github";
      owner = "NixOS";
      repo = "nixpkgs";
      ref = "nixpkgs-unstable";
    };

    pre-commit-hooks = {
      type = "github";
      owner = "cachix";
      repo = "pre-commit-hooks.nix";
      ref = "master";
      inputs = {
        flake-utils.follows = "flake-utils";
        nixpkgs.follows = "nixpkgs";
      };
    };

    rust-overlay = {
      type = "github";
      owner = "oxalica";
      repo = "rust-overlay";
      ref = "master";
      inputs = {
        flake-utils.follows = "flake-utils";
        nixpkgs.follows = "nixpkgs";
      };
    };
  };

  outputs =
    { self
    , flake-utils
    , naersk
    , nixpkgs
    , pre-commit-hooks
    , rust-overlay
    }:
    {
      overlay = final: prev: {
        foo =
          let
            inherit (final) system;
            overlays = [
              (import rust-overlay)
            ];
            pkgs = import nixpkgs { inherit overlays system; };
            my-rust = pkgs.rust-bin.stable.latest;
            naersk-lib = naersk.lib."${system}".override {
              cargo = my-rust.default;
              rustc = my-rust.default;
            };
          in
          naersk-lib.buildPackage {
            pname = "foo";
            version = "0.0.0";

            src = ./.;

            passthru = {
              inherit my-rust;
            };
          };
      };
    } // (flake-utils.lib.eachDefaultSystem (system:
    let
      overlays = [ self.overlay ];
      pkgs = import nixpkgs { inherit overlays system; };
      inherit (pkgs) lib;
      inherit (pkgs.foo.passthru) my-rust;
    in
    rec {
      checks = {
        pre-commit = pre-commit-hooks.lib.${system}.run {
          src = ./.;

          # FIXME: clippy and rustfmt fail when inside `nix flake check`
          # but `pre-commit run --all` works properly...
          # Also a problem when not overriding the `entry`
          hooks = {
            clippy = {
              enable = true;
              entry = lib.mkForce "${my-rust.clippy}/bin/cargo-clippy clippy";
            };

            nixpkgs-fmt = {
              enable = true;
            };

            rustfmt = {
              enable = true;
              entry = lib.mkForce "${my-rust.rustfmt}/bin/cargo-fmt fmt -- --check --color always";
            };
          };
        };
      };

      defaultPackage = packages.foo;

      devShell = pkgs.mkShell {
        inputsFrom = [
          defaultPackage
        ];

        nativeBuildInputs = with pkgs; [
          rust-analyzer
        ];

        inherit (checks.pre-commit) shellHook;

        RUST_SRC_PATH = "${my-rust.rust-src}/lib/rustlib/src/rust/library";
      };

      packages = {
        inherit (pkgs) foo;
      };
    }));
}

And executing the following commands to create a simple rust project:

nix shell nixpkgs#cargo -c cargo init .
# Create a Cargo.lock for naersk
nix shell nixpkgs#cargo -c cargo update
nix flake check

You get the following errors in the log:

clippy...................................................................Failed
- hook id: clippy
- exit code: 101

thread 'main' panicked at 'could not run cargo: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/tools/clippy/src/main.rs:163:10
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

nixpkgs-fmt..............................................................Passed
rustfmt..................................................................Failed
- hook id: rustfmt
- exit code: 1

No such file or directory (os error 2)

Even without overriding the entry attribute of clippy and rustfmt, I still get the same error.

@garbas
Copy link

garbas commented Jan 18, 2022

This solved it for me. Clearly is a hack but I haven't yet look how we can fix this in pre-commit-hooks.nix.

           pkgs.buildEnv {
              name = my-rust.name;
              inherit (my-rust) meta;
              buildInputs = [ pkgs.makeWrapper ];
              paths = [ my-rust ];
              pathsToLink = [ "/" "/bin" ];
              postBuild = ''
                for i in $out/bin/*; do
                  wrapProgram "$i" --prefix PATH : "$out/bin"
                done

              '';
            };

The problem was that cargo-fmt needs to see rustfmt binary.
This was the hint that help me -> rust-lang/rustfmt#1071 (comment)

@domenkozar
Copy link
Member

Could someone open a PR?

@K900
Copy link
Contributor

K900 commented Mar 16, 2022

I think what needs to happen here is you need to actually run in an environment that has rustc, cargo, clippy and all the other stuff in the same environment, so it gets kinda tricky.

@winterqt
Copy link
Contributor

Hey folks, I tried to fix this with #156. I've lightly tested it and it does seem to fix the issues described here. Let me know if there's anything I missed. Thanks!

@m1-s
Copy link
Contributor

m1-s commented Sep 8, 2022

@winterqt This still is b0rken for me. Requesting to re-open this ticket.

I have created a minimal reproducible example at github.com/m1-s/pre-commit-hooks-nix-clippy.

When executing nix run "git+ssh://git@github.com/m1-s/pre-commit-hooks-nix-clippy?ref=main" -L both clippy and rustfmt fail for me.

@winterqt
Copy link
Contributor

winterqt commented Sep 8, 2022

Ah, I see. Wonder why it's behaving differently in that context -- it's in PATH either way, so that's... weird.

I'll take a look at this soon, in the meantime I do agree that this ticket should be reopened.

@domenkozar domenkozar reopened this Sep 8, 2022
@winterqt
Copy link
Contributor

@m1-s Your clippy issue was caused by the fact that you have a dependency (and no Cargo.lock for a binary, but still the same issue), and the rustfmt hook is fixed with this patch:

diff --git a/modules/hooks.nix b/modules/hooks.nix
index b97badb..d55ceb5 100644
--- a/modules/hooks.nix
+++ b/modules/hooks.nix
@@ -313,7 +313,7 @@ in
             nativeBuildInputs = [ pkgs.makeWrapper ];
             postBuild = ''
               wrapProgram $out/bin/cargo-fmt \
-                --prefix PATH : ${lib.makeBinPath [ tools.cargo ]}
+                --prefix PATH : ${lib.makeBinPath [ tools.cargo tools.rustfmt ]}
             '';
           };
         in

Going back to the first issue, it seems like... well, nobody, has tested the check aspects of this with a project that has dependencies. We obviously need to have a way to download these dependencies into the build directory to be able to check them, which these hooks currently don't provide.

I have an idea that involves using importCargoLock to automatically do this for the cargo-check and clippy hooks, if @domenkozar is okay with the idea I can (try to) write + PR an implementation of it.

@lafrenierejm
Copy link
Contributor

lafrenierejm commented Sep 12, 2022

the rustfmt hook is fixed with [adding tools.rustfmt to makeBinPath's argument]

Thanks for the patch, @winterqt! I can confirm that it fixed the rustfmt hook for me.

@m1-s m1-s mentioned this issue Sep 13, 2022
@m1-s
Copy link
Contributor

m1-s commented Sep 14, 2022

@winterqt I cant speak for @domenkozar but I would love to see a PR that fixes clippy.

lafrenierejm added a commit to lafrenierejm/ripsecrets that referenced this issue Dec 19, 2022
cachix/git-hooks.nix#126 reduces the usefulness
of the hook.  I would like to reintroduce this check once that upstream issue
has been resolved.
sirwart pushed a commit to sirwart/ripsecrets that referenced this issue Dec 23, 2022
* Replace `naersk` with `crane` to cache Nix-built dependencies

* Remove `cargo check` pre-commit hook

cachix/git-hooks.nix#126 reduces the usefulness
of the hook.  I would like to reintroduce this check once that upstream issue
has been resolved.

* Update Nix flake sources

* Update Cargo dependencies

```shell
cargo update
```

* Replacing single-character string with char

* Add `packages.ripsecrets`

* Perform Nix flake build and check in same step
DrSensor added a commit to DrSensor/nusa that referenced this issue Oct 10, 2023
Somehow it emit this error
```
`cargo metadata` exited with an error: error: rustup could not choose a version of cargo to run, because one wasn't specified explicitly, and no default is configured.
help: run 'rustup default stable' to download the latest stable release of Rust and set it as your default toolchain.
```

cachix/git-hooks.nix#126
@mrcjkb mrcjkb linked a pull request Feb 10, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
7 participants