Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the ability to force preloaded packages to be latest #3871

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/cli/src/commands/run/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ impl Wasi {
for path in &self.include_webcs {
let pkg = preload_webc(path)
.with_context(|| format!("Unable to load \"{}\"", path.display()))?;
resolver.add_preload(pkg);
resolver.add_preload(pkg, false);
}

Ok(resolver.with_cache())
Expand Down
33 changes: 26 additions & 7 deletions lib/wasi/src/runtime/resolver/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,25 @@ use crate::{
runtime::resolver::{types::ResolverError, types::WebcIdentifier, PackageResolver},
};

#[derive(Debug)]
struct PreloadedPackage {
package: BinaryPackage,
override_latest: bool,
}

/// A [`PackageResolver`] that will resolve packages by fetching them from the
/// WAPM registry.
///
/// Any downloaded assets will be cached on disk.
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct RegistryResolver {
cache_dir: PathBuf,
registry_endpoint: Url,
/// A list of [`BinaryPackage`]s that have already been loaded into memory
/// by the user.
// TODO: Remove this "preload" hack and update the snapshot tests to
// use a local registry instead of "--include-webc"
preloaded: Vec<BinaryPackage>,
preloaded: Vec<PreloadedPackage>,
}

impl RegistryResolver {
Expand Down Expand Up @@ -65,15 +71,28 @@ impl RegistryResolver {
///
/// **This mechanism should only be used for testing**. Expect it to be
/// removed in future versions in favour of a local registry.
pub fn add_preload(&mut self, pkg: BinaryPackage) -> &mut Self {
self.preloaded.push(pkg);
///
/// Specify `override_latest` if this preloaded package should always be
/// returned when the latest version is requested.
pub fn add_preload(&mut self, pkg: BinaryPackage, override_latest: bool) -> &mut Self {
self.preloaded.push(PreloadedPackage {
package: pkg,
override_latest,
});
self
}

fn lookup_preloaded(&self, pkg: &WebcIdentifier) -> Option<&BinaryPackage> {
self.preloaded.iter().find(|candidate| {
candidate.package_name == pkg.full_name && pkg.version.matches(&candidate.version)
})
self.preloaded
.iter()
.find(|candidate| {
if candidate.package.package_name == pkg.full_name {
candidate.override_latest || pkg.version.matches(&candidate.package.version)
} else {
false
}
})
.map(|c| &c.package)
}
}

Expand Down