Skip to content

Add env to set environment variables for hooks (#1279)#1285

Merged
j178 merged 3 commits intoj178:masterfrom
danielparks:add-env
Dec 30, 2025
Merged

Add env to set environment variables for hooks (#1279)#1285
j178 merged 3 commits intoj178:masterfrom
danielparks:add-env

Conversation

@danielparks
Copy link
Contributor

@danielparks danielparks commented Dec 28, 2025

This key is not supported by pre-commit.


This requires prek-test-repos/script-hooks to be updated (prek-test-repos/script-hooks#1) before tests will pass. Unfortunately, merging that PR will break tests on this repo until this PR is merged.

I only added a couple of tests here; would you like me to go through and add tests for other languages?

Closes #1279

This key is not supported by `pre-commit`.
@codecov
Copy link

codecov bot commented Dec 28, 2025

Codecov Report

❌ Patch coverage is 96.82540% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.94%. Comparing base (1856285) to head (4e82569).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
crates/prek/src/run.rs 90.47% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1285      +/-   ##
==========================================
- Coverage   90.01%   89.94%   -0.07%     
==========================================
  Files          80       80              
  Lines       15754    15808      +54     
==========================================
+ Hits        14181    14219      +38     
- Misses       1573     1589      +16     

☔ 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.

@github-actions
Copy link

github-actions bot commented Dec 28, 2025

📦 Cargo Bloat Comparison

Binary size change: +0.00% (16.7 MiB → 16.7 MiB)

Expand for cargo-bloat output

Head Branch Results

 File  .text     Size          Crate Name
 0.7%   1.4% 114.7KiB           prek prek::languages::<impl prek::config::Language>::run::{{closure}}::{{closure}}
 0.6%   1.3% 107.3KiB          prek? <prek::cli::Command as clap_builder::derive::Subcommand>::augment_subcommands
 0.3%   0.7%  54.7KiB           prek prek::archive::unpack::{{closure}}
 0.3%   0.7%  52.8KiB             h2 h2::proto::connection::Connection<T,P,B>::poll
 0.2%   0.5%  39.8KiB           prek prek::languages::<impl prek::config::Language>::install::{{closure}}
 0.2%   0.5%  39.2KiB          prek? <prek::cli::RunArgs as clap_builder::derive::Args>::augment_args
 0.2%   0.5%  39.2KiB           prek prek::run::{{closure}}
 0.2%   0.5%  37.3KiB          hyper hyper::proto::h1::dispatch::Dispatcher<D,Bs,I,T>::poll_loop
 0.2%   0.5%  36.9KiB regex_automata regex_automata::meta::strategy::new
 0.2%   0.5%  36.5KiB           prek prek::workspace::Workspace::discover
 0.2%   0.4%  30.8KiB           std? <core::marker::PhantomData<T> as serde_core::de::DeserializeSeed>::deserialize
 0.2%   0.4%  30.7KiB           prek prek::cli::run::run::run::{{closure}}
 0.2%   0.4%  29.8KiB           prek prek::languages::rust::installer::RustInstaller::install::{{closure}}
 0.2%   0.4%  28.4KiB           prek prek::languages::node::installer::NodeInstaller::install::{{closure}}
 0.2%   0.4%  27.9KiB             h2 h2::proto::connection::DynConnection<B>::recv_frame
 0.2%   0.3%  27.6KiB           prek prek::identify::by_extension::{{closure}}
 0.2%   0.3%  27.3KiB           prek prek::cli::run::run::run_hooks::{{closure}}
 0.2%   0.3%  26.2KiB           prek prek::cli::auto_update::update_repo::{{closure}}
 0.1%   0.3%  25.3KiB           prek prek::main
 0.1%   0.3%  24.7KiB           prek prek::hook::HookBuilder::build::{{closure}}
41.2%  88.5%   6.9MiB                And 10638 smaller methods. Use -n N to show more.
46.5% 100.0%   7.8MiB                .text section size, the file size is 16.7MiB

Base Branch Results

 File  .text     Size          Crate Name
 0.6%   1.4% 107.3KiB          prek? <prek::cli::Command as clap_builder::derive::Subcommand>::augment_subcommands
 0.6%   1.4% 107.2KiB           prek prek::languages::<impl prek::config::Language>::run::{{closure}}::{{closure}}
 0.3%   0.7%  54.7KiB           prek prek::archive::unpack::{{closure}}
 0.3%   0.7%  52.8KiB             h2 h2::proto::connection::Connection<T,P,B>::poll
 0.2%   0.5%  39.8KiB           prek prek::languages::<impl prek::config::Language>::install::{{closure}}
 0.2%   0.5%  39.2KiB          prek? <prek::cli::RunArgs as clap_builder::derive::Args>::augment_args
 0.2%   0.5%  39.2KiB           prek prek::run::{{closure}}
 0.2%   0.5%  37.3KiB          hyper hyper::proto::h1::dispatch::Dispatcher<D,Bs,I,T>::poll_loop
 0.2%   0.5%  36.9KiB regex_automata regex_automata::meta::strategy::new
 0.2%   0.5%  36.5KiB           prek prek::workspace::Workspace::discover
 0.2%   0.4%  30.8KiB           std? <core::marker::PhantomData<T> as serde_core::de::DeserializeSeed>::deserialize
 0.2%   0.4%  30.7KiB           prek prek::cli::run::run::run::{{closure}}
 0.2%   0.4%  29.8KiB           prek prek::languages::rust::installer::RustInstaller::install::{{closure}}
 0.2%   0.4%  28.4KiB           prek prek::languages::node::installer::NodeInstaller::install::{{closure}}
 0.2%   0.4%  27.9KiB             h2 h2::proto::connection::DynConnection<B>::recv_frame
 0.2%   0.3%  27.6KiB           prek prek::identify::by_extension::{{closure}}
 0.2%   0.3%  27.3KiB           prek prek::cli::run::run::run_hooks::{{closure}}
 0.2%   0.3%  26.2KiB           prek prek::cli::auto_update::update_repo::{{closure}}
 0.1%   0.3%  25.3KiB           prek prek::main
 0.1%   0.3%  24.4KiB     hyper_util hyper_util::client::legacy::client::Client<C,B>::send_request::{{closure}}
41.2%  88.6%   6.9MiB                And 10633 smaller methods. Use -n N to show more.
46.5% 100.0%   7.8MiB                .text section size, the file size is 16.7MiB

@danielparks
Copy link
Contributor Author

I added a commit to point the script test at my fork of prek-test-repos/script-hooks; it will need to be removed before merging.

@j178 j178 added the enhancement New feature or request label Dec 30, 2025
@j178 j178 marked this pull request as ready for review December 30, 2025 09:01
@j178 j178 requested a review from Copilot December 30, 2025 09:01
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds support for setting environment variables for hooks via a new env configuration option. This feature allows users to pass custom environment variables to hooks, which will override existing environment variables like $PATH.

Key changes:

  • Added env field to hook configuration schema accepting key-value pairs of environment variable names and values
  • Implemented environment variable support across all language implementations (system, script, python, ruby, rust, node, golang, lua, pygrep, docker, docker_image)
  • Updated command-line argument length calculation on Unix to account for environment variable space in ARG_MAX limit
  • Added documentation and tests demonstrating the new env feature

Reviewed changes

Copilot reviewed 20 out of 20 changed files in this pull request and generated no comments.

Show a summary per file
File Description
prek.schema.json Added env field schema definition to both hook and manifest schemas, accepting object or null with string values
docs/configuration.md Added comprehensive documentation for the env hook-level option with usage example showing RUSTDOCFLAGS
crates/prek/src/config.rs Added env: Option<FxHashMap<String, String>> field to HookOptions with custom merge logic that extends rather than replaces
crates/prek/src/hook.rs Added env: FxHashMap<String, String> field to Hook struct and initialized it in the builder
crates/prek/src/run.rs Updated Unix ARG_MAX calculation to account for environment variable sizes, avoiding double-counting overridden variables
crates/prek/src/languages/system.rs Added .envs(&hook.env) call to pass environment variables to system commands
crates/prek/src/languages/script.rs Added .envs(&hook.env) call to pass environment variables to script commands
crates/prek/src/languages/python/python.rs Added .envs(&hook.env) call after setting Python-specific environment variables
crates/prek/src/languages/ruby/ruby.rs Added .envs(&hook.env) call after setting Ruby-specific environment variables
crates/prek/src/languages/rust/rust.rs Added .envs(&hook.env) call after setting Rust-specific environment variables
crates/prek/src/languages/node/node.rs Added .envs(&hook.env) call after setting Node-specific environment variables
crates/prek/src/languages/golang/golang.rs Added .envs(&hook.env) call after setting Go-specific environment variables
crates/prek/src/languages/lua.rs Added .envs(&hook.env) call after setting Lua-specific environment variables
crates/prek/src/languages/pygrep/pygrep.rs Added .envs(&hook.env) call to pass environment variables to pygrep commands
crates/prek/src/languages/docker.rs Implemented environment variable passing via -e flags on docker run command line
crates/prek/src/languages/docker_image.rs Implemented environment variable passing via -e flags on docker run command line
crates/prek/tests/languages/script.rs Updated tests to use versioned repo and added tests demonstrating env variable functionality
crates/prek/tests/languages/docker.rs Updated test to demonstrate env variable functionality with docker hooks
crates/prek/src/snapshots/prek__config__tests__read_manifest.snap Updated snapshot to include env: None in hook definitions
crates/prek/src/snapshots/prek__config__tests__read_config.snap Updated snapshot to include env: None in hook configurations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@j178
Copy link
Owner

j178 commented Dec 30, 2025

Thank you, great PR!

@j178 j178 merged commit 0efd637 into j178:master Dec 30, 2025
28 checks passed
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.

Feature request: Ability to set environment variables in .pre-commit-config.yaml

2 participants