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

Handle local package resolution #4638

Merged
merged 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ wasmer-config = { path = "./lib/config" }
wasmer-wasix = { path = "./lib/wasix" }

# Wasmer-owned crates
webc = { version = "6.0.0-alpha5", default-features = false, features = ["package"] }
webc = { version = "6.0.0-alpha6", default-features = false, features = ["package"] }
edge-schema = { version = "=0.1.0" }
shared-buffer = "0.1.4"

Expand Down
8 changes: 5 additions & 3 deletions lib/cli/src/commands/package/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::path::PathBuf;
use anyhow::Context;
use dialoguer::console::{style, Emoji};
use indicatif::ProgressBar;
use sha2::Digest;
use wasmer_config::package::PackageHash;

use crate::utils::load_package_manifest;
Expand Down Expand Up @@ -53,7 +54,10 @@ impl PackageBuild {
)
};
let pkg = webc::wasmer_package::Package::from_manifest(manifest_path)?;
let pkg_hash = PackageHash::from_sha256_bytes(pkg.webc_hash());
let data = pkg.serialize()?;
let hash = sha2::Sha256::digest(&data).into();

let pkg_hash = PackageHash::from_sha256_bytes(hash);
theduke marked this conversation as resolved.
Show resolved Hide resolved
let name = if let Some(manifest_pkg) = manifest.package {
if let Some(name) = manifest_pkg.name {
if let Some(version) = manifest_pkg.version {
Expand Down Expand Up @@ -116,8 +120,6 @@ impl PackageBuild {
);
}

let data = pkg.serialize()?;

pb.println(format!(
"{} {}Writing package...",
style("[3/3]").bold().dim(),
Expand Down
11 changes: 4 additions & 7 deletions lib/cli/src/commands/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,15 +673,12 @@ impl ExecutableTarget {
pb: &ProgressBar,
) -> Result<Self, Error> {
pb.set_message(format!("Loading \"{}\" into memory", dir.display()));

let manifest_path = dir.join("wasmer.toml");
let webc = webc::wasmer_package::Package::from_manifest(manifest_path)?;
let container = Container::from(webc);

pb.set_message("Resolving dependencies");
let inner_runtime = runtime.clone();
let pkg = runtime.task_manager().spawn_and_block_on(async move {
BinaryPackage::from_webc(&container, inner_runtime.as_ref()).await
let pkg = runtime.task_manager().spawn_and_block_on({
let path = dir.to_path_buf();

async move { BinaryPackage::from_dir(&path, inner_runtime.as_ref()).await }
})??;

Ok(ExecutableTarget::Package(pkg))
Expand Down
37 changes: 35 additions & 2 deletions lib/wasix/src/bin_factory/binary_package.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::sync::Arc;
use std::{path::Path, sync::Arc};

use anyhow::Context;
use derivative::*;
use once_cell::sync::OnceCell;
use sha2::Digest;
use virtual_fs::FileSystem;
use wasmer_config::package::{PackageHash, PackageId, PackageSource};
use webc::{compat::SharedBytes, Container};
Expand Down Expand Up @@ -79,6 +80,36 @@ pub struct BinaryPackage {
}

impl BinaryPackage {
#[tracing::instrument(level = "debug", skip_all)]
pub async fn from_dir(
dir: &Path,
rt: &(dyn Runtime + Send + Sync),
) -> Result<Self, anyhow::Error> {
let source = rt.source();

// since each package must be in its own directory, hash of the `dir` should provide a good enough
// unique identifier for the package
let hash = sha2::Sha256::digest(dir.display().to_string().as_bytes()).into();
let id = PackageId::Hash(PackageHash::from_sha256_bytes(hash));
theduke marked this conversation as resolved.
Show resolved Hide resolved

let manifest_path = dir.join("wasmer.toml");
let webc = webc::wasmer_package::Package::from_manifest(manifest_path)?;
let container = Container::from(webc);
let manifest = container.manifest();

let root = PackageInfo::from_manifest(id, manifest, container.version())?;
let root_id = root.id.clone();

let resolution = crate::runtime::resolver::resolve(&root_id, &root, &*source).await?;
let pkg = rt
.package_loader()
.load_package_tree(&container, &resolution)
.await
.map_err(|e| anyhow::anyhow!(e))?;

Ok(pkg)
}

/// Load a [`webc::Container`] and all its dependencies into a
/// [`BinaryPackage`].
#[tracing::instrument(level = "debug", skip_all)]
Expand All @@ -90,7 +121,9 @@ impl BinaryPackage {

let manifest = container.manifest();
let id = PackageInfo::package_id_from_manifest(manifest)?.unwrap_or_else(|| {
PackageId::Hash(PackageHash::from_sha256_bytes(container.webc_hash()))
PackageId::Hash(PackageHash::from_sha256_bytes(
container.webc_hash().unwrap(),
))
maminrayej marked this conversation as resolved.
Show resolved Hide resolved
});

let root = PackageInfo::from_manifest(id, manifest, container.version())?;
Expand Down
Loading