Use binary name when searching for artifacts (#1747)

* 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 <Jiahao_XU@outlook.com>
This commit is contained in:
Tomas Olvecky 2024-06-14 06:52:37 +02:00 committed by GitHub
parent a220952fd6
commit dfa230f039
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 82 additions and 39 deletions

View file

@ -15,6 +15,8 @@ use crate::{
SignaturePolicy, SignatureVerifier, TargetDataErased, DEFAULT_GH_API_RETRY_DURATION, SignaturePolicy, SignatureVerifier, TargetDataErased, DEFAULT_GH_API_RETRY_DURATION,
}; };
pub const FETCHER_GH_CRATE_META: &str = "GhCrateMeta";
pub(crate) mod hosting; pub(crate) mod hosting;
pub struct GhCrateMeta { pub struct GhCrateMeta {
@ -391,7 +393,7 @@ impl super::Fetcher for GhCrateMeta {
} }
fn fetcher_name(&self) -> &'static str { fn fetcher_name(&self) -> &'static str {
"GhCrateMeta" FETCHER_GH_CRATE_META
} }
fn is_third_party(&self) -> bool { fn is_third_party(&self) -> bool {

View file

@ -71,7 +71,7 @@ impl ManifestVisitor {
// Load and parse manifest // Load and parse manifest
let mut manifest = Manifest::from_slice_with_metadata(&self.cargo_toml_content)?; let mut manifest = Manifest::from_slice_with_metadata(&self.cargo_toml_content)?;
debug!("Manifest: {manifest:?}");
// Checks vfs for binary output names // Checks vfs for binary output names
manifest.complete_from_abstract_filesystem::<Value, _>(&self.vfs, None)?; manifest.complete_from_abstract_filesystem::<Value, _>(&self.vfs, None)?;

View file

@ -7,6 +7,7 @@ use std::{
sync::Arc, sync::Arc,
}; };
use binstalk_fetchers::FETCHER_GH_CRATE_META;
use compact_str::{CompactString, ToCompactString}; use compact_str::{CompactString, ToCompactString};
use itertools::Itertools; use itertools::Itertools;
use leon::Template; use leon::Template;
@ -91,45 +92,77 @@ async fn resolve_inner(
.collect::<Result<Vec<_>, _>>()?; .collect::<Result<Vec<_>, _>>()?;
let resolvers = &opts.resolvers; let resolvers = &opts.resolvers;
let mut handles: Vec<(Arc<dyn Fetcher>, _)> = let binary_name = match package_info.binaries.as_slice() {
Vec::with_capacity(desired_targets.len() * resolvers.len()); [bin] if bin.name != package_info.name => Some(CompactString::from(bin.name.as_str())),
_ => None,
};
let data = Arc::new(Data::new( let mut handles: Vec<(Arc<dyn Fetcher>, _)> = Vec::with_capacity(
package_info.name.clone(), desired_targets.len() * resolvers.len()
package_info.version_str.clone(), + if binary_name.is_some() {
package_info.repo.clone(), desired_targets.len()
)); } else {
0
handles.extend( },
resolvers
.iter()
.cartesian_product(desired_targets.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:?}");
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()))
}),
); );
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:?}");
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())))
}),
)
};
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 { for (fetcher, handle) in handles {
fetcher.clone().report_to_upstream(); fetcher.clone().report_to_upstream();
match handle.flattened_join().await { match handle.flattened_join().await {

View file

@ -8,7 +8,9 @@ unset CARGO_INSTALL_ROOT
# to find versions matching <= 1.3.3 # to find versions matching <= 1.3.3
# - `cargo-quickinstall` would test `fetch_crate_cratesio_version_matched` ability # - `cargo-quickinstall` would test `fetch_crate_cratesio_version_matched` ability
# to find latest stable version. # to find latest stable version.
crates="b3sum@<=1.3.3 cargo-release@0.24.9 cargo-binstall@0.20.1 cargo-watch@8.4.0 miniserve@0.23.0 sccache@0.3.3 cargo-quickinstall jj-cli@0.18.0" # - `git-mob-tool tests the using of using a binary name (`git-mob`) different
# from the package name.
crates="b3sum@<=1.3.3 cargo-release@0.24.9 cargo-binstall@0.20.1 cargo-watch@8.4.0 miniserve@0.23.0 sccache@0.3.3 cargo-quickinstall jj-cli@0.18.0 git-mob-tool@1.6.1"
CARGO_HOME=$(mktemp -d 2>/dev/null || mktemp -d -t 'cargo-home') CARGO_HOME=$(mktemp -d 2>/dev/null || mktemp -d -t 'cargo-home')
export CARGO_HOME export CARGO_HOME
@ -59,3 +61,8 @@ jj_version="$(jj --version)"
echo "$jj_version" echo "$jj_version"
[ "$jj_version" = "jj 0.18.0-9fb5307b7886e390c02817af7c31b403f0279144" ] [ "$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" ]

View file

@ -1,3 +1,4 @@
[toolchain] [toolchain]
channel = "stable" channel = "stable"
profile = "minimal" profile = "minimal"
components = ["rustfmt", "clippy"]