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

Refactor webc usage #5171

Merged
merged 11 commits into from
Oct 23, 2024
Merged
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
250 changes: 126 additions & 124 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,12 @@ version = "5.0.0-rc.1"

[workspace.dependencies]
# Repo-local crates
wasmer-package = { version = "0.1.0", path = "lib/package" }
wasmer-config = { path = "./lib/config" }
wasmer-wasix = { path = "./lib/wasix" }

# Wasmer-owned crates
webc = { version = "6.1.0", default-features = false, features = ["package"] }
webc = { version = "7.0.0-rc.1" }
shared-buffer = "0.1.4"

# Third-party crates
Expand Down
1 change: 1 addition & 0 deletions lib/backend-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ rust-version.workspace = true
[dependencies]
# Wasmer dependencies.
wasmer-config = { version = "0.9.0", path = "../config" }
wasmer-package.workspace = true
webc.workspace = true


Expand Down
6 changes: 4 additions & 2 deletions lib/backend-api/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use time::OffsetDateTime;
use tracing::Instrument;
use url::Url;
use wasmer_config::package::PackageIdent;
use wasmer_package::utils::from_bytes;
use webc::Container;

use crate::{
types::{self, *},
Expand Down Expand Up @@ -374,7 +376,7 @@ pub async fn fetch_webc_package(
client: &WasmerClient,
ident: &PackageIdent,
default_registry: &Url,
) -> Result<webc::compat::Container, anyhow::Error> {
) -> Result<Container, anyhow::Error> {
let url = match ident {
PackageIdent::Named(n) => Url::parse(&format!(
"{default_registry}/{}:{}",
Expand All @@ -398,7 +400,7 @@ pub async fn fetch_webc_package(
.bytes()
.await?;

webc::compat::Container::from_bytes(data).context("failed to parse webc package")
from_bytes(data).context("failed to parse webc package")
}

/// Fetch app templates.
Expand Down
2 changes: 1 addition & 1 deletion lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ wasmer-compiler-cranelift = { version = "=5.0.0-rc.1", path = "../compiler-crane
wasmer-compiler-singlepass = { version = "=5.0.0-rc.1", path = "../compiler-singlepass", optional = true }
wasmer-compiler-llvm = { version = "=5.0.0-rc.1", path = "../compiler-llvm", optional = true }
wasmer-emscripten = { version = "=5.0.0-rc.1", path = "../emscripten" }
wasmer-package = { version = "=0.1.0", path = "../package" }
wasmer-package.workspace = true

wasmer-vm = { version = "=5.0.0-rc.1", path = "../vm", optional = true }
wasmer-wasix = { path = "../wasix", version = "=0.29.0", features = [
Expand Down
14 changes: 7 additions & 7 deletions lib/cli/src/commands/create_exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use tar::Archive;
use wasmer::sys::Artifact;
use wasmer::*;
use wasmer_object::{emit_serialized, get_object_for_target};
use wasmer_package::utils::from_disk;
use wasmer_types::{compilation::symbols::ModuleMetadataSymbolRegistry, ModuleInfo};
use webc::{
compat::{Container, Volume as WebcVolume},
PathSegments,
};
use webc::Container;
use webc::PathSegments;
use webc::{Metadata, Volume as WebcVolume};

use self::utils::normalize_atom_name;
use crate::{
Expand Down Expand Up @@ -245,7 +245,7 @@ impl CreateExe {
};
std::fs::create_dir_all(&tempdir)?;

let atoms = if let Ok(pirita) = Container::from_disk(&input_path) {
let atoms = if let Ok(pirita) = from_disk(&input_path) {
// pirita file
compile_pirita_into_directory(
&pirita,
Expand Down Expand Up @@ -516,14 +516,14 @@ fn serialize_volume_to_webc_v1(volume: &WebcVolume) -> Vec<u8> {
path.push(segment);

match meta {
webc::compat::Metadata::Dir { .. } => {
Metadata::Dir { .. } => {
files.insert(
webc::v1::DirOrFile::Dir(path.to_string().into()),
Vec::new(),
);
read_dir(volume, path, files);
}
webc::compat::Metadata::File { .. } => {
Metadata::File { .. } => {
if let Some((contents, _)) = volume.read_file(&*path) {
files.insert(
webc::v1::DirOrFile::File(path.to_string().into()),
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/src/commands/create_obj.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{env, path::PathBuf};
use anyhow::{Context, Result};
use clap::Parser;
use wasmer::*;
use wasmer_package::utils::from_disk;

use crate::store::CompilerOptions;

Expand Down Expand Up @@ -83,7 +84,7 @@ impl CreateObj {
println!("Compiler: {}", compiler_type);
println!("Target: {}", target.triple());

let atoms = if let Ok(webc) = webc::compat::Container::from_disk(&input_path) {
let atoms = if let Ok(webc) = from_disk(&input_path) {
crate::commands::create_exe::compile_pirita_into_directory(
&webc,
&output_directory_path,
Expand Down
11 changes: 8 additions & 3 deletions lib/cli/src/commands/gen_c_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ use anyhow::{Context, Error};
use bytes::Bytes;
use clap::Parser;
use wasmer_compiler::Artifact;
use wasmer_package::package::WasmerPackageError;
use wasmer_package::utils::from_bytes;
use wasmer_types::{
compilation::symbols::ModuleMetadataSymbolRegistry, CpuFeature, MetadataHeader, Triple,
};
use webc::{compat::SharedBytes, Container, DetectError};
use webc::{compat::SharedBytes, DetectError};
use webc::{Container, ContainerError};

use crate::store::CompilerOptions;

Expand Down Expand Up @@ -60,9 +63,11 @@ impl GenCHeader {
None => crate::commands::PrefixMapCompilation::hash_for_bytes(&file),
};

let atom = match Container::from_bytes(file.clone()) {
let atom = match from_bytes(file.clone()) {
Ok(webc) => self.get_atom(&webc)?,
Err(webc::compat::ContainerError::Detect(DetectError::InvalidMagic { .. })) => {
Err(WasmerPackageError::ContainerError(ContainerError::Detect(
DetectError::InvalidMagic { .. },
))) => {
// we've probably got a WebAssembly file
file.into()
}
Expand Down
16 changes: 8 additions & 8 deletions lib/cli/src/commands/package/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use dialoguer::console::{style, Emoji};
use indicatif::ProgressBar;
use sha2::Digest;
use wasmer_config::package::PackageHash;
use webc::wasmer_package::Package;
use wasmer_package::package::Package;

use crate::utils::load_package_manifest;

Expand Down Expand Up @@ -54,12 +54,10 @@ impl PackageBuild {
manifest_path.display()
)
};
let pkg = webc::wasmer_package::Package::from_manifest(manifest_path.clone()).context(
format!(
"While parsing the manifest (loaded from {})",
manifest_path.canonicalize()?.display()
),
)?;
let pkg = Package::from_manifest(manifest_path.clone()).context(format!(
"While parsing the manifest (loaded from {})",
manifest_path.canonicalize()?.display()
))?;
let data = pkg.serialize().context("While validating the package")?;
let hash = sha2::Sha256::digest(&data).into();
let pkg_hash = PackageHash::from_sha256_bytes(hash);
Expand Down Expand Up @@ -181,6 +179,8 @@ impl PackageBuild {

#[cfg(test)]
mod tests {
use wasmer_package::utils::from_disk;

use super::*;

/// Download a package from the dev registry.
Expand Down Expand Up @@ -215,6 +215,6 @@ description = "hello"

cmd.execute().unwrap();

webc::Container::from_disk(path.join("wasmer-hello-0.1.0.webc")).unwrap();
from_disk(path.join("wasmer-hello-0.1.0.webc")).unwrap();
}
}
2 changes: 1 addition & 1 deletion lib/cli/src/commands/package/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::{
};
use wasmer_api::WasmerClient;
use wasmer_config::package::{Manifest, NamedPackageIdent, PackageHash};
use webc::wasmer_package::Package;
use wasmer_package::package::Package;

pub mod macros;
pub mod wait;
Expand Down
5 changes: 3 additions & 2 deletions lib/cli/src/commands/package/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use dialoguer::console::{style, Emoji};
use indicatif::{ProgressBar, ProgressStyle};
use tempfile::NamedTempFile;
use wasmer_config::package::{PackageIdent, PackageSource};
use wasmer_package::utils::from_disk;

use crate::config::WasmerEnv;

Expand Down Expand Up @@ -243,7 +244,7 @@ impl PackageDownload {

step_num += 1;

webc::compat::Container::from_disk(tmpfile.path())
from_disk(tmpfile.path())
.context("could not parse downloaded file as a package - invalid download?")?;
}

Expand Down Expand Up @@ -302,6 +303,6 @@ mod tests {

cmd.execute().unwrap();

webc::compat::Container::from_disk(out_path).unwrap();
from_disk(out_path).unwrap();
}
}
2 changes: 1 addition & 1 deletion lib/cli/src/commands/package/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use is_terminal::IsTerminal;
use std::path::{Path, PathBuf};
use wasmer_api::WasmerClient;
use wasmer_config::package::{Manifest, PackageHash};
use webc::wasmer_package::Package;
use wasmer_package::package::Package;

/// Push a package to the registry.
///
Expand Down
3 changes: 2 additions & 1 deletion lib/cli/src/commands/package/unpack.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 wasmer_package::utils::from_disk;

/// Extract contents of a webc image to a directory.
///
Expand Down Expand Up @@ -67,7 +68,7 @@ impl PackageUnpack {
PACKAGE_EMOJI
));

let pkg = webc::compat::Container::from_disk(&self.package_path).with_context(|| {
let pkg = from_disk(&self.package_path).with_context(|| {
format!(
"could not open package at '{}'",
self.package_path.display()
Expand Down
6 changes: 4 additions & 2 deletions lib/cli/src/commands/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use wasmer::{
#[cfg(feature = "compiler")]
use wasmer_compiler::ArtifactBuild;
use wasmer_config::package::PackageSource as PackageSpecifier;
use wasmer_package::utils::from_disk;
use wasmer_registry::{wasmer_env::WasmerEnv, Package};
use wasmer_types::ModuleHash;
#[cfg(feature = "journal")]
Expand All @@ -53,7 +54,8 @@ use wasmer_wasix::{
},
Runtime, WasiError,
};
use webc::{metadata::Manifest, Container};
use webc::metadata::Manifest;
use webc::Container;

use crate::{
commands::run::wasi::Wasi, common::HashAlgorithm, error::PrettyError, logging::Output,
Expand Down Expand Up @@ -761,7 +763,7 @@ impl ExecutableTarget {
})
}
TargetOnDisk::LocalWebc => {
let container = Container::from_disk(path)?;
let container = from_disk(path)?;
pb.set_message("Resolving dependencies");

let inner_runtime = runtime.clone();
Expand Down
18 changes: 18 additions & 0 deletions lib/package/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,25 @@ rust-version.workspace = true
webc.workspace = true
wasmer-config = { version = "0.9.0", path = "../config" }
toml = "0.8.0"
bytes = "1.8.0"
sha2 = "0.10.8"
shared-buffer.workspace = true
serde_json = "1.0.132"
anyhow = "1.0.90"
thiserror = "1.0.64"
cfg-if = "1.0.0"
ciborium = "0.2.2"
semver = "1.0.23"
url = "2.5.2"
serde = "1.0.211"
insta = { version = "1", features = ["filters", "yaml"] }
flate2 = "1.0.34"
tar = "0.4.42"
tempfile = "3.12.0"

[dev-dependencies]
pretty_assertions.workspace = true
tempfile = "3.12.0"
regex = "1.11.0"
ureq = "2.10.1"
hexdump = "0.1.2"
14 changes: 7 additions & 7 deletions lib/package/src/convert/webc_to_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ use std::path::Path;

use wasmer_config::package::ModuleReference;

use webc::Container;

use super::ConversionError;

/// Convert a webc image into a directory with a wasmer.toml file that can
/// be used for generating a new pacakge.
pub fn webc_to_package_dir(
webc: &webc::Container,
target_dir: &Path,
) -> Result<(), ConversionError> {
pub fn webc_to_package_dir(webc: &Container, target_dir: &Path) -> Result<(), ConversionError> {
let mut pkg_manifest = wasmer_config::package::Manifest::new_empty();

let manifest = webc.manifest();
Expand Down Expand Up @@ -227,6 +226,8 @@ mod tests {

use pretty_assertions::assert_eq;

use crate::{package::Package, utils::from_bytes};

use super::*;

// Build a webc from a pacakge directory, and then restore the directory
Expand Down Expand Up @@ -277,10 +278,9 @@ main-args = ["/mounted/script.py"]
)
.unwrap();

let pkg = webc::wasmer_package::Package::from_manifest(dir_input.join("wasmer.toml"))
.unwrap();
let pkg = Package::from_manifest(dir_input.join("wasmer.toml")).unwrap();
let raw = pkg.serialize().unwrap();
webc::Container::from_bytes(raw).unwrap()
from_bytes(raw).unwrap()
};

let dir_output = dir.join("output");
Expand Down
6 changes: 6 additions & 0 deletions lib/package/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
#[macro_use]
#[cfg(test)]
mod macros;

pub mod convert;
pub mod package;
pub mod utils;
Loading
Loading