-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Add an option to cargo install
to specify a cache directory
#4725
Comments
Hi, I would like to work on this issue. Can I work on this issue or is it taken? I have been looking and fiddling with the source code for 2 days now and I understand some of it pretty well. I do have few questions before I get started. Should |
@ishanjain28 sure! I think one way to implement this would be to perhaps get the existing |
@alexcrichton Should the |
Ok thanks for checking @ishanjain28! I think for now I'd personally prefer to avoid adding a command line argument just yet (more surface area to stabilize on a lot of commands) and instead just document the usage of the env vars here. |
@alexcrichton Ok. Information about |
@ishanjain28 sure yeah, but perhaps we could call it out in the documentation for |
@ishanjain28 thanks a lot for looking into this! However, I'm not seeing the intended behavior when using the environment variables. I'm on
This shows the arguments I never thought to check for an environment variable, so it would definitely be helpful to note this in |
@jtgeibel I tried compiling a project using Command,
Full rustc command,
I'll add documentation about |
@ishanjain28 it looks like the difference is that I'm specifying a Here is the behavior I am seeing locally:
|
@jtgeibel yes, Now I am able to replicate this issue. It arises because of this code here. If no path is specified with From a quick look, A simple fix for this would be to pass |
Passing |
Thank you so much for investigating this @ishanjain28! I've been trying some things locally, what do you think about the following? diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs
index 2c8c405f..c85923e4 100644
--- a/src/cargo/ops/cargo_install.rs
+++ b/src/cargo/ops/cargo_install.rs
@@ -159,13 +159,17 @@ fn install_one(root: &Filesystem,
};
let mut td_opt = None;
+ let mut needs_cleanup = false;
let overidden_target_dir = if source_id.is_path() {
None
+ } else if let Some(dir) = config.target_dir()? {
+ Some(dir)
} else if let Ok(td) = TempDir::new("cargo-install") {
let p = td.path().to_owned();
td_opt = Some(td);
Some(Filesystem::new(p))
} else {
+ needs_cleanup = true;
Some(Filesystem::new(config.cwd().join("target-install")))
};
@@ -311,7 +315,7 @@ fn install_one(root: &Filesystem,
// Reaching here means all actions have succeeded. Clean up.
installed.success();
- if !source_id.is_path() {
+ if needs_cleanup {
// Don't bother grabbing a lock as we're going to blow it all away
// anyway.
let target_dir = ws.target_dir().into_path_unlocked(); |
@jtgeibel Looks good to me. 👍 |
This aligns the behavior of crates.io and `--git` sources with that of `--path` regarding the `CARGO_TARGET_DIR` and `CARGO_BUILD_TARGET_DIR` environment variables. If neither environment variable is set, then a temporary directory is still used when installing from crates.io or `--git`. As discussed in rust-lang#4725, this can be used to enable caching of artifacts between continuous integration builds.
Always respect `CARGO_TARGET_DIR` during `cargo install` This aligns the behavior of crates.io and `--git` sources with that of `--path` regarding the `CARGO_TARGET_DIR` and `CARGO_BUILD_TARGET_DIR` environment variables. If neither environment variable is set, then a temporary directory is still used when installing from crates.io or `--git`. As discussed in #4725, this can be used to enable caching of artifacts between continuous integration builds. /cc @alexcrichton, @ishanjain28
I think this is now documented/added, so closing! |
Hey is there any information / documentation on how this works? Tried caching the |
Hi, this still seems to be an issue for me. |
|
In a CI environment it is sometimes necessary to run
cargo install --force some-other-crate
to produce binaries that are used later in the script. Currently,cargo install
appears to create a temporary directory in which the specified crate and its dependencies are built anew for each invocation of the command. It would be nice if it was possible to cache these artifacts between CI builds.For instance, when running CI for crates.io we install
diesel_cli
,rustfmt-nightly
andclippy
. On a recent build these took 81, 263, and 247 seconds respectively to build. It may be possible to shave nearly 10 minutes off the build time ifcargo install
could reuse cached artifacts from a previous build.It would be even nicer if the same cache directory could be reused such that, for instance,
serde v1.0.19
could be reused when buildingclippy
because it was already built during the install ofrustfmt-nightly
. I believe the existing hashes included in the filenames for build artifacts would ensure that the 2 dependencies are truly equivalent (such as enabling the same features).As an example, if the new flag was called
--cache-dir
it would be awesome if the following "just worked" and reused cached artifacts between all 3 commands when possible:The text was updated successfully, but these errors were encountered: