Skip to content

Commit 328b7d6

Browse files
committed
Auto merge of #7768 - chrisduerr:install-workspaces-from-git, r=ehuss
Search for root manifest with ephemeral workspaces Fixes #5495. This seems like it's too simple to just work like this, but after trying a few different things, this was the only solution which worked reliably for me. I've verified that no `/target` is present in the actual checkout location, the target directory used is actually the one created in `/tmp`. I've also verified that both workspaces and "normal" packages still install through git and that a normal `cargo install --path` works too (though that doesn't use ephemeral workspaces anyways).
2 parents 9d32b7b + 601d04c commit 328b7d6

File tree

3 files changed

+60
-26
lines changed

3 files changed

+60
-26
lines changed

src/cargo/core/workspace.rs

+4
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,10 @@ impl<'cfg> Workspace<'cfg> {
829829
}
830830
Ok(())
831831
}
832+
833+
pub fn set_target_dir(&mut self, target_dir: Filesystem) {
834+
self.target_dir = Some(target_dir);
835+
}
832836
}
833837

834838
impl<'cfg> Packages<'cfg> {

src/cargo/ops/cargo_install.rs

+26-22
Original file line numberDiff line numberDiff line change
@@ -200,31 +200,35 @@ fn install_one(
200200
)?
201201
};
202202

203-
let mut td_opt = None;
204-
let mut needs_cleanup = false;
205-
let overidden_target_dir = if source_id.is_path() {
206-
None
207-
} else if let Some(dir) = config.target_dir()? {
208-
Some(dir)
209-
} else if let Ok(td) = TempFileBuilder::new().prefix("cargo-install").tempdir() {
210-
let p = td.path().to_owned();
211-
td_opt = Some(td);
212-
Some(Filesystem::new(p))
203+
let (mut ws, git_package) = if source_id.is_git() {
204+
// Don't use ws.current() in order to keep the package source as a git source so that
205+
// install tracking uses the correct source.
206+
(Workspace::new(pkg.manifest_path(), config)?, Some(&pkg))
207+
} else if source_id.is_path() {
208+
(Workspace::new(pkg.manifest_path(), config)?, None)
213209
} else {
214-
needs_cleanup = true;
215-
Some(Filesystem::new(config.cwd().join("target-install")))
216-
};
217-
218-
let mut ws = match overidden_target_dir {
219-
Some(dir) => Workspace::ephemeral(pkg, config, Some(dir), false)?,
220-
None => {
221-
let mut ws = Workspace::new(pkg.manifest_path(), config)?;
222-
ws.set_require_optional_deps(false);
223-
ws
224-
}
210+
(Workspace::ephemeral(pkg, config, None, false)?, None)
225211
};
226212
ws.set_ignore_lock(config.lock_update_allowed());
227-
let pkg = ws.current()?;
213+
ws.set_require_optional_deps(false);
214+
215+
let mut td_opt = None;
216+
let mut needs_cleanup = false;
217+
if !source_id.is_path() {
218+
let target_dir = if let Some(dir) = config.target_dir()? {
219+
dir
220+
} else if let Ok(td) = TempFileBuilder::new().prefix("cargo-install").tempdir() {
221+
let p = td.path().to_owned();
222+
td_opt = Some(td);
223+
Filesystem::new(p)
224+
} else {
225+
needs_cleanup = true;
226+
Filesystem::new(config.cwd().join("target-install"))
227+
};
228+
ws.set_target_dir(target_dir);
229+
}
230+
231+
let pkg = git_package.map_or_else(|| ws.current(), |pkg| Ok(pkg))?;
228232

229233
if from_cwd {
230234
if pkg.manifest().edition() == Edition::Edition2015 {

tests/testsuite/install.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -316,10 +316,10 @@ fn multiple_crates_git_all() {
316316
let p = git::repo(&paths::root().join("foo"))
317317
.file(
318318
"Cargo.toml",
319-
r#"\
320-
[workspace]
321-
members = ["bin1", "bin2"]
322-
"#,
319+
r#"
320+
[workspace]
321+
members = ["bin1", "bin2"]
322+
"#,
323323
)
324324
.file("bin1/Cargo.toml", &basic_manifest("bin1", "0.1.0"))
325325
.file("bin2/Cargo.toml", &basic_manifest("bin2", "0.1.0"))
@@ -1422,3 +1422,29 @@ fn install_version_req() {
14221422
.with_stderr_contains("[INSTALLING] foo v0.0.3")
14231423
.run();
14241424
}
1425+
1426+
#[cargo_test]
1427+
fn git_install_reads_workspace_manifest() {
1428+
let p = git::repo(&paths::root().join("foo"))
1429+
.file(
1430+
"Cargo.toml",
1431+
r#"
1432+
[workspace]
1433+
members = ["bin1"]
1434+
1435+
[profile.release]
1436+
incremental = 3
1437+
"#,
1438+
)
1439+
.file("bin1/Cargo.toml", &basic_manifest("bin1", "0.1.0"))
1440+
.file(
1441+
"bin1/src/main.rs",
1442+
r#"fn main() { println!("Hello, world!"); }"#,
1443+
)
1444+
.build();
1445+
1446+
cargo_process(&format!("install --git {}", p.url().to_string()))
1447+
.with_status(101)
1448+
.with_stderr_contains(" invalid type: integer `3`[..]")
1449+
.run();
1450+
}

0 commit comments

Comments
 (0)