Skip to content

Commit

Permalink
fix: collect executables in --dry-run
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowrey committed Jul 24, 2024
1 parent 6d0b475 commit a1df193
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
68 changes: 44 additions & 24 deletions src/cargo/core/compiler/build_runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,49 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
})
}

fn collect_tests_and_executables(&mut self, unit: &Unit) -> CargoResult<()> {
for output in self.outputs(unit)?.iter() {
if output.flavor == FileFlavor::DebugInfo || output.flavor == FileFlavor::Auxiliary {
continue;
}

let bindst = output.bin_dst();

if unit.mode == CompileMode::Test {
self.compilation
.tests
.push(self.unit_output(unit, &output.path));
} else if unit.target.is_executable() {
self.compilation
.binaries
.push(self.unit_output(unit, bindst));
} else if unit.target.is_cdylib()
&& !self.compilation.cdylibs.iter().any(|uo| uo.unit == *unit)
{
self.compilation
.cdylibs
.push(self.unit_output(unit, bindst));
}
}
Ok(())
}

pub fn dry_run(mut self) -> CargoResult<Compilation<'gctx>> {
let _lock = self
.bcx
.gctx
.acquire_package_cache_lock(CacheLockMode::Shared)?;
self.lto = super::lto::generate(self.bcx)?;
self.prepare_units()?;
self.prepare()?;

for unit in &self.bcx.roots {
self.collect_tests_and_executables(unit)?;
}

Ok(self.compilation)
}

/// Starts compilation, waits for it to finish, and returns information
/// about the result of compilation.
///
Expand Down Expand Up @@ -215,30 +258,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
// Collect the result of the build into `self.compilation`.
for unit in &self.bcx.roots {
// Collect tests and executables.
for output in self.outputs(unit)?.iter() {
if output.flavor == FileFlavor::DebugInfo || output.flavor == FileFlavor::Auxiliary
{
continue;
}

let bindst = output.bin_dst();

if unit.mode == CompileMode::Test {
self.compilation
.tests
.push(self.unit_output(unit, &output.path));
} else if unit.target.is_executable() {
self.compilation
.binaries
.push(self.unit_output(unit, bindst));
} else if unit.target.is_cdylib()
&& !self.compilation.cdylibs.iter().any(|uo| uo.unit == *unit)
{
self.compilation
.cdylibs
.push(self.unit_output(unit, bindst));
}
}
self.collect_tests_and_executables(unit)?;

// Collect information for `rustdoc --test`.
if unit.mode.is_doc_test() {
Expand Down
3 changes: 2 additions & 1 deletion src/cargo/ops/cargo_compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ pub fn compile_ws<'a>(
return Compilation::new(&bcx);
}
if options.build_config.dry_run {
return Compilation::new(&bcx);
let build_runner = BuildRunner::new(&bcx)?;
return build_runner.dry_run();
}
crate::core::gc::auto_gc(bcx.gctx);
let build_runner = BuildRunner::new(&bcx)?;
Expand Down
14 changes: 8 additions & 6 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ impl<'gctx> InstallablePackage<'gctx> {
.iter()
.filter(|t| t.is_executable())
.collect();
if !binaries.is_empty() && !dry_run {
if !binaries.is_empty() {
self.gctx
.shell()
.warn(make_warning_about_missing_features(&binaries))?;
Expand Down Expand Up @@ -423,7 +423,7 @@ impl<'gctx> InstallablePackage<'gctx> {
for &(bin, src) in binaries.iter() {
let dst = staging_dir.path().join(bin);
// Try to move if `target_dir` is transient.
if !self.source_id.is_path() && fs::rename(src, &dst).is_ok() {
if (!self.source_id.is_path() && fs::rename(src, &dst).is_ok()) || dry_run {
continue;
}
paths::copy(src, &dst)?;
Expand All @@ -442,9 +442,11 @@ impl<'gctx> InstallablePackage<'gctx> {
let src = staging_dir.path().join(bin);
let dst = dst.join(bin);
self.gctx.shell().status("Installing", dst.display())?;
fs::rename(&src, &dst).with_context(|| {
format!("failed to move `{}` to `{}`", src.display(), dst.display())
})?;
if !dry_run {
fs::rename(&src, &dst).with_context(|| {
format!("failed to move `{}` to `{}`", src.display(), dst.display())
})?;
}
installed.bins.push(dst);
successful_bins.insert(bin.to_string());
}
Expand Down Expand Up @@ -747,7 +749,7 @@ pub fn install(
let path = gctx.get_env_os("PATH").unwrap_or_default();
let dst_in_path = env::split_paths(&path).any(|path| path == dst);

if !dst_in_path && !dry_run {
if !dst_in_path {
gctx.shell().warn(&format!(
"be sure to add `{}` to your PATH to be \
able to run the installed binaries",
Expand Down
3 changes: 3 additions & 0 deletions tests/testsuite/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2742,6 +2742,9 @@ fn dry_run() {
[DOWNLOADING] crates ...
[DOWNLOADED] foo v0.0.1 (registry `dummy-registry`)
[INSTALLING] foo v0.0.1
[INSTALLING] [ROOT]/home/.cargo/bin/foo[EXE]
[INSTALLED] package `foo v0.0.1` (executable `foo[EXE]`)
[WARNING] be sure to add `[ROOT]/home/.cargo/bin` to your PATH to be able to run the installed binaries
"#]])
.run();
Expand Down

0 comments on commit a1df193

Please sign in to comment.