doublify / pre-commit-rust Goto Github PK
View Code? Open in Web Editor NEWRust hooks for pre-commit
Rust hooks for pre-commit
I noticed that the current hooks use language: system
, but it would be nice if the hooks used language: rust
instead (e.g. this is needed for pre-commit.ci)
Hi,
I am trying to use this repo with pre-commit
but when I use it to check my code format, it apply modification and I want to not commit them directly.
My repo with staged changes before format looks like this with git status
:
$ git status
On branch <branch-name>
Your branch is ahead of 'origin/<branch-name>' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: tests/test_commands.rs
and commiting leads to this:
$ git commit -m "test"
Formatting source files
Formatting done, changed files: tests/test_commands.rs
fmt..................................................(no files to check)Skipped
[<branch-name> <commit-hash>] test
And the file is modified by the formatter which is not what I expect.
I would have it to fail to commit and having a status like this:
$ git status
On branch 209-add-cargo-fmt-to-git-hooks-pre-commit
Your branch is ahead of 'origin/<branch-name>' by 1 commit.
(use "git push" to publish your local commits)
Changes to be committed:
(use "git restore --staged <file>..." to unstage)
modified: tests/test_commands.rs
Untracked files:
(use "git add <file>..." to include in what will be committed)
tests/test_commands.rs
My config file looks like this:
repos:
- repo: https://github.com/doublify/pre-commit-rust
rev: v1.0
hooks:
- id: fmt
name: fmt
description: Format files with cargo fmt.
entry: cargo fmt --
language: system
types: [rust]
args: ["--verbose", "--check"]
Using check
in argument prevent the modification of the file which makes it event more frustrating and is not what I want to do.
I want it to make unstaged changes to the file that I can add
and then commit
.
Where does this behavior comes from?
I'm tinkering with a project, right now, which compiles fine with stable rust, but its rustfmt configuration uses a whole bunch of options that are only available with nightly rustfmt. I don't want to switch my default toolchain to nightly just for the sake of this project. It would be nice if it were possible to specify, in .pre-commit-config.yaml
for this project, that the fmt hook should run cargo +nightly fmt
instead of cargo fmt
.
This doesn't appear to be possible with the args array because that's inserted too late on the command line, e.g.
hooks:
- id: fmt
args: ["+nightly"]
gives an error message about there not being any file named +nightly
.
cargo test
hook, that runs test suite of Rust package.A standard practice for my projects is to have tests run for each commit and push. My impression of this repo is the go-to for standard Rust hooks including basic cargo functionality. However, I see cargo test
is not yet implemented -> feature request! ๐
With cargo 1.29.0-nightly (506eea76e 2018-07-17)
I get Unrecognized option: 'write-mode'
when using the hook.
I just discovered cargo spellcheck, which works great for my crate.
Having that in pre-commit would be cool:-)
Hi.
Since this repo has not been updated in a very long time, and since I wanted to add more pre-commit hooks, I decided to fork this repo. The fork is linked here: https://github.com/FeryET/pre-commit-rust
It would be awesome if you could link to my fork as an active continuation of your awesome tool.
Thanks a lot!
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
Following the readme in this repo, I get the following error:
The 'rev' field of repo 'https://github.com/doublify/pre-commit-rust' appears to be a mutable reference (moving tag / branch). Mutable references are never updated after first install and are not supported. See https://pre-commit.com/#using-the-latest-version-for-a-repository for more details. Hint:
pre-commit autoupdate
often fixes this.
Doing the recommended step fixes this:
pre-commit autoupdate
Which sets the hook to rev: v1.0
so I guess the readme needs to be updated to include this?
pre-commit recommends using immutable revisions as the value of the rev
field.
It would be better for users if a release was tagged, so they can pin the hook to a stable version instead of relying on a commit SHA (whose stability isn't guaranteed).
should this hook provide a way to disable the 'deny warnings' flag?
many codebases (including my own) use compiler flags to set the warning level for clippy lints. the 'deny warnings' flag escalates them further, beyond the original intention of the author.
for a concrete example, i tend to use
#![deny(clippy::all)]
#![warn(clippy::pedantic)]
this hook effectively elevates this to
#![deny(clippy::all)]
#![deny(clippy::pedantic)]
this is a nightmare for reproducible builds, for example.
cargo fmt
would use cargo.toml for the info, rustfmt has also the option --edition
but
- repo: https://github.com/doublify/pre-commit-rust
rev: master
hooks:
- id: fmt
args:
- --edition=2018
Says: "Error: file --edition=2018
does not exist"
Workaround: create .rustfmt.toml
and put edition="2018"
there.
This is a new one to me, but seems it'd be a very useful pre-commit hook, i.e. 'you just removed the last use of this dependency':
https://github.com/est31/cargo-udeps
I have repository that has both Python and Rust code inside.
In the root of the repository I have .pre-commit-config.yaml
with:
- repo: https://github.com/doublify/pre-commit-rust
rev: master
hooks:
- id: fmt
args: ['--manifest-path', 'rust_code/Cargo.toml']
In the rust_code/
folder I have rust code together with Cargo.toml
.
However, it looks like the hook expects Cargo.toml
to be in the root of the repository and is ignoring arguments.
Error during execution of `cargo metadata`: error: could not find `Cargo.toml` in `D:\projects\project-repo` or any parent directory
What options do I have?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.