How to allow members from different platforms to exist in the same workspace

I have an project that Cargo.toml like:

[workspace]
members = [
    "member1",  # only use on windows
    "member2",  # all platform
]

How to prevent member2 from being compiled in non-Windows target environments?

Maybe you need something like #[cfg(target_os = "windows")] and #[cfg(not(target_os = "windows"))] in your code.

What's more, in your project's Cargo.toml(which uses member1 and member2), you could directly adding something like:

[target.'cfg(windows)'.dependencies]
member1 = "*"

[target.'cfg(not(windows))'.dependencies]
member2 = "*"

" this virtual manifest specifies a target section, which is not allowed "

target section is not allowed in workspace's Cargo.toml

The question is that, you use the wrong structure. You should use member1 and member2 as modules of one crate, not seperate crates that stored in the workspace.

If you really want to publish member1 and member2 seperately, then nothing you could do to stop users use member1 in linux or macOS.

If you want to auto select between member1 and member2, you should write a wrapper crate, member, write the target.*.dependencies in member's Cargo.toml.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.