Skip to content

Commit

Permalink
fix: use open_ambient_dir instead of fs::File::open to load folders (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelmmiguel authored May 25, 2023
1 parent 5631fb4 commit 40c6405
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
6 changes: 3 additions & 3 deletions crates/runtimes/src/modules/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{
fs,
path::{Path, PathBuf},
};
use wasmtime_wasi::{Dir, WasiCtxBuilder};
use wasmtime_wasi::{ambient_authority, Dir, WasiCtxBuilder};
use wws_runtimes_manager::metadata::Runtime as RuntimeMetadata;
use wws_store::Store;

Expand Down Expand Up @@ -90,10 +90,10 @@ impl Runtime for ExternalRuntime {
/// Mount the source code in the WASI context so it can be
/// processed by the engine
fn prepare_wasi_ctx(&self, builder: WasiCtxBuilder) -> Result<WasiCtxBuilder> {
let source = fs::File::open(&self.store.folder)?;
let dir = Dir::open_ambient_dir(&self.store.folder, ambient_authority())?;

Ok(builder
.preopened_dir(Dir::from_std_file(source), "/src")?
.preopened_dir(dir, "/src")?
.args(&self.metadata.args)?)
}

Expand Down
10 changes: 4 additions & 6 deletions crates/runtimes/src/modules/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

use crate::runtime::Runtime;
use anyhow::Result;
use std::path::Path;
use std::{fs, path::PathBuf};
use wasmtime_wasi::Dir;
use wasmtime_wasi::WasiCtxBuilder;
use std::path::{Path, PathBuf};
use wasmtime_wasi::{ambient_authority, Dir, WasiCtxBuilder};
use wws_store::Store;

static JS_ENGINE_WASM: &[u8] =
Expand Down Expand Up @@ -48,8 +46,8 @@ impl Runtime for JavaScriptRuntime {
/// Mount the source code in the WASI context so it can be
/// processed by the engine
fn prepare_wasi_ctx(&self, builder: WasiCtxBuilder) -> Result<WasiCtxBuilder> {
let source = fs::File::open(&self.store.folder)?;
Ok(builder.preopened_dir(Dir::from_std_file(source), "/src")?)
let dir = Dir::open_ambient_dir(&self.store.folder, ambient_authority())?;
Ok(builder.preopened_dir(dir, "/src")?)
}

/// Returns a reference to the Wasm module that should
Expand Down
7 changes: 3 additions & 4 deletions crates/worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::path::PathBuf;
use std::{collections::HashMap, path::Path};
use stdio::Stdio;
use wasmtime::{Engine, Linker, Module, Store};
use wasmtime_wasi::{Dir, WasiCtxBuilder};
use wasmtime_wasi::{ambient_authority, Dir, WasiCtxBuilder};
use wws_config::Config as ProjectConfig;
use wws_runtimes::{init_runtime, Runtime};

Expand Down Expand Up @@ -106,9 +106,8 @@ impl Worker {
if let Some(folders) = self.config.folders.as_ref() {
for folder in folders {
if let Some(base) = &self.path.parent() {
let source = fs::File::open(base.join(&folder.from))?;
wasi_builder =
wasi_builder.preopened_dir(Dir::from_std_file(source), &folder.to)?;
let dir = Dir::open_ambient_dir(base.join(&folder.from), ambient_authority())?;
wasi_builder = wasi_builder.preopened_dir(dir, &folder.to)?;
} else {
// TODO: Revisit error management on #73
return Err(anyhow!("The worker couldn't be initialized"));
Expand Down

0 comments on commit 40c6405

Please sign in to comment.