Skip to content

Commit

Permalink
handle local package resolution (#4638)
Browse files Browse the repository at this point in the history
  • Loading branch information
maminrayej authored May 7, 2024
2 parents cc6daca + f5c5e42 commit 387e2c3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 23 deletions.
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);
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
56 changes: 46 additions & 10 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));

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 @@ -89,9 +120,13 @@ impl BinaryPackage {
let source = rt.source();

let manifest = container.manifest();
let id = PackageInfo::package_id_from_manifest(manifest)?.unwrap_or_else(|| {
PackageId::Hash(PackageHash::from_sha256_bytes(container.webc_hash()))
});
let id = PackageInfo::package_id_from_manifest(manifest)?
.or_else(|| {
container
.webc_hash()
.map(|hash| PackageId::Hash(PackageHash::from_sha256_bytes(hash)))
})
.ok_or_else(|| anyhow::Error::msg("webc file did not provide its hash"))?;

let root = PackageInfo::from_manifest(id, manifest, container.version())?;
let root_id = root.id.clone();
Expand Down Expand Up @@ -204,17 +239,16 @@ mod tests {
std::fs::create_dir_all(&out).unwrap();
let file_txt = "Hello, World!";
std::fs::write(out.join("file.txt"), file_txt).unwrap();
let webc: Container = webc::wasmer_package::Package::from_manifest(manifest)
.unwrap()
.into();
let tasks = task_manager();
let mut runtime = PluggableRuntime::new(tasks);
runtime.set_package_loader(
BuiltinPackageLoader::new()
.with_shared_http_client(runtime.http_client().unwrap().clone()),
);

let pkg = BinaryPackage::from_webc(&webc, &runtime).await.unwrap();
let pkg = BinaryPackage::from_dir(&temp.path(), &runtime)
.await
.unwrap();

// We should have mapped "./out/file.txt" on the host to
// "/public/file.txt" on the guest.
Expand Down Expand Up @@ -257,7 +291,7 @@ mod tests {
let atom_path = temp.path().join("foo.wasm");
std::fs::write(&atom_path, b"").unwrap();

let webc: Container = webc::wasmer_package::Package::from_manifest(manifest)
let webc: Container = webc::wasmer_package::Package::from_manifest(&manifest)
.unwrap()
.into();

Expand All @@ -268,7 +302,9 @@ mod tests {
.with_shared_http_client(runtime.http_client().unwrap().clone()),
);

let pkg = BinaryPackage::from_webc(&webc, &runtime).await.unwrap();
let pkg = BinaryPackage::from_dir(&temp.path(), &runtime)
.await
.unwrap();

assert_eq!(pkg.commands.len(), 1);
let command = pkg.get_command("cmd").unwrap();
Expand Down

0 comments on commit 387e2c3

Please sign in to comment.