Skip to content

Commit f7e89ea

Browse files
committed
Refactor additional validation steps to determine_package
1 parent 3beb322 commit f7e89ea

File tree

1 file changed

+64
-59
lines changed

1 file changed

+64
-59
lines changed

src/cargo/ops/cargo_install.rs

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,11 @@ pub fn install(
5959
vers,
6060
opts,
6161
force,
62+
no_track,
6263
true,
6364
)?;
6465
if let Some(pkg) = pkg {
65-
install_one(
66-
config, &root, source_id, from_cwd, vers, opts, force, no_track, pkg,
67-
)?;
66+
install_one(config, &root, source_id, vers, opts, force, no_track, pkg)?;
6867
}
6968
(true, false)
7069
} else {
@@ -86,6 +85,7 @@ pub fn install(
8685
vers,
8786
opts,
8887
force,
88+
no_track,
8989
!did_update,
9090
) {
9191
Ok(Some(pkg)) => {
@@ -105,9 +105,7 @@ pub fn install(
105105
continue;
106106
}
107107
};
108-
match install_one(
109-
config, &root, source_id, from_cwd, vers, opts, force, no_track, pkg,
110-
) {
108+
match install_one(config, &root, source_id, vers, opts, force, no_track, pkg) {
111109
Ok(()) => {
112110
succeeded.push(krate);
113111
}
@@ -169,6 +167,7 @@ fn determine_package(
169167
vers: Option<&str>,
170168
opts: &ops::CompileOptions,
171169
force: bool,
170+
no_track: bool,
172171
needs_update_if_source_is_index: bool,
173172
) -> CargoResult<Option<Package>> {
174173
if let Some(name) = krate {
@@ -266,23 +265,8 @@ fn determine_package(
266265
)
267266
}
268267
};
269-
Ok(Some(pkg))
270-
}
271268

272-
fn install_one(
273-
config: &Config,
274-
root: &Filesystem,
275-
source_id: SourceId,
276-
from_cwd: bool,
277-
vers: Option<&str>,
278-
opts: &ops::CompileOptions,
279-
force: bool,
280-
no_track: bool,
281-
pkg: Package,
282-
) -> CargoResult<()> {
283-
let dst = root.join("bin").into_path_unlocked();
284-
285-
let (mut ws, rustc, target) = make_ws_rustc_target(config, opts, &source_id, pkg.clone())?;
269+
let (ws, rustc, target) = make_ws_rustc_target(config, opts, &source_id, pkg.clone())?;
286270
// If we're installing in --locked mode and there's no `Cargo.lock` published
287271
// ie. the bin was published before https://github.com/rust-lang/cargo/pull/7026
288272
if config.locked() && !ws.root().join("Cargo.lock").exists() {
@@ -299,22 +283,6 @@ fn install_one(
299283
ws.current()?.clone()
300284
};
301285

302-
let mut td_opt = None;
303-
let mut needs_cleanup = false;
304-
if !source_id.is_path() {
305-
let target_dir = if let Some(dir) = config.target_dir()? {
306-
dir
307-
} else if let Ok(td) = TempFileBuilder::new().prefix("cargo-install").tempdir() {
308-
let p = td.path().to_owned();
309-
td_opt = Some(td);
310-
Filesystem::new(p)
311-
} else {
312-
needs_cleanup = true;
313-
Filesystem::new(config.cwd().join("target-install"))
314-
};
315-
ws.set_target_dir(target_dir);
316-
}
317-
318286
if from_cwd {
319287
if pkg.manifest().edition() == Edition::Edition2015 {
320288
config.shell().warn(
@@ -345,40 +313,77 @@ fn install_one(
345313
);
346314
}
347315

348-
// Helper for --no-track flag to make sure it doesn't overwrite anything.
349-
let no_track_duplicates = || -> CargoResult<BTreeMap<String, Option<PackageId>>> {
350-
let duplicates: BTreeMap<String, Option<PackageId>> = exe_names(&pkg, &opts.filter)
351-
.into_iter()
352-
.filter(|name| dst.join(name).exists())
353-
.map(|name| (name, None))
354-
.collect();
355-
if !force && !duplicates.is_empty() {
356-
let mut msg: Vec<String> = duplicates
357-
.iter()
358-
.map(|(name, _)| format!("binary `{}` already exists in destination", name))
359-
.collect();
360-
msg.push("Add --force to overwrite".to_string());
361-
bail!("{}", msg.join("\n"));
362-
}
363-
Ok(duplicates)
364-
};
365-
366316
// WARNING: no_track does not perform locking, so there is no protection
367317
// of concurrent installs.
368318
if no_track {
369319
// Check for conflicts.
370-
no_track_duplicates()?;
320+
no_track_duplicates(&pkg, opts, &dst, force)?;
371321
} else if is_installed(&pkg, config, opts, &rustc, &target, root, &dst, force)? {
372322
let msg = format!(
373323
"package `{}` is already installed, use --force to override",
374324
pkg
375325
);
376326
config.shell().status("Ignored", &msg)?;
377-
return Ok(());
327+
return Ok(None);
378328
}
379329

330+
Ok(Some(pkg))
331+
}
332+
333+
fn no_track_duplicates(
334+
pkg: &Package,
335+
opts: &ops::CompileOptions,
336+
dst: &Path,
337+
force: bool,
338+
) -> CargoResult<BTreeMap<String, Option<PackageId>>> {
339+
// Helper for --no-track flag to make sure it doesn't overwrite anything.
340+
let duplicates: BTreeMap<String, Option<PackageId>> = exe_names(&pkg, &opts.filter)
341+
.into_iter()
342+
.filter(|name| dst.join(name).exists())
343+
.map(|name| (name, None))
344+
.collect();
345+
if !force && !duplicates.is_empty() {
346+
let mut msg: Vec<String> = duplicates
347+
.iter()
348+
.map(|(name, _)| format!("binary `{}` already exists in destination", name))
349+
.collect();
350+
msg.push("Add --force to overwrite".to_string());
351+
bail!("{}", msg.join("\n"));
352+
}
353+
Ok(duplicates)
354+
}
355+
356+
fn install_one<'cfg>(
357+
config: &Config,
358+
root: &Filesystem,
359+
source_id: SourceId,
360+
vers: Option<&str>,
361+
opts: &ops::CompileOptions,
362+
force: bool,
363+
no_track: bool,
364+
pkg: Package,
365+
) -> CargoResult<()> {
380366
config.shell().status("Installing", &pkg)?;
381367

368+
let dst = root.join("bin").into_path_unlocked();
369+
let (mut ws, rustc, target) = make_ws_rustc_target(config, opts, &source_id, pkg.clone())?;
370+
371+
let mut td_opt = None;
372+
let mut needs_cleanup = false;
373+
if !source_id.is_path() {
374+
let target_dir = if let Some(dir) = config.target_dir()? {
375+
dir
376+
} else if let Ok(td) = TempFileBuilder::new().prefix("cargo-install").tempdir() {
377+
let p = td.path().to_owned();
378+
td_opt = Some(td);
379+
Filesystem::new(p)
380+
} else {
381+
needs_cleanup = true;
382+
Filesystem::new(config.cwd().join("target-install"))
383+
};
384+
ws.set_target_dir(target_dir);
385+
}
386+
382387
check_yanked_install(&ws)?;
383388

384389
let exec: Arc<dyn Executor> = Arc::new(DefaultExecutor);
@@ -414,7 +419,7 @@ fn install_one(
414419
binaries.sort_unstable();
415420

416421
let (tracker, duplicates) = if no_track {
417-
(None, no_track_duplicates()?)
422+
(None, no_track_duplicates(&pkg, opts, &dst, force)?)
418423
} else {
419424
let tracker = InstallTracker::load(config, root)?;
420425
let (_freshness, duplicates) =

0 commit comments

Comments
 (0)