Skip to content

Also run hooks when only Cargo.toml and/or Cargo.lock has changed #33

@tyilo

Description

@tyilo

Both the cargo-check and clippy hooks should also be run if only Cargo.toml and/or Cargo.lock has been changed.

The following shows the problem:

$ cd /tmp
$ cargo new test-pre-commit
$ cd test-pre-commit
$ cat > .pre-commit-config.yaml
repos:
  - repo: https://github.com/doublify/pre-commit-rust
    rev: v1.0
    hooks:
    -   id: fmt
    -   id: cargo-check
^D
$ pre-commit install
$ git add .
$ git commit -m 'init'
fmt......................................................................Passed
cargo check..............................................................Passed
...
$ cargo add libc
$ cat > src/main.rs
fn main() {
    let r = unsafe { libc::rand() };
    dbg!(r);
}
^D
$ git add .
$ git commit -m '2nd'
fmt......................................................................Passed
cargo check..............................................................Passed
...
$ cargo rm libc
$ git add .
$ git commit -m '3rd'
fmt..................................................(no files to check)Skipped
cargo check..........................................(no files to check)Skipped

In the 3rd commit we have removed libc so that the code no longer compiles. However the cargo-check step is skipped, so this is not caught :(

Running pre-commit run --all-files manually results in:

fmt......................................................................Passed
cargo check..............................................................Failed
- hook id: cargo-check
- exit code: 101

    Checking test-pre-commit v0.1.0 (/tmp/test-pre-commit)
error[E0433]: failed to resolve: use of undeclared crate or module `libc`
 --> src/main.rs:2:22
  |
2 |     let r = unsafe { libc::rand() };
  |                      ^^^^ use of undeclared crate or module `libc`

For more information about this error, try `rustc --explain E0433`.
error: could not compile `test-pre-commit` (bin "test-pre-commit") due to 1 previous error

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions