Repository: clap-rs/clap (language: rust)

Issue:
Recent minor version upgrade of clap breaks building with a wasm target.
### Please complete the following tasks

- [X] I have searched the [discussions](https://github.com/clap-rs/clap/discussions)
- [X] I have searched the [open](https://github.com/clap-rs/clap/issues) and [rejected](https://github.com/clap-rs/clap/issues?q=is%3Aissue+label%3AS-wont-fix+is%3Aclosed) issues

### Rust Version

rustc 1.67.0-nightly (b3bc6bf31 2022-11-24)

### Clap Version

between 4.0.22 and 4.0.27

### Minimal reproducible code

```rust
fn main() {}
```


### Steps to reproduce the bug with the above code

just compile to wasm32-unknown-unknown and it breaks. 4.0.22 works as expected, 4.0.27 does not compile.

### Actual Behaviour

```
~/lending-contracts (develop) » cargo update -p clap                                                                                                                                                                                                       ewoolsey@Erics-MacBook-Pro
    Updating crates.io index
    Updating clap v4.0.22 -> v4.0.27
      Adding errno v0.2.8
      Adding errno-dragonfly v0.1.2
      Adding hermit-abi v0.2.6
      Adding io-lifetimes v1.0.2
      Adding is-terminal v0.4.0
      Adding linux-raw-sys v0.1.3
      Adding rustix v0.36.3
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
~/lending-contracts (develop*) » d b                                                                                                                                                                                              ewoolsey@Erics-MacBook-Pro
   Compiling libc v0.2.137
   Compiling io-lifetimes v1.0.2
   Compiling rustix v0.36.3
   Compiling errno v0.2.8
   Compiling neptune-authorization v0.1.0 (/Users/ewoolsey/lending-contracts/packages/neptune-auth)
error[E0583]: file not found for module `sys`
  --> /Users/ewoolsey/.cargo/registry/src/github.com-1ecc6299db9ec823/errno-0.2.8/src/lib.rs:33:1
   |
33 | mod sys;
   | ^^^^^^^^
   |
   = help: to create the module `sys`, create file "/Users/ewoolsey/.cargo/registry/src/github.com-1ecc6299db9ec823/errno-0.2.8/src/sys.rs" or "/Users/ewoolsey/.cargo/registry/src/github.com-1ecc6299db9ec823/errno-0.2.8/src/sys/mod.rs"

error[E0425]: cannot find function `errno` in module `sys`
   --> /Users/ewoolsey/.cargo/registry/src/github.com-1ecc6299db9ec823/errno-0.2.8/src/lib.rs:101:10
    |
101 |     sys::errno()
    |          ^^^^^ not found in `sys`

error[E0425]: cannot find function `set_errno` in module `sys`
   --> /Users/ewoolsey/.cargo/registry/src/github.com-1ecc6299db9ec823/errno-0.2.8/src/lib.rs:106:10
    |
106 |     sys::set_errno(err)
    |          ^^^^^^^^^ not found in `sys`

Some errors have detailed explanations: E0425, E0583.
For more information about an error, try `rustc --explain E0425`.
error: could not compile `errno` due to 3 previous errors
warning: build failed, waiting for other jobs to finish...
process exited unsuccessfully: exit status: 101
```

### Expected Behaviour

Minor version increments should not introduce breaking changes. I'm not sure exactly which version increment causes the issue but it appears to be from the inclusion of errno.

### Additional Context

_No response_

### Debug Output

_No response_


Discussion / hints:
After fiddling a little bit I can confirm that the offender in 4.0.27 specifically. Everything works as expected on 4.0.26.
Might be useful to add a wasm build to you CI workflow to avoid accidental bugs like that.
4.0.27 saw us switch from atty to is-terminal, so that is the root cause.

We also do check `wasm32-unknown-unknown`  and `wasm32-wasi` in CI
- https://github.com/clap-rs/clap/blob/master/.github/workflows/ci.yml#L78
- https://github.com/clap-rs/clap/blob/master/Makefile#L18

This was all done in #4249 and the wams jobs definitely built is-terminal and it worked
- https://github.com/clap-rs/clap/actions/runs/3541494860/jobs/5945830558
- https://github.com/clap-rs/clap/actions/runs/3541494860/jobs/5945830669

So why did this work in CI but not for you?
> So why did this work in CI but not for you?

That's a fantastic question. Is there any other information I can give you that might be useful? Unfortunately I'm working on a large not-yet-open-source project otherwise I'd zip my build for you. I'm working on a Mac, although I doubt that's related to the problem.
Seeing the same thing in our project as well.

To reproduce:

```
git clone https://github.com/johanhelsing/matchbox
cd matchbox/matchbox_demo
cargo check --target wasm32-unknown-unknown
# no errors
cargo update -p clap
cargo check --target wasm32-unknown-unknown
# same error as ewoolsey
```
> So why did this work in CI but not for you?

it seems to have picked atty looking at the logs .. ?

`Checking atty v0.2.14`

EDIT: wasm CI doesn't include color feature which is the feature that brings is-terminal ?

`cargo check --features "deprecated derive cargo env unicode string unstable-replace unstable-grouped" --all-targets --workspace`
Working on rustup default stable -> 1.65.0 w/ associated w32-u-u target installed

So granular dep changes here were:

root@5d44a4311b14:/matchbox/matchbox_demo# cargo update -p clap
```
    Updating crates.io index
    Updating clap v4.0.18 -> v4.0.27
    Updating clap_derive v4.0.18 -> v4.0.21
      Adding errno v0.2.8
      Adding errno-dragonfly v0.1.2
      Adding hermit-abi v0.2.6
      Adding io-lifetimes v1.0.2
      Adding is-terminal v0.4.0
      Adding linux-raw-sys v0.1.3
      Adding rustix v0.36.3
```

I can replicate it inside rust default docker w/ add rustup target add wasm32-u-u from scratch

Immediate work/around seems if one disables the `color` feature by explicitly setting the defaults:

`clap = { version = "4.0", default-features = false, features = ["derive", "std", "help", "usage", "error-context", "suggestions"] }`

This prevents feature color (default) being included which brings is-terminal dependency that doesn't work with wasm32-u-u

EDIT: Intiially I had a brainfreeze and I had written no-default-features = true which obviously doesn't work ;)

This would need some cfg magic I think in order for the cargo not include these crates that don't work in wasm32-u-u

It's probably quite involved to cfg default features with: `target_family = "wasm", target_os = "unknown"`

I've also asked @sunfishcode if is-terminal could stub wasm32-u-u: https://github.com/sunfishcode/is-terminal/issues/9
> EDIT: wasm CI doesn't include color feature which is the feature that brings is-terminal ?

`color` is a default feature.

If you look in the linked [logs](https://github.com/clap-rs/clap/actions/runs/3541494860/jobs/5945830558), you see that `termcolor` and `is-terminal` are being built.  I think the question is if we are using the default target or what I thought was an overriding target.
Got a [reproduction in CI](https://github.com/clap-rs/clap/pull/4515)

There are two failures
- `io-lifetimes` fails to compile with `wasm32-unknown-unknown`
- The errno issue above for wasi

Fix patch:
diff --git a/Cargo.lock b/Cargo.lock
index 3095e02db05..175c745f1b4 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -445,15 +445,15 @@ dependencies = [
 
 [[package]]
 name = "io-lifetimes"
-version = "0.7.3"
+version = "0.7.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "1ea37f355c05dde75b84bba2d767906ad522e97cd9e2eef2be7a4ab7fb442c06"
+checksum = "59ce5ef949d49ee85593fc4d3f3f95ad61657076395cbbce23e2121fc5542074"
 
 [[package]]
 name = "io-lifetimes"
-version = "1.0.1"
+version = "1.0.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a7d367024b3f3414d8e01f437f704f41a9f64ab36f9067fa73e526ad4c763c87"
+checksum = "46112a93252b123d31a119a8d1a1ac19deac4fac6e0e8b0df58f0d4e5870e63c"
 dependencies = [
  "libc",
  "windows-sys 0.42.0",
@@ -461,13 +461,13 @@ dependencies = [
 
 [[package]]
 name = "is-terminal"
-version = "0.4.0"
+version = "0.4.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "aae5bc6e2eb41c9def29a3e0f1306382807764b9b53112030eff57435667352d"
+checksum = "927609f78c2913a6f6ac3c27a4fe87f43e2a35367c0c4b0f8265e8f49a104330"
 dependencies = [
  "hermit-abi 0.2.6",
- "io-lifetimes 1.0.1",
- "rustix 0.36.3",
+ "io-lifetimes 1.0.3",
+ "rustix 0.36.4",
  "windows-sys 0.42.0",
 ]
 
@@ -752,27 +752,27 @@ checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
 
 [[package]]
 name = "rustix"
-version = "0.35.9"
+version = "0.35.13"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "72c825b8aa8010eb9ee99b75f05e10180b9278d161583034d7574c9d617aeada"
+checksum = "727a1a6d65f786ec22df8a81ca3121107f235970dc1705ed681d3e6e8b9cd5f9"
 dependencies = [
  "bitflags",
  "errno",
- "io-lifetimes 0.7.3",
+ "io-lifetimes 0.7.5",
  "libc",
  "linux-raw-sys 0.0.46",
- "windows-sys 0.36.1",
+ "windows-sys 0.42.0",
 ]
 
 [[package]]
 name = "rustix"
-version = "0.36.3"
+version = "0.36.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0b1fbb4dfc4eb1d390c02df47760bb19a84bb80b301ecc947ab5406394d8223e"
+checksum = "cb93e85278e08bb5788653183213d3a60fc242b10cb9be96586f5a73dcb67c23"
 dependencies = [
  "bitflags",
  "errno",
- "io-lifetimes 1.0.1",
+ "io-lifetimes 1.0.3",
  "libc",
  "linux-raw-sys 0.1.3",
  "windows-sys 0.42.0",
@@ -909,7 +909,7 @@ version = "0.2.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "8440c860cf79def6164e4a0a983bcc2305d82419177a0e0c71930d049e3ac5a1"
 dependencies = [
- "rustix 0.35.9",
+ "rustix 0.35.13",
  "windows-sys 0.36.1",
 ]
 
diff --git a/Cargo.toml b/Cargo.toml
index f025dde4796..68acc107d4c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -96,7 +96,7 @@ clap_lex = { path = "./clap_lex", version = "0.3.0" }
 bitflags = "1.2"
 unicase = { version = "2.6", optional = true }
 strsim = { version = "0.10",  optional = true }
-is-terminal = { version = "0.4",  optional = true }
+is-terminal = { version = "0.4.1",  optional = true }
 termcolor = { version = "1.1.1", optional = true }
 terminal_size = { version = "0.2.1", optional = true }
 backtrace = { version = "0.3", optional = true }


Test patch: