Skip to content

Commit

Permalink
Merge #2042 #2044
Browse files Browse the repository at this point in the history
2042: feat(cli) Improve environment variables parsing r=Hywan a=Hywan

# Description

This PR fixes #2028. In #1762 we have already improved environment variables syntax, but the same work must be done in the CLI crate.

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.

The environment variable is trimmed before being parsed with [`str::trim`](https://doc.rust-lang.org/std/primitive.str.html#method.trim).

This PR also adds tests for the `parse_envvar` function, which was missing.

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


2044: feat(c-api) Do not build C headers if the env var `DOCS_RS` exists r=Hywan a=Hywan

# Description

Fix #1954.

According to https://docs.rs/about/builds, the `DOCS_RS` environment variable is set when the documentation is built from docs.rs. In this case, we must not build the C headers because it generates such error (see #1954 to learn more):

> Unable to copy the generated C bindings: `Os { code: 30, kind: Other, message: "Read-only file system" }`

# Review

- [x] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Ivan Enderlin <[email protected]>
  • Loading branch information
bors[bot] and Hywan authored Jan 22, 2021
3 parents 4ba8194 + 0bf86c8 + 16d4639 commit b7e1987
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
- [#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
- [#2044](https://github.com/wasmerio/wasmer/pull/2044) Do not build C headers on docs.rs.

## 1.0.1 - 2021-01-12

Expand Down
22 changes: 19 additions & 3 deletions lib/c-api/build.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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");

Expand Down
51 changes: 45 additions & 6 deletions lib/cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,53 @@ 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::<Vec<&str>>()[..] {
Ok((env_var.to_string(), value.to_string()))
} else {
bail!(
"Env vars must be of the form <var_name>=<value>. Found {}",
let entry = entry.trim();

match entry.find('=') {
None => bail!(
"Environment variable must be of the form `<name>=<value>`; found `{}`",
&entry
),

Some(0) => bail!(
"Environment variable is not well formed, the `name` is missing in `<name>=<value>`; got `{}`",
&entry
),

Some(position) if position == entry.len() - 1 => bail!(
"Environment variable is not well formed, the `value` is missing in `<name>=<value>`; got `{}`",
&entry
),

Some(position) => 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 `<name>=<value>`; found `A`"
);
assert_eq!(
parse_envvar("=A").unwrap_err().to_string(),
"Environment variable is not well formed, the `name` is missing in `<name>=<value>`; got `=A`"
);
assert_eq!(
parse_envvar("A=").unwrap_err().to_string(),
"Environment variable is not well formed, the `value` is missing in `<name>=<value>`; 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())
);
}
}

0 comments on commit b7e1987

Please sign in to comment.