Skip to content

Commit

Permalink
Merge #3370
Browse files Browse the repository at this point in the history
3370: Fix wasmer run not interpreting URLs correctly + display fixes r=fschutt a=fschutt

Fixes #3366.
Fixes #3346.

Should land in the 3.0.2 release.

Co-authored-by: Felix Schütt <[email protected]>
Co-authored-by: Felix Schütt <[email protected]>
  • Loading branch information
3 people authored Nov 25, 2022
2 parents e0707e9 + 83a3854 commit 316d435
Show file tree
Hide file tree
Showing 8 changed files with 207 additions and 83 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/test-sys.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,19 @@ jobs:
CARGO_TARGET: --target ${{ matrix.target }}
WAPM_DEV_TOKEN: ${{ secrets.WAPM_DEV_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Test integration CLI
if: matrix.run_test && matrix.os == 'windows-2019'
shell: bash
run: |
make build-wasmer &&
cargo test --package wasmer-integration-tests-cli --test run -- test_wasmer_run_complex_url --exact --nocapture
env:
TARGET: ${{ matrix.target }}
TARGET_DIR: target/${{ matrix.target }}/release
CARGO_TARGET: --target x86_64-pc-windows-msvc
WAPM_DEV_TOKEN: ${{ secrets.WAPM_DEV_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# cargo test --package wasmer-integration-tests-cli --test run -- test_wasmer_run_complex_url --exact --nocapture
#- name: Test integration CLI
# if: matrix.run_test && matrix.os == 'windows-2019'
# shell: bash
Expand Down
76 changes: 40 additions & 36 deletions Cargo.lock

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

8 changes: 7 additions & 1 deletion lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ wasmer-vfs = { version = "=3.0.1", path = "../vfs", default-features = false, f
atty = "0.2"
colored = "2.0"
anyhow = "1.0"
spinner = "0.5.0"
spinoff = "0.5.4"
clap = { version = "3.2.22", features = ["derive", "env"] }
# For the function names autosuggestion
distance = "0.4"
Expand Down Expand Up @@ -166,6 +166,12 @@ http = [
"serde",
]

[target.'cfg(target_os = "windows")'.dependencies]
colored = "2.0.0"

[package.metadata.binstall]
pkg-fmt = "tgz"

[package.metadata.binstall.overrides.aarch64-apple-darwin]
pkg-url = "{ repo }/releases/download/v{ version }/wasmer-darwin-arm64.{ archive-format }"
bin-dir = "bin/{ bin }"
Expand Down
69 changes: 43 additions & 26 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,17 +645,20 @@ impl Run {
}
}

fn start_spinner(msg: String) -> Option<spinner::SpinnerHandle> {
fn start_spinner(msg: String) -> Option<spinoff::Spinner> {
if !isatty::stdout_isatty() {
return None;
}
Some(
spinner::SpinnerBuilder::new(msg)
.spinner(vec![
"⣾", "⣽", "⣻", "⢿", "⡿", "⣟", "⣯", "⣷", " ", "⠁", "⠂", "⠄", "⡀", "⢀", "⠠", "⠐", "⠈",
])
.start(),
)
#[cfg(target_os = "windows")]
{
use colored::control;
let _ = control::set_virtual_terminal(true);
}
Some(spinoff::Spinner::new(
spinoff::Spinners::Dots,
msg,
spinoff::Color::White,
))
}

/// Before looking up a command from the registry, try to see if we have
Expand Down Expand Up @@ -706,8 +709,7 @@ pub(crate) fn try_autoinstall_package(
force_install,
);
if let Some(sp) = sp.take() {
sp.close();
print!("\r");
sp.clear();
}
let _ = std::io::stdout().flush();
let (_, package_dir) = match result {
Expand Down Expand Up @@ -765,8 +767,8 @@ fn try_lookup_command(sv: &mut SplitVersion) -> Result<PackageDownloadInfo, anyh

for registry in wasmer_registry::get_all_available_registries().unwrap_or_default() {
let result = wasmer_registry::query_command_from_registry(&registry, &sv.package);
if sp.is_some() {
print!("\r");
if let Some(s) = sp.take() {
s.clear();
}
let _ = std::io::stdout().flush();
let command = sv.package.clone();
Expand All @@ -779,8 +781,7 @@ fn try_lookup_command(sv: &mut SplitVersion) -> Result<PackageDownloadInfo, anyh
}

if let Some(sp) = sp.take() {
sp.close();
print!("\r");
sp.clear();
}
let _ = std::io::stdout().flush();
Err(anyhow::anyhow!("command {sv} not found"))
Expand Down Expand Up @@ -828,11 +829,6 @@ pub(crate) fn try_run_package_or_file(
) -> Result<(), anyhow::Error> {
let debug_msgs_allowed = isatty::stdout_isatty();

if let Ok(url) = url::Url::parse(&format!("{}", r.path.display())) {
let result = try_run_url(&url, args, r, debug);
return result;
}

// Check "r.path" is a file or a package / command name
if r.path.exists() {
if r.path.is_dir() && r.path.join("wapm.toml").exists() {
Expand All @@ -848,6 +844,18 @@ pub(crate) fn try_run_package_or_file(
return r.execute();
}

// c:// might be parsed as a URL on Windows
let url_string = format!("{}", r.path.display());
if let Ok(url) = url::Url::parse(&url_string) {
if url.scheme() == "http" || url.scheme() == "https" {
match try_run_url(&url, args, r, debug) {
Err(ExecuteLocalPackageError::BeforeExec(_)) => {}
Err(ExecuteLocalPackageError::DuringExec(e)) => return Err(e),
Ok(o) => return Ok(o),
}
}
}

let package = format!("{}", r.path.display());

let mut is_fake_sv = false;
Expand Down Expand Up @@ -915,9 +923,15 @@ pub(crate) fn try_run_package_or_file(
try_autoinstall_package(args, &sv, package_download_info, r.force_install)
}

fn try_run_url(url: &Url, _args: &[String], r: &Run, _debug: bool) -> Result<(), anyhow::Error> {
let checksum = wasmer_registry::get_remote_webc_checksum(url)
.map_err(|e| anyhow::anyhow!("error fetching {url}: {e}"))?;
fn try_run_url(
url: &Url,
_args: &[String],
r: &Run,
_debug: bool,
) -> Result<(), ExecuteLocalPackageError> {
let checksum = wasmer_registry::get_remote_webc_checksum(url).map_err(|e| {
ExecuteLocalPackageError::BeforeExec(anyhow::anyhow!("error fetching {url}: {e}"))
})?;

let packages = wasmer_registry::get_all_installed_webc_packages();

Expand All @@ -926,20 +940,23 @@ fn try_run_url(url: &Url, _args: &[String], r: &Run, _debug: bool) -> Result<(),

let result = wasmer_registry::install_webc_package(url, &checksum);

result.map_err(|e| anyhow::anyhow!("error fetching {url}: {e}"))?;
result.map_err(|e| {
ExecuteLocalPackageError::BeforeExec(anyhow::anyhow!("error fetching {url}: {e}"))
})?;

if let Some(sp) = sp {
sp.close();
sp.clear();
}
}

let webc_dir = wasmer_registry::get_webc_dir();

let webc_install_path = webc_dir
.context("Error installing package: no webc dir")?
.context("Error installing package: no webc dir")
.map_err(ExecuteLocalPackageError::BeforeExec)?
.join(checksum);

let mut r = r.clone();
r.path = webc_install_path;
r.execute()
r.execute().map_err(ExecuteLocalPackageError::DuringExec)
}
5 changes: 3 additions & 2 deletions lib/registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ pub fn query_package_from_registry(

let v = response.package_version.as_ref().ok_or_else(|| {
QueryPackageError::ErrorSendingQuery(format!(
"Invalid response for crate {name:?}: no manifest"
"Invalid response for crate {name:?}: no package version: {response:#?}"
))
})?;

Expand Down Expand Up @@ -1136,7 +1136,8 @@ pub fn get_checksum_hash(bytes: &[u8]) -> String {
/// file is already installed before downloading it
pub fn get_remote_webc_checksum(url: &Url) -> Result<String, anyhow::Error> {
let request_max_bytes = webc::WebC::get_signature_offset_start() + 4 + 1024 + 8 + 8;
let data = get_webc_bytes(url, Some(0..request_max_bytes)).context("get_webc_bytes failed")?;
let data = get_webc_bytes(url, Some(0..request_max_bytes))
.with_context(|| format!("get_webc_bytes failed on {url}"))?;
let checksum = webc::WebC::get_checksum_bytes(&data)
.map_err(|e| anyhow::anyhow!("{e}"))
.context("get_checksum_bytes failed")?
Expand Down
1 change: 1 addition & 0 deletions tests/integration/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ target-lexicon = "0.12.4"
[dependencies]
anyhow = "1"
tempfile = "3"
target-lexicon = "0.12.5"

[features]
default = ["webc_runner"]
Expand Down
Loading

0 comments on commit 316d435

Please sign in to comment.