Skip to content

Commit 47d35a1

Browse files
committed
fix(cargo-codspeed): fix build when package is included in its deps
Fix #37
1 parent 4cf6291 commit 47d35a1

File tree

6 files changed

+75
-8
lines changed

6 files changed

+75
-8
lines changed

crates/cargo-codspeed/src/build.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ fn get_compile_options(
1818
features: &Option<Vec<String>>,
1919
package: &Package,
2020
benches: Vec<&str>,
21+
is_root_package: bool,
2122
) -> Result<CompileOptions> {
2223
let mut compile_opts = CompileOptions::new(config, CompileMode::Build)?;
23-
compile_opts.spec = Packages::Packages(vec![package.name().to_string()]);
24+
25+
// if the package is not the root package, we need to specify the package to build
26+
if !is_root_package {
27+
compile_opts.spec = Packages::Packages(vec![package.name().to_string()]);
28+
}
2429
if let Some(features) = features {
2530
compile_opts.cli_features.features = Rc::new(
2631
features
@@ -51,14 +56,15 @@ pub fn build_benches(
5156
package_name: Option<String>,
5257
features: Option<Vec<String>>,
5358
) -> Result<()> {
59+
let is_root_package = package_name.is_none();
5460
let package = match package_name.as_ref() {
55-
Some(package_name) => ws
56-
.members()
57-
.find(|p| p.name().to_string() == *package_name)
58-
.ok_or_else(|| anyhow!("Package {} not found", package_name))?,
59-
None => ws.current().map_err(|_| anyhow!("No package found. If working with a workspace please use the -p option to specify a member."))?,
60-
};
61+
Some(package_name) => ws.members()
62+
.find(|p| p.name().to_string() == *package_name)
63+
.ok_or_else(|| anyhow!("Package {} not found", package_name))?
6164

65+
,
66+
None => ws.current().map_err(|_| anyhow!("No package found. If working with a workspace please use the -p option to specify a member."))?
67+
};
6268
let all_benches = package
6369
.targets()
6470
.iter()
@@ -99,7 +105,8 @@ pub fn build_benches(
99105
ws.config()
100106
.shell()
101107
.status_with_color("Building", benches_names_str.clone(), Color::Yellow)?;
102-
let compile_opts = get_compile_options(config, &features, package, benches_names)?;
108+
let compile_opts =
109+
get_compile_options(config, &features, package, benches_names, is_root_package)?;
103110
let result = cargo::ops::compile(ws, &compile_opts)?;
104111
let built_benches = result
105112
.tests

crates/cargo-codspeed/tests/helpers.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub enum Project {
2020
Simple,
2121
Features,
2222
Workspace,
23+
PackageInDeps,
2324
}
2425

2526
pub fn setup(dir: &str, project: Project) -> String {
@@ -55,6 +56,13 @@ pub fn setup(dir: &str, project: Project) -> String {
5556
workspace_root.join("crates").to_str().unwrap(),
5657
);
5758
}
59+
Project::PackageInDeps => {
60+
replace_in_file(
61+
tmp_dir.join("Cargo.toml").to_str().unwrap(),
62+
"../../..",
63+
workspace_root.join("crates").to_str().unwrap(),
64+
);
65+
}
5866
}
5967
tmp_dir.to_str().unwrap().to_string()
6068
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Cargo.lock
2+
target/
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "blit"
3+
version = "0.8.5"
4+
edition = "2021"
5+
publish = false
6+
7+
[dependencies]
8+
bencher = "0.1.5"
9+
codspeed = { path = "../../../codspeed" }
10+
codspeed-bencher-compat = { path = "../../../bencher_compat" }
11+
12+
[dev-dependencies]
13+
blit = "*"
14+
15+
[[bench]]
16+
name = "bencher_example"
17+
harness = false
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use codspeed::codspeed::black_box;
2+
use codspeed_bencher_compat::{benchmark_group, benchmark_main, Bencher};
3+
4+
pub fn a(bench: &mut Bencher) {
5+
bench.iter(|| (0..100).fold(0, |x, y| black_box(x + y)))
6+
}
7+
8+
pub fn b(bench: &mut Bencher) {
9+
const N: usize = 1024;
10+
bench.iter(|| vec![0u8; N]);
11+
12+
bench.bytes = N as u64;
13+
}
14+
15+
benchmark_group!(benches, a, b);
16+
benchmark_main!(benches);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use predicates::str::contains;
2+
3+
mod helpers;
4+
use helpers::*;
5+
6+
const DIR: &str = "tests/package_in_deps.in";
7+
8+
#[test]
9+
fn test_package_in_deps_build() {
10+
let dir = setup(DIR, Project::PackageInDeps);
11+
cargo_codspeed(&dir)
12+
.arg("build")
13+
.assert()
14+
.success()
15+
.stderr(contains("Finished built 1 benchmark suite(s)"));
16+
teardown(dir);
17+
}

0 commit comments

Comments
 (0)