Open RobbieMcKinstry opened 2 weeks ago
Thanks for the detailed report!.
Indeed I do this in our original C based wasm language module but must have missed it wasn't being done in the rust one.
This will also get environment variables set in the config file. IIRC they are read and set by the "prototype" process before the "application" process(es) are fork(2)ed.
IIRC they are read and set by the "prototype" process before the "application" process(es) are fork(2)ed.
Awesome, that makes sense to me! The presence of the "prototype" process clearly up my concern that the values wouldn't be copied from the environment
config setting. šš»
Thank you for your hard work on this lovely project!
So I'm trying this out with the below patch
diff --git ./src/wasm-wasi-component/src/lib.rs ./src/wasm-wasi-component/src/lib.rs
index b0552e81..0df33914 100644
--- ./src/wasm-wasi-component/src/lib.rs
+++ ./src/wasm-wasi-component/src/lib.rs
@@ -256,6 +256,7 @@ impl GlobalState {
// shouldn't get raw access to stdout/stderr.
cx.inherit_stdout();
cx.inherit_stderr();
+ cx.inherit_env();
for dir in self.global_config.dirs.iter() {
let fd = Dir::open_ambient_dir(dir, ambient_authority())
.with_context(|| {
But the wasm-wasi-component language module doesn't build with it...
$ make wasm-wasi-component install
cargo build --release --manifest-path src/wasm-wasi-component/Cargo.toml
Compiling wasm-wasi-component v0.1.0 (/home/andrew/src/unit/src/wasm-wasi-component)
error[E0599]: no method named `inherit_env` found for struct `wasmtime_wasi::preview2::WasiCtxBuilder` in the current scope
--> src/lib.rs:259:20
|
259 | cx.inherit_env();
| ^^^^^^^^^^^ method not found in `WasiCtxBuilder`
For more information about this error, try `rustc --explain E0599`.
error: could not compile `wasm-wasi-component` (lib) due to 1 previous error
make: *** [build/Makefile:2151: src/wasm-wasi-component/target/release/libwasm_wasi_component.so] Error 101
Yet according to the docs (as you pointed to) it is a thing!
I can add an environment variable via cx.env();
, but I don't know why it can't find .inherit_env()
Pinging someone who knows rust...
Cc: @avahahn
inherit_env
was added to the wasmtime-wasi crate in version 20.0.0; we're still locked to 17.0.0.
This is reason enough to go ahead and work on updating our Rust deps now.
About
After making small changes to the application described in this official blog post, I discovered that environment variables were not being propagated to the application, either from the parent process' environment or variables specifically defined in Unit configuration.
Suspected Cause
At a high level, I believe the issue is in the Wasm host. Wasm environment contexts start empty and have to explicitly opt-in to inheriting environment variables.
Specifically, these lines of code, reproduced below, seem to illustrate the issue:
As you can see, the
WasiCxtBuilder
explicitly opts-in to inheritingstdout
and stderr`, but it does not opt-in to inheriting environment variables. This API should be used to inherit env vars:I'm not sure if that's enough to inherit variables defined according to the
environment
configuration, which I found does not work. I would expect that duringsetup
, the application configuration provided here would inject theenvironment
section of the config file, and environment variable would have to be copied out of that config struct and into theWasiCtx
here.It looks like that's what's done later in the same method with the
dirs
field, stored inGlobalConfig
, which is used to provide filesystem access to the Wasm component which would be otherwise unable to read from the filesystem.Code Sample
The following code sample adapts the application presented in the aforementioned blog post. The output demonstrates that no environment variables were observed. To reproduce, initialize the repository as described in the blog post, then alter the
config.js
file andsrc/lib.rs
as described below. When you visit the website, the webpage will demonstrate there aren't any environmental variables at all, neither those set by default by your system, not those defined in the Unit'senvironment
config section.From
config.js
:From
src/lib.rs
:Thank you very much for your time and attention.