Skip to content

bundle/cargo: respect CARGO_HOME and friends#21795

Open
branchv wants to merge 1 commit intomainfrom
cargo-env
Open

bundle/cargo: respect CARGO_HOME and friends#21795
branchv wants to merge 1 commit intomainfrom
cargo-env

Conversation

@branchv
Copy link
Member

@branchv branchv commented Mar 21, 2026


  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same change?
  • Have you added an explanation of what your changes do and why you'd like us to include them?
  • Have you written new tests (excluding integration tests) for your changes? Here's an example.
  • Have you successfully run brew lgtm (style, typechecking and tests) with your changes locally?

  • AI was used to generate or assist with generating this PR. Please specify below how you used AI to help you, and what steps you have taken to manually verify the changes.

Similar to #20866 for go, this passes through cargo env variables that affect cargo install --list and cargo install

  • CARGO_HOME and CARGO_INSTALL_ROOT tell cargo where to put binaries
  • RUSTUP_HOME tells a rustup-provided cargo where to find its default toolchain (which will otherwise fail with rustup could not choose a version of cargo to run)

Copilot AI review requested due to automatic review settings March 21, 2026 20:07
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 updates Homebrew Bundle’s Cargo integration to preserve user Cargo/Rustup environment configuration across Homebrew’s environment filtering, matching the existing approach used for other toolchains (e.g., Go).

Changes:

  • Pass through CARGO_HOME, CARGO_INSTALL_ROOT, and RUSTUP_HOME by exporting them as HOMEBREW_* variables in bin/brew.
  • Apply those variables when running cargo install --list and cargo install in the Bundle Cargo extension via a shared cargo_env helper.

Reviewed changes

Copilot reviewed 1 out of 2 changed files in this pull request and generated 1 comment.

File Description
bin/brew Allows Cargo/Rustup-related env vars to survive env -i filtering by copying them to HOMEBREW_*.
Library/Homebrew/bundle/extensions/cargo.rb Wraps Cargo invocations with with_env(cargo_env(...)) so Cargo sees the intended install/home/toolchain dirs.

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

Comment on lines +83 to +91
sig { params(cargo: Pathname).returns(T::Hash[String, T.nilable(String)]) }
def cargo_env(cargo)
{
"CARGO_HOME" => ENV.fetch("HOMEBREW_CARGO_HOME", nil),
"CARGO_INSTALL_ROOT" => ENV.fetch("HOMEBREW_CARGO_INSTALL_ROOT", nil),
"PATH" => "#{cargo.dirname}:#{ENV.fetch("PATH")}",
"RUSTUP_HOME" => ENV.fetch("HOMEBREW_RUSTUP_HOME", nil),
}
end
Copy link

Copilot AI Mar 21, 2026

Choose a reason for hiding this comment

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

The new env pass-through logic (CARGO_HOME/CARGO_INSTALL_ROOT/RUSTUP_HOME via cargo_env and with_env) isn’t covered by tests. Since this file already has specs, please add assertions that these variables are set/passed through when corresponding HOMEBREW_* vars are present (and that they don’t leak outside the block).

Copilot uses AI. Check for mistakes.
Comment on lines +83 to +90
sig { params(cargo: Pathname).returns(T::Hash[String, T.nilable(String)]) }
def cargo_env(cargo)
{
"CARGO_HOME" => ENV.fetch("HOMEBREW_CARGO_HOME", nil),
"CARGO_INSTALL_ROOT" => ENV.fetch("HOMEBREW_CARGO_INSTALL_ROOT", nil),
"PATH" => "#{cargo.dirname}:#{ENV.fetch("PATH")}",
"RUSTUP_HOME" => ENV.fetch("HOMEBREW_RUSTUP_HOME", nil),
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
sig { params(cargo: Pathname).returns(T::Hash[String, T.nilable(String)]) }
def cargo_env(cargo)
{
"CARGO_HOME" => ENV.fetch("HOMEBREW_CARGO_HOME", nil),
"CARGO_INSTALL_ROOT" => ENV.fetch("HOMEBREW_CARGO_INSTALL_ROOT", nil),
"PATH" => "#{cargo.dirname}:#{ENV.fetch("PATH")}",
"RUSTUP_HOME" => ENV.fetch("HOMEBREW_RUSTUP_HOME", nil),
}
sig { params(cargo: Pathname).returns(T::Hash[String, String]) }
def cargo_env(cargo)
{
"CARGO_HOME" => ENV.fetch("HOMEBREW_CARGO_HOME", nil),
"CARGO_INSTALL_ROOT" => ENV.fetch("HOMEBREW_CARGO_INSTALL_ROOT", nil),
"PATH" => "#{cargo.dirname}:#{ENV.fetch("PATH")}",
"RUSTUP_HOME" => ENV.fetch("HOMEBREW_RUSTUP_HOME", nil),
}.compact

Copy link
Member

@MikeMcQuaid MikeMcQuaid left a comment

Choose a reason for hiding this comment

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

Agreed with comments, otherwise looks good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants