Different features for one git package but with different branches

I have git package library, where branch = "dev" for development and branch = "master" for production.

In my main app, using features I want to configure what branch to use.
Something like this:

[target.'cfg(feature = "dev")'.dependencies]
utils = { git = "https://url/utils.git", branch = "master", optional = true }
[target.'cfg(feature = "prod")'.dependencies]
utils = { git = "https://url/utils.git", branch = "dev", optional = true }

Code above does't work, cause of:

Dependency 'utils' has different source paths depending on the build target. Each dependency must have a single canonical source path irrespective of build target.

I found the way like this:

[features]
dev = ["utils-dev"]
prod = ["utils-prod"]

utils-prod = { git = "https://url/utils.git", branch = "master", optional = true, package = "utils" }
utils-dev = { git = "https://url/utils.git", branch = "dev", optional = true, package = "utils" }

But every time when I use this library, I need to add this code:

#[cfg(feature = "dev")]
use utils_dev as regex;
#[cfg(feature = "prod")]
use utils_prod as regex;

Maybe exists better and easier way to do this?

Yes. You could try this in your root mod (like lib.rs or main.rs)

#[cfg(feature = "dev")]
extern crate utils_dev as regex;
#[cfg(feature = "prod")]
extern crate utils_prod as regex;

then you could use regex in any level of submod without repeating them.

2 Likes

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.