From 802ccc6d809ac6659d789573fb9cf1e19be820c3 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 22 Jan 2021 13:50:20 +0100 Subject: [PATCH 1/5] feat(cli) Improve environment variables parsing. The following environment variables are considered incorrect: * `A`, no equal sign, * `A=`, value is missing, * `=A`, key is missing. The following environment variables are considered correct: * `A=B` with `A` for the name and `B` for the value, * `A=B=C` with `A` for the name and `B=C` for the value. --- lib/cli/src/utils.rs | 48 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/lib/cli/src/utils.rs b/lib/cli/src/utils.rs index dfc693e91c1..4eaba96ed17 100644 --- a/lib/cli/src/utils.rs +++ b/lib/cli/src/utils.rs @@ -40,14 +40,50 @@ pub fn parse_mapdir(entry: &str) -> Result<(String, PathBuf)> { } } -/// Parses a mapdir from an env var +/// Parses an environment variable. pub fn parse_envvar(entry: &str) -> Result<(String, String)> { - if let [env_var, value] = entry.split('=').collect::>()[..] { - Ok((env_var.to_string(), value.to_string())) - } else { - bail!( - "Env vars must be of the form =. Found {}", + match entry.find('=') { + None => bail!( + "Environment variable must be of the form `=`; found `{}`", + &entry + ), + + Some(0) => bail!( + "Environment variable is not well formed, the `name` is missing in `=`; got `{}`", &entry + ), + + Some(position) if position == entry.len() - 1 => bail!( + "Environment variable is not well formed, the `value` is missing in `=`; got `{}`", + &entry + ), + + Some(position) => dbg!(Ok((entry[..position].into(), entry[position + 1..].into()))), + } +} + +#[cfg(test)] +mod tests { + use super::parse_envvar; + + #[test] + fn test_parse_envvar() { + assert_eq!( + parse_envvar("A").unwrap_err().to_string(), + "Environment variable must be of the form `=`; found `A`" + ); + assert_eq!( + parse_envvar("=A").unwrap_err().to_string(), + "Environment variable is not well formed, the `name` is missing in `=`; got `=A`" + ); + assert_eq!( + parse_envvar("A=").unwrap_err().to_string(), + "Environment variable is not well formed, the `value` is missing in `=`; got `A=`" + ); + assert_eq!(parse_envvar("A=B").unwrap(), ("A".into(), "B".into())); + assert_eq!( + parse_envvar("A=B=C=D").unwrap(), + ("A".into(), "B=C=D".into()) ); } } From ae7f9c28c1ef8f62854cd97f7628f48d67a49e59 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 22 Jan 2021 13:56:08 +0100 Subject: [PATCH 2/5] doc(changelog) Add #2042. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 637b76e1945..7e7e71e2d57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ - [#2026](https://github.com/wasmerio/wasmer/pull/2010) Expose trap code of a `RuntimeError`, if it's a `Trap`. ### Changed +- [#2042](https://github.com/wasmerio/wasmer/pull/2042) Parse more exotic environment variables in `wasmer run`. - [#2041](https://github.com/wasmerio/wasmer/pull/2041) Documentation diagrams now have a solid white background rather than a transparent background. ### Fixed From 0bf86c8d01babeee34cf05244afe4c693225e724 Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 22 Jan 2021 13:59:30 +0100 Subject: [PATCH 3/5] feat(cli) Trim environment variables before parsing them. --- lib/cli/src/utils.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/cli/src/utils.rs b/lib/cli/src/utils.rs index 4eaba96ed17..a032f26f40b 100644 --- a/lib/cli/src/utils.rs +++ b/lib/cli/src/utils.rs @@ -42,6 +42,8 @@ pub fn parse_mapdir(entry: &str) -> Result<(String, PathBuf)> { /// Parses an environment variable. pub fn parse_envvar(entry: &str) -> Result<(String, String)> { + let entry = entry.trim(); + match entry.find('=') { None => bail!( "Environment variable must be of the form `=`; found `{}`", @@ -58,7 +60,7 @@ pub fn parse_envvar(entry: &str) -> Result<(String, String)> { &entry ), - Some(position) => dbg!(Ok((entry[..position].into(), entry[position + 1..].into()))), + Some(position) => Ok((entry[..position].into(), entry[position + 1..].into())), } } @@ -81,6 +83,7 @@ mod tests { "Environment variable is not well formed, the `value` is missing in `=`; got `A=`" ); assert_eq!(parse_envvar("A=B").unwrap(), ("A".into(), "B".into())); + assert_eq!(parse_envvar(" A=B\t").unwrap(), ("A".into(), "B".into())); assert_eq!( parse_envvar("A=B=C=D").unwrap(), ("A".into(), "B=C=D".into()) From 70858eb201e03264742a5a759b219a58c90158bb Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 22 Jan 2021 14:22:28 +0100 Subject: [PATCH 4/5] feat(c-api) Do not build C headers if the env var `DOCS_RS` exists. --- lib/c-api/build.rs | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/c-api/build.rs b/lib/c-api/build.rs index db1b0ae0858..7982deb76eb 100644 --- a/lib/c-api/build.rs +++ b/lib/c-api/build.rs @@ -1,3 +1,8 @@ +//! This build script aims at: +//! +//! * generating the C header files for the C API, +//! * setting `inline-c` up. + use cbindgen::{Builder, Language}; use std::{env, fs, path::PathBuf}; @@ -59,11 +64,22 @@ fn main() { let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap(); let out_dir = env::var("OUT_DIR").unwrap(); - build_wasm_c_api_headers(&crate_dir, &out_dir); - build_wasmer_headers(&crate_dir, &out_dir); + if building_c_api_headers() { + build_wasm_c_api_headers(&crate_dir, &out_dir); + build_wasmer_c_api_headers(&crate_dir, &out_dir); + } + build_inline_c_env_vars(); } +/// Check whether we should build the C API headers. +/// +/// For the moment, it's always enabled, unless if the `DOCS_RS` +/// environment variable is present. +fn building_c_api_headers() -> bool { + env::var("DOCS_RS").is_err() +} + /// Build the header files for the `wasm_c_api` API. fn build_wasm_c_api_headers(crate_dir: &str, out_dir: &str) { let mut crate_header_file = PathBuf::from(crate_dir); @@ -126,7 +142,7 @@ fn build_wasm_c_api_headers(crate_dir: &str, out_dir: &str) { } /// Build the header files for the `deprecated` API. -fn build_wasmer_headers(crate_dir: &str, out_dir: &str) { +fn build_wasmer_c_api_headers(crate_dir: &str, out_dir: &str) { let mut crate_header_file = PathBuf::from(crate_dir); crate_header_file.push("wasmer"); From 16d4639c284f582d6526a285b5437fd183ad622d Mon Sep 17 00:00:00 2001 From: Ivan Enderlin Date: Fri, 22 Jan 2021 14:26:56 +0100 Subject: [PATCH 5/5] doc(changelog) Add #2044. --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 637b76e1945..69565029161 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - [#2041](https://github.com/wasmerio/wasmer/pull/2041) Documentation diagrams now have a solid white background rather than a transparent background. ### Fixed +- [#2044](https://github.com/wasmerio/wasmer/pull/2044) Do not build C headers on docs.rs. ## 1.0.1 - 2021-01-12