Skip to content

Commit 263cb09

Browse files
RagnarGrootKoerkampdjc
authored andcommitted
Use default_run from Cargo.toml as default bin target
1 parent efdae7e commit 263cb09

File tree

1 file changed

+32
-13
lines changed

1 file changed

+32
-13
lines changed

src/bin/cargo-flamegraph.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -314,29 +314,48 @@ fn find_unique_target(
314314
});
315315
}
316316

317+
let mut num_packages = 0;
318+
let mut is_default = false;
319+
317320
let mut targets: Vec<_> = packages
318321
.flat_map(|p| {
319-
let Package { targets, name, .. } = p;
322+
let Package {
323+
targets,
324+
name,
325+
default_run,
326+
..
327+
} = p;
328+
num_packages += 1;
329+
if default_run.is_some() {
330+
is_default = true;
331+
}
320332
targets.into_iter().filter_map(move |t| {
321-
t.kind
322-
.iter()
323-
.any(|s| kind.contains(&s.as_str()))
324-
.then(|| BinaryTarget {
325-
package: name.clone(),
326-
target: t.name,
327-
kind: t.kind,
328-
})
333+
// Keep only targets that are of the right kind.
334+
let ok_kind = t.kind.iter().any(|s| kind.contains(&s.as_str()));
335+
// When `default_run` is set, keep only the target with that name.
336+
let default_filter = match &default_run {
337+
None => true,
338+
Some(default_name) => &t.name == default_name,
339+
};
340+
(ok_kind && default_filter).then(|| BinaryTarget {
341+
package: name.clone(),
342+
target: t.name,
343+
kind: t.kind,
344+
})
329345
})
330346
})
331347
.collect();
332348

333349
match targets.as_slice() {
334350
[_] => {
335351
let target = targets.remove(0);
336-
eprintln!(
337-
"automatically selected {} as it is the only valid target",
338-
target
339-
);
352+
// If the selected target is the default_run of the only package, do not print a message.
353+
if num_packages != 1 || !is_default {
354+
eprintln!(
355+
"automatically selected {} as it is the only valid target",
356+
target
357+
);
358+
}
340359
Ok(target)
341360
}
342361
[] => Err(anyhow!(

0 commit comments

Comments
 (0)