7

I have a rust project where i include the mysql-crate dependency and i want to have it os independent.

So i tried: Cargo.toml

[package]
name = "test"
version = "0.1.0"
authors = ["daMaex"]

[dependencies]
ws = "*"
clap = "*"
env_logger = "*"

[target.'cfg(any(unix, macos))'.dependencies.mysql]
version = "*"
default-features = false
features = ["socket"]

[target.'cfg(windows)'.dependencies.mysql]
version = "*"
default-features = false
features = ["pipe"]

[features]
default = []
ssl = []

The error already happens with a minimal main: src/main.rs

fn main () {
}

But the build fails. On macos/unix it always wants to compile the pipe and get's an unresolved import:

error[E0432]: unresolved import `std::os::windows::io::RawHandle`
  --> /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/named_pipe-0.2.2/src/lib.rs:38:5
   |
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Could not find `windows` in `std::os`

and the same happens on Windows for the mysql crate itself:

error[E0432]: unresolved import `std::os::unix`
  --> C:\Users\user\.cargo\registry\src\github.com-1ecc6299db9ec823\mysql-7.1.2\src\io.rs:24:5
   |
24 | use std::os::unix as unix;
   |     ^^^^^^^^^^^^^^^^^^^^^ no `unix` in `std::os`

So my question is, how do i handle the OS-Dependency in this case?

Da Maex
  • 481
  • 7
  • 27
  • I would look into [conditional compilation](http://rustbyexample.com/attribute/cfg.html); you'll probably need two versions of each function/module that is platform-dependent. Although it's hard to say which method would be best for you, since you haven't provided code, I would probably suggest using that attribute in a function and adding your `use` declarations *inside the function*. – Aurora0001 Oct 27 '16 at 09:10
  • @Aurora0001 I'm not using the os dependency in my own code, the crate mysql depends on it. – Da Maex Oct 27 '16 at 09:11
  • Oh, I see what you mean. I had a look at the other crates that use `mysql` to see if they handle it like you, and it seems that none of them use conditions in their `Cargo.toml`, so I don't know what would cause this. Perhaps it'd be worth asking on GitHub if you get no response here? – Aurora0001 Oct 27 '16 at 09:22
  • 2
    This looks like a `cargo` bug to me. – WiSaGaN Oct 27 '16 at 09:49

1 Answers1

2

This looks like a cargo bug to me. One related issue is Cannot declare different version of dependencies in mutually exclusive targets #3195

Edit: It is more of an unsupported feature than a bug according to the code.

WiSaGaN
  • 46,887
  • 10
  • 54
  • 88