Skip to content

Commit

Permalink
Use binary name when searching for artifacts (#1747)
Browse files Browse the repository at this point in the history
* Use binary name when searching for artifacts

When there is a single binary declared in the manifest and
it differs from the package name, add it to the list of handles
used for pre-built artifact fetching.

* Simplify `binary_name` assignment with a `match`

* Add e2e test

* Only attempt to use the binary name with `GhCrateMeta` fetcher

* Avoid too much over-allocating.

Technically it should also check if gh-crate-meta resolver is enabled, but it is unlikely for it to be disabled and overallocating for extra n-target should be fine, it is an improvement over doubling the space allocated if the binary_name is Some.

* Fix fmt in crates/binstalk/src/ops/resolve.rs

---------

Co-authored-by: Jiahao XU <[email protected]>
  • Loading branch information
tomasol and NobodyXu authored Jun 14, 2024
1 parent a220952 commit dfa230f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 36 deletions.
4 changes: 3 additions & 1 deletion crates/binstalk-fetchers/src/gh_crate_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use crate::{
SignaturePolicy, SignatureVerifier, TargetDataErased, DEFAULT_GH_API_RETRY_DURATION,
};

pub const FETCHER_GH_CRATE_META: &str = "GhCrateMeta";

pub(crate) mod hosting;

pub struct GhCrateMeta {
Expand Down Expand Up @@ -391,7 +393,7 @@ impl super::Fetcher for GhCrateMeta {
}

fn fetcher_name(&self) -> &'static str {
"GhCrateMeta"
FETCHER_GH_CRATE_META
}

fn is_third_party(&self) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion crates/binstalk-registry/src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl ManifestVisitor {

// Load and parse manifest
let mut manifest = Manifest::from_slice_with_metadata(&self.cargo_toml_content)?;

debug!("Manifest: {manifest:?}");
// Checks vfs for binary output names
manifest.complete_from_abstract_filesystem::<Value, _>(&self.vfs, None)?;

Expand Down
99 changes: 66 additions & 33 deletions crates/binstalk/src/ops/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
sync::Arc,
};

use binstalk_fetchers::FETCHER_GH_CRATE_META;
use compact_str::{CompactString, ToCompactString};
use itertools::Itertools;
use leon::Template;
Expand Down Expand Up @@ -91,45 +92,77 @@ async fn resolve_inner(
.collect::<Result<Vec<_>, _>>()?;
let resolvers = &opts.resolvers;

let mut handles: Vec<(Arc<dyn Fetcher>, _)> =
Vec::with_capacity(desired_targets.len() * resolvers.len());

let data = Arc::new(Data::new(
package_info.name.clone(),
package_info.version_str.clone(),
package_info.repo.clone(),
));
let binary_name = match package_info.binaries.as_slice() {
[bin] if bin.name != package_info.name => Some(CompactString::from(bin.name.as_str())),
_ => None,
};

handles.extend(
resolvers
.iter()
.cartesian_product(desired_targets.into_iter().map(|(triple, target)| {
debug!("Building metadata for target: {target}");
let mut handles: Vec<(Arc<dyn Fetcher>, _)> = Vec::with_capacity(
desired_targets.len() * resolvers.len()
+ if binary_name.is_some() {
desired_targets.len()
} else {
0
},
);

let target_meta = package_info.meta.merge_overrides(
iter::once(&opts.cli_overrides).chain(package_info.overrides.get(target)),
);
let mut handles_fn =
|data: Arc<Data>, filter_fetcher_by_name_predicate: fn(&'static str) -> bool| {
handles.extend(
resolvers
.iter()
.cartesian_product(desired_targets.clone().into_iter().map(
|(triple, target)| {
debug!("Building metadata for target: {target}");

let target_meta = package_info.meta.merge_overrides(
iter::once(&opts.cli_overrides)
.chain(package_info.overrides.get(target)),
);

debug!("Found metadata: {target_meta:?}");
debug!("Found metadata: {target_meta:?}");

Arc::new(TargetData {
target: target.clone(),
meta: target_meta,
target_related_info: triple,
})
},
))
.filter_map(|(f, target_data)| {
let fetcher = f(
opts.client.clone(),
opts.gh_api_client.clone(),
data.clone(),
target_data,
opts.signature_policy,
);
filter_fetcher_by_name_predicate(fetcher.fetcher_name())
.then_some((fetcher.clone(), AutoAbortJoinHandle::new(fetcher.find())))
}),
)
};

Arc::new(TargetData {
target: target.clone(),
meta: target_meta,
target_related_info: triple,
})
}))
.map(|(f, target_data)| {
let fetcher = f(
opts.client.clone(),
opts.gh_api_client.clone(),
data.clone(),
target_data,
opts.signature_policy,
);
(fetcher.clone(), AutoAbortJoinHandle::new(fetcher.find()))
}),
handles_fn(
Arc::new(Data::new(
package_info.name.clone(),
package_info.version_str.clone(),
package_info.repo.clone(),
)),
|_| true,
);

if let Some(binary_name) = binary_name {
handles_fn(
Arc::new(Data::new(
binary_name,
package_info.version_str.clone(),
package_info.repo.clone(),
)),
|name| name == FETCHER_GH_CRATE_META,
);
}

for (fetcher, handle) in handles {
fetcher.clone().report_to_upstream();
match handle.flattened_join().await {
Expand Down
9 changes: 8 additions & 1 deletion e2e-tests/live.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ unset CARGO_INSTALL_ROOT
# to find versions matching <= 1.3.3
# - `cargo-quickinstall` would test `fetch_crate_cratesio_version_matched` ability
# to find latest stable version.
crates="b3sum@<=1.3.3 [email protected] [email protected] [email protected] [email protected] [email protected] cargo-quickinstall [email protected]"
# - `git-mob-tool tests the using of using a binary name (`git-mob`) different
# from the package name.
crates="b3sum@<=1.3.3 [email protected] [email protected] [email protected] [email protected] [email protected] cargo-quickinstall [email protected] [email protected]"

CARGO_HOME=$(mktemp -d 2>/dev/null || mktemp -d -t 'cargo-home')
export CARGO_HOME
Expand Down Expand Up @@ -59,3 +61,8 @@ jj_version="$(jj --version)"
echo "$jj_version"

[ "$jj_version" = "jj 0.18.0-9fb5307b7886e390c02817af7c31b403f0279144" ]

git_mob_version="$(git-mob --version)"
echo "$git_mob_version"

[ "$git_mob_version" = "git-mob-tool 1.6.1" ]
1 change: 1 addition & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[toolchain]
channel = "stable"
profile = "minimal"
components = ["rustfmt", "clippy"]

0 comments on commit dfa230f

Please sign in to comment.