From e137ee6979481aad64e524c037618ed6451a7c75 Mon Sep 17 00:00:00 2001 From: Christian Duerr Date: Tue, 14 Jan 2020 01:05:30 +0100 Subject: [PATCH] Use non-ephemeral workspace --- src/cargo/core/workspace.rs | 15 ++++++++++++--- src/cargo/ops/cargo_install.rs | 13 ++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/cargo/core/workspace.rs b/src/cargo/core/workspace.rs index 7854ed2c71f..dc08811f248 100644 --- a/src/cargo/core/workspace.rs +++ b/src/cargo/core/workspace.rs @@ -198,9 +198,7 @@ impl<'cfg> Workspace<'cfg> { target_dir: Option, require_optional_deps: bool, ) -> CargoResult> { - let manifest_path = package.manifest_path(); - let mut ws = Workspace::new_default(manifest_path.to_path_buf(), config); - ws.root_manifest = ws.find_root(&manifest_path)?; + let mut ws = Workspace::new_default(package.manifest_path().to_path_buf(), config); ws.is_ephemeral = true; ws.require_optional_deps = require_optional_deps; let key = ws.current_manifest.parent().unwrap(); @@ -831,6 +829,17 @@ impl<'cfg> Workspace<'cfg> { } Ok(()) } + + pub fn set_target_dir(&mut self, target_dir: Filesystem) { + self.target_dir = Some(target_dir); + } + + // TODO: This seems like the wrong approach + pub fn set_package(&mut self, package: Package) { + let key = self.current_manifest.parent().unwrap(); + let package = MaybePackage::Package(package); + self.packages.packages.insert(key.to_path_buf(), package); + } } impl<'cfg> Packages<'cfg> { diff --git a/src/cargo/ops/cargo_install.rs b/src/cargo/ops/cargo_install.rs index a2c5565e79f..5622c459464 100644 --- a/src/cargo/ops/cargo_install.rs +++ b/src/cargo/ops/cargo_install.rs @@ -202,7 +202,7 @@ fn install_one( let mut td_opt = None; let mut needs_cleanup = false; - let overidden_target_dir = if source_id.is_path() { + let overidden_target_dir = if source_id.is_git() || source_id.is_path() { None } else if let Some(dir) = config.target_dir()? { Some(dir) @@ -220,6 +220,17 @@ fn install_one( None => { let mut ws = Workspace::new(pkg.manifest_path(), config)?; ws.set_require_optional_deps(false); + + // Use tempdir to build git depedencies to prevent bloat in cargo cache + if source_id.is_git() && config.target_dir()?.is_none() { + match TempFileBuilder::new().prefix("cargo-install").tempdir() { + Ok(td) => ws.set_target_dir(Filesystem::new(td.path().to_owned())), + // If tempfile creation fails, write to cargo cache but clean up afterwards + Err(_) => needs_cleanup = true, + } + } + ws.set_package(pkg); + ws } };