Skip to content

Commit a1df193

Browse files
committed
fix: collect executables in --dry-run
1 parent 6d0b475 commit a1df193

File tree

4 files changed

+57
-31
lines changed

4 files changed

+57
-31
lines changed

src/cargo/core/compiler/build_runner/mod.rs

+44-24
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,49 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
126126
})
127127
}
128128

129+
fn collect_tests_and_executables(&mut self, unit: &Unit) -> CargoResult<()> {
130+
for output in self.outputs(unit)?.iter() {
131+
if output.flavor == FileFlavor::DebugInfo || output.flavor == FileFlavor::Auxiliary {
132+
continue;
133+
}
134+
135+
let bindst = output.bin_dst();
136+
137+
if unit.mode == CompileMode::Test {
138+
self.compilation
139+
.tests
140+
.push(self.unit_output(unit, &output.path));
141+
} else if unit.target.is_executable() {
142+
self.compilation
143+
.binaries
144+
.push(self.unit_output(unit, bindst));
145+
} else if unit.target.is_cdylib()
146+
&& !self.compilation.cdylibs.iter().any(|uo| uo.unit == *unit)
147+
{
148+
self.compilation
149+
.cdylibs
150+
.push(self.unit_output(unit, bindst));
151+
}
152+
}
153+
Ok(())
154+
}
155+
156+
pub fn dry_run(mut self) -> CargoResult<Compilation<'gctx>> {
157+
let _lock = self
158+
.bcx
159+
.gctx
160+
.acquire_package_cache_lock(CacheLockMode::Shared)?;
161+
self.lto = super::lto::generate(self.bcx)?;
162+
self.prepare_units()?;
163+
self.prepare()?;
164+
165+
for unit in &self.bcx.roots {
166+
self.collect_tests_and_executables(unit)?;
167+
}
168+
169+
Ok(self.compilation)
170+
}
171+
129172
/// Starts compilation, waits for it to finish, and returns information
130173
/// about the result of compilation.
131174
///
@@ -215,30 +258,7 @@ impl<'a, 'gctx> BuildRunner<'a, 'gctx> {
215258
// Collect the result of the build into `self.compilation`.
216259
for unit in &self.bcx.roots {
217260
// Collect tests and executables.
218-
for output in self.outputs(unit)?.iter() {
219-
if output.flavor == FileFlavor::DebugInfo || output.flavor == FileFlavor::Auxiliary
220-
{
221-
continue;
222-
}
223-
224-
let bindst = output.bin_dst();
225-
226-
if unit.mode == CompileMode::Test {
227-
self.compilation
228-
.tests
229-
.push(self.unit_output(unit, &output.path));
230-
} else if unit.target.is_executable() {
231-
self.compilation
232-
.binaries
233-
.push(self.unit_output(unit, bindst));
234-
} else if unit.target.is_cdylib()
235-
&& !self.compilation.cdylibs.iter().any(|uo| uo.unit == *unit)
236-
{
237-
self.compilation
238-
.cdylibs
239-
.push(self.unit_output(unit, bindst));
240-
}
241-
}
261+
self.collect_tests_and_executables(unit)?;
242262

243263
// Collect information for `rustdoc --test`.
244264
if unit.mode.is_doc_test() {

src/cargo/ops/cargo_compile/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ pub fn compile_ws<'a>(
155155
return Compilation::new(&bcx);
156156
}
157157
if options.build_config.dry_run {
158-
return Compilation::new(&bcx);
158+
let build_runner = BuildRunner::new(&bcx)?;
159+
return build_runner.dry_run();
159160
}
160161
crate::core::gc::auto_gc(bcx.gctx);
161162
let build_runner = BuildRunner::new(&bcx)?;

src/cargo/ops/cargo_install.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ impl<'gctx> InstallablePackage<'gctx> {
386386
.iter()
387387
.filter(|t| t.is_executable())
388388
.collect();
389-
if !binaries.is_empty() && !dry_run {
389+
if !binaries.is_empty() {
390390
self.gctx
391391
.shell()
392392
.warn(make_warning_about_missing_features(&binaries))?;
@@ -423,7 +423,7 @@ impl<'gctx> InstallablePackage<'gctx> {
423423
for &(bin, src) in binaries.iter() {
424424
let dst = staging_dir.path().join(bin);
425425
// Try to move if `target_dir` is transient.
426-
if !self.source_id.is_path() && fs::rename(src, &dst).is_ok() {
426+
if (!self.source_id.is_path() && fs::rename(src, &dst).is_ok()) || dry_run {
427427
continue;
428428
}
429429
paths::copy(src, &dst)?;
@@ -442,9 +442,11 @@ impl<'gctx> InstallablePackage<'gctx> {
442442
let src = staging_dir.path().join(bin);
443443
let dst = dst.join(bin);
444444
self.gctx.shell().status("Installing", dst.display())?;
445-
fs::rename(&src, &dst).with_context(|| {
446-
format!("failed to move `{}` to `{}`", src.display(), dst.display())
447-
})?;
445+
if !dry_run {
446+
fs::rename(&src, &dst).with_context(|| {
447+
format!("failed to move `{}` to `{}`", src.display(), dst.display())
448+
})?;
449+
}
448450
installed.bins.push(dst);
449451
successful_bins.insert(bin.to_string());
450452
}
@@ -747,7 +749,7 @@ pub fn install(
747749
let path = gctx.get_env_os("PATH").unwrap_or_default();
748750
let dst_in_path = env::split_paths(&path).any(|path| path == dst);
749751

750-
if !dst_in_path && !dry_run {
752+
if !dst_in_path {
751753
gctx.shell().warn(&format!(
752754
"be sure to add `{}` to your PATH to be \
753755
able to run the installed binaries",

tests/testsuite/install.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2742,6 +2742,9 @@ fn dry_run() {
27422742
[DOWNLOADING] crates ...
27432743
[DOWNLOADED] foo v0.0.1 (registry `dummy-registry`)
27442744
[INSTALLING] foo v0.0.1
2745+
[INSTALLING] [ROOT]/home/.cargo/bin/foo[EXE]
2746+
[INSTALLED] package `foo v0.0.1` (executable `foo[EXE]`)
2747+
[WARNING] be sure to add `[ROOT]/home/.cargo/bin` to your PATH to be able to run the installed binaries
27452748
27462749
"#]])
27472750
.run();

0 commit comments

Comments
 (0)