Conditionally exclude dependencies based on target (wasm32-unknown-unknown)

I would like to conditionally compile a cargo crate based on the target architecture. I want to exclude certain dependencies when compiling to WASM with cargo build --target=wasm32-unknown-unknown.

I have read Platform-specific dependencies in the Cargo Book and tried this syntax in my Cargo.toml:

[target.'cfg(not(target_arch = "wasm32-unknown-unknown"))'.dependencies]
termion = "1.5"
chrono = "^0.4"
linefeed = "0.6"

and I annotated relevant code with
#[cfg(not(target_arch = "wasm32-unknown-unknown"))]

but when I run cargo build --target=wasm32-unknown-unknown I still get compilation errors about specific crates (dirs-1.0.5) not finding some platform specific OS functions. For example

error[E0433]: failed to resolve: could not find `unix` in `os`
  --> /home/_/.cargo/registry/src/github.com-1ecc6299db9ec823/dirs-1.0.5/src/lin.rs:41:18
   |
41 |     use std::os::unix::ffi::OsStringExt;
   |                  ^^^^ could not find `unix` in `os`

However when I manually disable the relevant dependencies the library compiles fine. How do I correctly disable those dependencies when compiling to wasm32-unknown-unknown?

Ah, I used the wrong cfg.
Use
cfg(all(target_arch = "wasm32", target_os = "unknown"))

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.