Skip to content

Implement check-executables-have-shebangs as builtin-hook#924

Merged
j178 merged 11 commits intoj178:masterfrom
feliblo:feature/check-executables-have-shebangs
Oct 30, 2025
Merged

Implement check-executables-have-shebangs as builtin-hook#924
j178 merged 11 commits intoj178:masterfrom
feliblo:feature/check-executables-have-shebangs

Conversation

@feliblo
Copy link
Contributor

@feliblo feliblo commented Oct 18, 2025

Description

Following the information provided in 880. This is an implementation of the check-executables-have-shebangs hook.

The check-executables-have-shebangs has 1k grep.app hits. It's the final unimplemented hook used by airflow as mentioned in this comment.


Notes

  • Windows; I'm having trouble testing this on windows, since .sh files aren't seen as executable and setting them as executable using git add --chmod=+x {file_name}. See [bug] Executables not correctly identified on windows #928
  • Windows32
    • Currently we do not check windows32. In this PR and the original pre-commit implementation, it takes a completely different route than UNIX based on the return value of git config core.fileMode (returns false only on win32`
    • 2 options:
      1. Add win32 to the ci/cd pipeline
      2. We need to be able to overwrite the output of this command in tests (like one would in python)
# git config core.fileMode is used to determine UNIX vs win32
let status = Command::new("git")
      .arg("config")
      .arg("core.fileMode")
      .current_dir(repo_path)
      .status()?;
  • Bugfix
    • Fixed a bug in identify.rs. Where the first element of a shebangs was being accessed. However the parse_shebang function can still return an Ok with an empty vector, making the access fail.
    • This caused prek to panic when running against the ardupilot repository.
    • I added the check in identify.rs but the parse_shebang function still contains a Todo.
    • Also described in: [bug] parse_shebang can return an empty list #925

Memory changes

cargo clean
cargo build --release 
  • Master branch:  8.49 MB
  • Feature branch: 8.58MB
    +89,800 bytes (+1.06%) increase from master to feature branch

Performance

Ran these commands on the ardupilot repository:
simply because it was the top-hit on grap.app

Linux

# pre-commit
pre-commit run check-executables-have-shebangs --all-files --verbose
check that executables have shebangs.....................................Passed
- hook id: check-executables-have-shebangs
- duration: 0.09s

# Old prek implementation (python)
prek run check-executables-have-shebangs --all-files --verbose
thread '<unnamed>' panicked at src/identify.rs:767:55:
index out of bounds: the len is 0 but the index is 0
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

# Old prek implementation bugfixed (python)
prek run check-executables-have-shebangs --all-files --verbose
check that executables have shebangs.....................................Passed
- hook id: check-executables-have-shebangs
- duration: 0.07s

# New implementation (rust)
rek run check-executables-have-shebangs --all-files --verbose
check that executables have shebangs.....................................Passed
- hook id: check-executables-have-shebangs
- duration: 0.01s

Linux output:
image

@codecov
Copy link

codecov bot commented Oct 18, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.07%. Comparing base (8470e04) to head (4d658d8).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #924      +/-   ##
==========================================
+ Coverage   89.93%   90.07%   +0.13%     
==========================================
  Files          66       67       +1     
  Lines       12223    12391     +168     
==========================================
+ Hits        10993    11161     +168     
  Misses       1230     1230              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@feliblo feliblo force-pushed the feature/check-executables-have-shebangs branch 14 times, most recently from 24297d2 to d1f5a01 Compare October 18, 2025 16:45
@feliblo
Copy link
Contributor Author

feliblo commented Oct 18, 2025

I'm having some trouble setting .sh files as executable on windows, even when using git chmod. I will boot up my windows pc later and take a look.

It's almost certainly due to line 750 in indentify.rs:

    #[cfg(not(unix))]
    {
        executable = {
            let ext = path.extension().and_then(|ext| ext.to_str());
            ext.is_some_and(|ext| ext == "exe" || ext == "bat" || ext == "cmd")
        };
    }

edit: created this issue: #928


With the other bug mentioned above it could be too much for just 1 PR though.

@feliblo feliblo force-pushed the feature/check-executables-have-shebangs branch from d1f5a01 to a232e1f Compare October 18, 2025 16:59
@j178
Copy link
Owner

j178 commented Oct 19, 2025

Thank you! I'll take a look soon.

@j178 j178 mentioned this pull request Oct 20, 2025
34 tasks
@j178 j178 changed the title feat (check-executables-have-shebangs): implement builtin-hook Implement check-executables-have-shebangs as builtin-hook Oct 20, 2025
@j178 j178 added the enhancement New feature or request label Oct 20, 2025
@j178 j178 force-pushed the feature/check-executables-have-shebangs branch 2 times, most recently from e22d418 to 214955d Compare October 20, 2025 05:57
@j178
Copy link
Owner

j178 commented Oct 24, 2025

I'll try to resolve #928, then we can get this merge.

@feliblo
Copy link
Contributor Author

feliblo commented Oct 24, 2025

I'll try to resolve #928, then we can get this merge.

I mentioned it in the other PR. But the way pre-commit resolves this in windows is, send all files to the hook and resolve for those marked as executable in the hook itself. Hence it always returning passed in the output above.

@github-actions
Copy link

github-actions bot commented Oct 29, 2025

📦 Cargo Bloat Comparison

Binary size change: +0.62% (16.1 MiB → 16.2 MiB)

Expand for cargo-bloat output

Head Branch Results

 File  .text     Size          Crate Name
 0.6%   1.3% 101.1KiB          prek? <prek::cli::Command as clap_builder::derive::Subcommand>::augment_subcommands
 0.6%   1.3%  98.1KiB           prek prek::builtin::pre_commit_hooks::Implemented::run::{{closure}}
 0.5%   1.2%  88.0KiB           prek prek::languages::<impl prek::config::Language>::run::{{closure}}
 0.4%   0.8%  63.8KiB           prek prek::archive::unpack::{{closure}}
 0.3%   0.7%  49.2KiB           prek prek::run::{{closure}}
 0.3%   0.6%  42.0KiB           prek prek::languages::<impl prek::config::Language>::install::{{closure}}
 0.2%   0.5%  40.9KiB regex_automata regex_automata::meta::strategy::new
 0.2%   0.5%  40.1KiB          prek? <prek::cli::RunArgs as clap_builder::derive::Args>::augment_args
 0.2%   0.4%  32.9KiB           prek prek::workspace::Workspace::discover
 0.2%   0.4%  32.8KiB           prek prek::cli::run::run::run::{{closure}}
 0.2%   0.4%  32.3KiB           prek prek::languages::node::installer::NodeInstaller::install::{{closure}}
 0.2%   0.4%  31.8KiB             h2 h2::proto::connection::DynConnection<B>::recv_frame
 0.2%   0.4%  31.7KiB             h2 h2::proto::connection::Connection<T,P,B>::poll
 0.2%   0.4%  27.6KiB           prek prek::identify::by_extension::{{closure}}
 0.2%   0.3%  26.2KiB     hyper_util hyper_util::client::legacy::client::Client<C,B>::send_request::{{closure}}
 0.2%   0.3%  25.8KiB     hyper_util hyper_util::client::legacy::client::Client<C,B>::connect_to::{{closure}}::{{closure}}::{{closure}}
 0.2%   0.3%  25.0KiB        globset globset::GlobSetBuilder::build
 0.2%   0.3%  25.0KiB           prek prek::main
 0.1%   0.3%  24.2KiB           prek prek::languages::golang::installer::GoInstaller::install::{{closure}}
 0.1%   0.3%  23.7KiB          hyper hyper::proto::h1::dispatch::Dispatcher<D,Bs,I,T>::poll_loop
39.8%  87.5%   6.4MiB                And 10221 smaller methods. Use -n N to show more.
45.4% 100.0%   7.3MiB                .text section size, the file size is 16.2MiB

Base Branch Results

 File  .text     Size          Crate Name
 0.6%   1.3% 101.1KiB          prek? <prek::cli::Command as clap_builder::derive::Subcommand>::augment_subcommands
 0.5%   1.2%  88.0KiB           prek prek::languages::<impl prek::config::Language>::run::{{closure}}
 0.5%   1.1%  85.8KiB           prek prek::builtin::pre_commit_hooks::Implemented::run::{{closure}}
 0.4%   0.9%  63.8KiB           prek prek::archive::unpack::{{closure}}
 0.3%   0.7%  49.2KiB           prek prek::run::{{closure}}
 0.3%   0.6%  42.0KiB           prek prek::languages::<impl prek::config::Language>::install::{{closure}}
 0.2%   0.5%  40.9KiB regex_automata regex_automata::meta::strategy::new
 0.2%   0.5%  40.1KiB          prek? <prek::cli::RunArgs as clap_builder::derive::Args>::augment_args
 0.2%   0.4%  32.9KiB           prek prek::workspace::Workspace::discover
 0.2%   0.4%  32.8KiB           prek prek::cli::run::run::run::{{closure}}
 0.2%   0.4%  32.3KiB           prek prek::languages::node::installer::NodeInstaller::install::{{closure}}
 0.2%   0.4%  31.8KiB             h2 h2::proto::connection::DynConnection<B>::recv_frame
 0.2%   0.4%  31.7KiB             h2 h2::proto::connection::Connection<T,P,B>::poll
 0.2%   0.4%  27.6KiB           prek prek::identify::by_extension::{{closure}}
 0.2%   0.3%  26.2KiB     hyper_util hyper_util::client::legacy::client::Client<C,B>::send_request::{{closure}}
 0.2%   0.3%  25.8KiB     hyper_util hyper_util::client::legacy::client::Client<C,B>::connect_to::{{closure}}::{{closure}}::{{closure}}
 0.2%   0.3%  25.0KiB        globset globset::GlobSetBuilder::build
 0.2%   0.3%  25.0KiB           prek prek::main
 0.1%   0.3%  24.2KiB           prek prek::languages::golang::installer::GoInstaller::install::{{closure}}
 0.1%   0.3%  23.7KiB          hyper hyper::proto::h1::dispatch::Dispatcher<D,Bs,I,T>::poll_loop
39.8%  87.7%   6.4MiB                And 10186 smaller methods. Use -n N to show more.
45.4% 100.0%   7.3MiB                .text section size, the file size is 16.1MiB

@feliblo feliblo force-pushed the feature/check-executables-have-shebangs branch 2 times, most recently from e6454ad to e7c2810 Compare October 29, 2025 18:16
@feliblo feliblo force-pushed the feature/check-executables-have-shebangs branch from e7c2810 to 404e96f Compare October 29, 2025 18:28
@feliblo feliblo force-pushed the feature/check-executables-have-shebangs branch from 404e96f to b92d7b2 Compare October 29, 2025 18:32
@j178 j178 force-pushed the feature/check-executables-have-shebangs branch from 9eb62c1 to 3614c05 Compare October 30, 2025 08:12
@j178 j178 merged commit aa6cd18 into j178:master Oct 30, 2025
21 checks passed
@j178
Copy link
Owner

j178 commented Oct 30, 2025

Thank you!

@feliblo feliblo deleted the feature/check-executables-have-shebangs branch November 4, 2025 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants