Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to specify CARGO_HOME via file #6452

Open
tyranron opened this issue Dec 17, 2018 · 5 comments
Open

Ability to specify CARGO_HOME via file #6452

tyranron opened this issue Dec 17, 2018 · 5 comments
Labels
A-configuration Area: cargo config files and env vars C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage.

Comments

@tyranron
Copy link

tyranron commented Dec 17, 2018

Describe the problem you are trying to solve

Currently, the $CARGO_HOME env var may be set to specify the location of cargo "caches" (fetched crates, binaries installed, etc). When we do not want to use the global one, we can override this for local project by providing directly to cargo commands:

CARGO_HOME=.cache/cargo cargo fetch

# or
export CARGO_HOME=.cache/cargo
cargo build

While this works OK, it's still not ergonomic enough for certain situations. For example:
I'm developing HTTP daemon and using Docker. I want $CARGO_HOME being reused both for normal builds in terminal or IDE (via cargo build) and for those ones inside Dockerfile. This makes me:

  • either specify CARGO_HOME=.cache/cargo for every command (not very handy);
  • or do not forget to pre-setup export CARGO_HOME=.cache/cargo each time I'm opening terminal for this project (as I cannot set it globally);
  • or leave everything "as is" and download all the crates on each docker build.

The problem with docker build that I cannot pass arbitrary directory to the build context, only those dirs which are inside cwd. So, I should persist $CARGO_HOME inside my project root and do it only when using this project specifically. I want no bothering and just run cargo build or docker build without any crates refetches.

Describe the solution you'd like

Simply allow to override the $CARGO_HOME for the current directory with some sort of configuration file: either .cargo/config, or Cargo.toml. It will allow to declare the desired value once and just don't bother, while playing well with both cargo build/docker build experiences at the same time.

The feature is not new and already exists in package managers for other languages. For example, I can specify the packages cache folder in:

  • .yarnrc/.npmrc file for Yarn/NPM;
  • composer.json for Composer.
@lukaslueg
Copy link
Contributor

One can use autoenv to that effect.

@alexcrichton alexcrichton added the C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` label Dec 17, 2018
@tyranron
Copy link
Author

@lukaslueg Docker images has no zsh, however. Win users, generally, too.

@ehuss ehuss added the A-configuration Area: cargo config files and env vars label Apr 6, 2020
@jonhoo
Copy link
Contributor

jonhoo commented Feb 9, 2021

I suppose the tricky part here is that by changing CARGO_HOME you change what configuration files cargo reads (it'll now have to read $CARGO_HOME2/.cargo/config). Imagine something like the following:

# ~/.cargo/config
home = "/path/to/a"

[build]
target = "foo"

Now imagine you invoke cargo from the directory b without CARGO_HOME set. What should the target directory be? ~/.cargo/config says that it should be "foo", but it also says that CARGO_HOME should have a different value. And if it had that different value, then ~/.cargo/config would never have been read, which means that the target was never set. It's a little contrived, but suggests there are some weird corner-cases here.

One way to get around this is to have cargo home no be set in the normal configuration files, but instead with a different file under .cargo. For example, imagine a .cargo/home which should hold the path that CARGO_HOME should be set to. cargo should first check CARGO_HOME, and use that if set. If not set, it should search up the tree for .cargo/home, stopping at the first one it finds and using that as the cargo home. And otherwise, it should use ~/.cargo like today. Only after that process has completed should it start reading configuration files. I think that gets around any weird circular definitions or other recursive shenanigans.

jonhoo pushed a commit to jonhoo/cargo that referenced this issue Feb 9, 2021
This patch allows users to set the Cargo home directory through the file
system using a pointer file in `.cargo/home`. As with `.cargo/config`,
the file is searched for recursively up the tree from the current
working directory. Unlike with `.cargo/config`, the search terminates
when a matching file is found.

The search for `.cargo/home` happens before any `.cargo/config` files
are read, as that may be affected by the choice of Cargo home directory.

The `CARGO_HOME` environment variable is preferred over `.cargo/home`.

Fixes rust-lang#6452.
@jonhoo
Copy link
Contributor

jonhoo commented Feb 9, 2021

I filed a PR that implements the .cargo/home idea: #9154

@epage
Copy link
Contributor

epage commented Oct 30, 2023

In light of https://internals.rust-lang.org/t/pre-rfc-split-cargo-home/19747, I assume what would be wanted is CARGO_CACHE_HOME. I suspect that is something we could make overrideable in .cargo/config.toml.

@epage epage added the S-triage Status: This issue is waiting on initial triage. label Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-configuration Area: cargo config files and env vars C-feature-request Category: proposal for a feature. Before PR, ping rust-lang/cargo if this is not `Feature accepted` S-triage Status: This issue is waiting on initial triage.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants