How to patch crates-io with path using Cargo? - rust

In my Rust crate I need to be able to use 2 git repositories both checked out in the root of my crate. They are both modified and pkg_b requires an older version of pkg_a, 0.1.0. I can't seem to get pkg_b to use the newer version of pkg_a, 0.2.0. My current workaround has been to just update the Cargo.toml file in pkg_b.
My crate's Cargo.toml:
[package]
name = "mypkg"
version = "0.1.0"
[dependencies]
pkg_a = { path = "./pkg_a" } # This is 0.2.0 with my changes added
pkg_b = { path = "./pkg_b" }
[patch.crates-io]
pkg_a = { path = "./pkg_a", features = ["serde_support"] }
And pkg_b's Cargo.toml:
[package]
name = "pkg_b"
version = "0.1.0"
[dependencies]
pkg_a = { version = "0.1.0", features = ["serde_support"] }

If pkg_b requires pkg_a ^0.1.0 (meaning "between 0.1.0 and 0.2.0", see caret requirements in Cargo), you can't make it accept pkg_a 0.2.0 as a dependency, even if you patch the registry to make local pkg_a repository discoverable. Maintaining a patch for pkg_b/Cargo.toml seems like your best bet.

Related

Getting dependency error in rust Cargo.toml

I am getting some dependencies version errors in rust.
I am fairly new to rust so I do not have any idea how to check for compatible updates between multiple dependencies.
It will be great if someone can resolve this issue and let me know how to manage dependencies.
Cargo.toml:
[package]
name = "chatbot"
version = "0.2.3"
authors = ["Joe Wilm <joe#jwilm.com>"]
license = "MIT"
description = "An extensible chatbot"
documentation = "https://docs.rs/chatbot"
repository = "https://github.com/jwilm/chatbot"
keywords = ["chat", "bot", "extensible"]
readme = "README.md"
[package.metadata.docs.rs]
all-features = true
[dependencies]
regex = "0.1"
rustc-serialize = "0.3"
getopts = "0.2"
irc = { version = "0.12", optional = true }
slack = { version = "0.18.0", optional = true }
[features]
default = []
irc-adapter = ["irc"]
slack-adapter = ["slack"]
I tried with versions suggested by VS Code but it did not work.
Error which I am getting after I do cargo-run is:
failed to select a version for the requirement `security-framework
= "^0.1.13"`
candidate versions found which didn't match: 2.8.2, 2.8.1, 2.8.0, ...
location searched: crates.io index
required by package `native-tls v0.1.2`
... which satisfies dependency `native-tls = "^0.1.2"` of package `tungstenite v0.2.0`
... which satisfies dependency `tungstenite = "^0.2.0"` of package `slack v0.18.0`
... which satisfies dependency `slack = "^0.18.0"` of package `chatbot v0.2.3
You are using old versions of the slack and irc crates. Both depend on an old version of native-tls (v0.1) in their dependency trees. native-tls v0.1 depends on a very old version of the security-framework crate (v0.1). All versions of security-framework v0.1 have been yanked from crates.io, see here: https://crates.io/crates/security-framework/versions. I assume this is due to a security issue in those versions. Because the v0.1 version of security-framework is yanked, you can't download it from crates.io anymore, causing the dependency error during compilation.
You can fix this by updating your irc and slack dependencies to their latest versions:
[package]
name = "chatbot"
version = "0.2.3"
authors = ["Joe Wilm <joe#jwilm.com>"]
license = "MIT"
description = "An extensible chatbot"
documentation = "https://docs.rs/chatbot"
repository = "https://github.com/jwilm/chatbot"
keywords = ["chat", "bot", "extensible"]
readme = "README.md"
[package.metadata.docs.rs]
all-features = true
[dependencies]
regex = "0.1"
rustc-serialize = "0.3"
getopts = "0.2"
irc = { version = "0.15", optional = true }
slack = { version = "0.25", optional = true }
[features]
default = []
irc-adapter = ["irc"]
slack-adapter = ["slack"]
Be aware that updating these dependencies is likely to break your code, since there are probably API changes in between your current and the latest version of these crates.
Just update your dependencies especially
[dependencies]
irc = { version = "0.15.0", optional = true }
slack = { version = "0.25.0", optional = true }
seem to be troublesome.

rust-gpu how to use as a dependency?

I am trying to integrate rust-gpu into a project. The docs explain how to use it as a build dependency but the examples use it as a straight dependency, I have struggled with both but I would prefer to get the dependency version to work as it's more suited for my purposes.
I downloaded the rust-gpu git repo and compiled the ash example, which runs.
This is the toml I used for compilation:
[package]
name = "example-runner-ash"
version = "0.0.0"
authors = ["Embark <opensource#embark-studios.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"
publish = false
# See rustc_codegen_spirv/Cargo.toml for details on these features
[features]
default = ["use-compiled-tools"]
use-installed-tools = ["spirv-builder/use-installed-tools"]
use-compiled-tools = ["spirv-builder/use-compiled-tools"]
[dependencies]
ash = "0.35"
ash-window = "0.9"
winit = "0.26"
structopt = "0.3.20"
cfg-if = "1.0.0"
shared = { path = "../../shaders/shared" }
spirv-builder = { path = "../../../crates/spirv-builder", default-features = false }
# TODO: Remove this once no longer needed, only needed to make cargo-deny happy for some reason.
# https://rustsec.org/advisories/RUSTSEC-2021-0119
nix = "0.20.2"
[target.'cfg(target_os = "macos")'.dependencies]
ash-molten = { version = "0.12.0", features = ["pre-built"] }
So I thought if I did something similar on my own code it would work:
[package]
name = "vulkan_bindings"
version = "0.1.0"
edition = "2021"
[features]
default = ["use-compiled-tools"]
use-installed-tools = ["spirv-builder/use-installed-tools"]
use-compiled-tools = ["spirv-builder/use-compiled-tools"]
[dependencies]
ash = { version = "0.37.0" }
glfw = { version = "0.45.0", features = ["vulkan"] }
gpu-allocator = "0.18.0"
ash-window = "0.9"
winit = "0.26"
structopt = "0.3.20"
cfg-if = "1.0.0"
# shared = { path = "../../shaders/shared" }
spirv-builder = { path = "rust-gpu/crates/spirv-builder", default-features = false }
paste = "1.0.8"
termcolor = "1.1.3"
But this fails with a multiplicity of errors, the first of which is:
error[E0432]: unresolved import `rustc_codegen_ssa::METADATA_FILENAME`
--> /home/makogan/.cargo/git/checkouts/rust-gpu-e0a37a82a46176e6/8052971/crates/rustc_codegen_spirv/src/link.rs:9:52
|
9 | use rustc_codegen_ssa::{CodegenResults, NativeLib, METADATA_FILENAME};
| ^^^^^^^^^^^^^^^^^ no `METADATA_FILENAME` in the root
Not that I am pointing my project to the spirv-builder crate in the rust-gpu repo I downloaded, which worked for the ash example.
I also tried just following the instructions in the docs
So I declared spirv-builder as a build-dependency (using the git link) and I made a build.rs build script and copy pasted the snippet in the docs, as described.
I get the same error about missing METADATA_FILENAME.
I do have a rust-toolchain file setup just like the docs mentioned, and I tried switching the edition field in the toml to 2018, but I consistently get the same error.
I am nto sure what to do now.

Rust in two different projects but same Cargo.toml. The other project is giving me an issue of dependency issue

Rust in two different projects but same Cargo.toml. The other project is giving me an issue of dependency issue while the other one is building fine. Both are compiling and in the same rust nightly build.
Am I missing anything?
Below is the error that I am seeing when I do `cargo build
error: failed to select a version for the requirement `pbkdf2 = "^0.2.0"`
candidate versions found which didn't match: 0.9.0, 0.8.0, 0.7.5, ...
required by package `mongodb v0.3.12`
... which satisfies dependency `mongodb = "^0.3.12"` of package `r2d2-mongodb v0.2.2`
... which satisfies dependency `r2d2-mongodb = "^0.2.2"` of package
Here's my cargo.toml
[dependencies]
anyhow = "1.0.34"
chrono = { version = "0.4.19", features = ["serde"] }
dotenv = "0.15.0"
jsonwebtoken = "7.2.0"
r2d2 = "0.8.9"
r2d2-mongodb = "0.2.2"
rand = "0.7.3"
rocket = "0.4.8"
rocket_contrib = { version = "0.4.8", features = ["helmet", "uuid"] }
rust-argon2 = "0.8.2"
serde = { version = "1.0.117", features = ["derive"] }
uuid = { version = "0.8.1", features = ["serde", "v4"] }
log = "0.4"
log4rs = "0.8"
[dev-dependencies]
lazy_static = "1.4.0"
serde_json = "1.0.59"
My rustc version
rustc 1.56.0-nightly (29ef6cf16 2021-08-31)
failed to select a version for the requirement `pbkdf2 = "^0.2.0"`
A transitive dependency is looking for a pbkdf2 0.2.x dependency. However, all versions of pbkdf2 prior to 0.3.0 have been yanked from crates.io. I haven't found a specific reason why, it could be due to a security vulnerability or other error on the authors' part, wherein they've decided these versions shouldn't be used.
The likely reason you get the error in one project and not the other is because one has a Cargo.lock file that has already selected a pbkdf2 0.2 version. Yanking doesn't prevent a version's use, only prevents new crates from depending on it.
The fix is to copy the Cargo.lock from the working project to the other.

How can I force rustqlite to be built as statically linked when using sqlcipher?

I'm using rustqlite and am trying to configure it to use sqlcipher via a Cargo feature. In the usual case, rustqlite has a bundled feature to include the sqlite source. When changing to sqlcipher, the source isn't bundled anymore.
Locally I was able to install sqlcipher and compile my project (installed via brew install sqlcipher on Mac).
The resulting binary dynamically links to the local sqlcipher installation so I can no longer distribute the binary to customers
How I can I embed the library libsqlcipher.0.dylib into the binary? Customers can't be expected to install sqlcipher on their own.
For reference, looking at the linked libraries for a dummy project:
/tmp/try-sqlite/cipher$ otool -L target/release/cipher
target/release/cipher:
/usr/local/opt/sqlcipher/lib/libsqlcipher.0.dylib (compatibility version 9.0.0, current version 9.6.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.60.1)
/usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0)
My project is simple:
Cargo.toml
[package]
name = "cipher"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[dependencies.rusqlite]
version = "0.24.2"
features = ["sqlcipher"]
main.rs
use rusqlite::{params, Connection, Result, NO_PARAMS};
use std::thread::sleep;
#[derive(Debug)]
struct Person {
id: i32,
name: String,
data: Option<Vec<u8>>,
}
fn main() -> Result<()> {
let conn = Connection::open("/tmp/enc2.db")?;
conn.execute("PRAGMA KEY='passphrase'", NO_PARAMS);
conn.execute(
"CREATE TABLE IF NOT EXISTS person (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
data BLOB
)",
NO_PARAMS,
)?;
let me = Person {
id: 0,
name: "Steven".to_string(),
data: None,
};
conn.execute(
"INSERT INTO person (name, data) VALUES (?1, ?2)",
params![me.name, me.data],
)?;
let mut stmt = conn.prepare("SELECT id, name, data FROM person")?;
let person_iter = stmt.query_map(NO_PARAMS, |row| {
Ok(Person {
id: row.get(0)?,
name: row.get(1)?,
data: row.get(2)?,
})
})?;
for person in person_iter {
println!("Found person {:?}", person.unwrap());
}
Ok(())
}
EDIT
I have a partial solution to static linking (not tested robustly yet) by exporting
SQLCIPHER_STATIC=1
otool -L /private/tmp/try-sqlite/cypher/target/debug/cypher
/private/tmp/try-sqlite/cypher/target/debug/cypher:
/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.11)
/usr/local/opt/openssl#1.1/lib/libcrypto.1.1.dylib (compatibility version 1.1.0, current version 1.1.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.60.1)
/usr/lib/libresolv.9.dylib (compatibility version 1.0.0, current version 1.0.0)
Open issues
how to auto-export this environment variable (through toml config / build.rs - still working on that)
developer ergonomics - how to ensure that sqlcipher is installed on a dev machine (through build.rs / toml dev etc. config)
ensure that CI will run with SQLCIPHER_STATIC=1 (and have it installed)
It looks like this is currently not possible with the version published on crates.io.
But there is an open Pull Request that you can probably use for that.
First clone the working branch locally and then use it as a patch in your Cargo.toml like that:
[dependencies]
rusqlite = { version = "0.24.2", features = ["bundled-sqlcipher"] }
[patch.crates-io]
rusqlite = { path = "../path/to/your_patch" }
Note that I did not test this and because the feature only exists in the patch it might be that you need to include the feature branch like that:
[dependencies]
rusqlite = { path = "../path/to/your_patch", features = ["bundled-sqlcipher"] }
A word of Warning: As stated in the PR it is not clear if zetetic (the vendor of sqlcipher) is fine with that, so you should be cautious when you use this (especially in a commercial product).
Edit
This Issue / Comment is also relevant.

Resolving imports in Rust

I'm having trouble with importing rand crate from crates.io. After adding the line rand="0.8.3" and then running command cargo build for the project, it keeps displaying the same errors:
error[E0432]: unresolved import `rand`
--> main.rs:1:5
|
1 | use rand::Rng;
| ^^^^ maybe a missing crate `rand`?
error[E0433]: failed to resolve: use of undeclared crate or module `rand`
--> main.rs:4:25
|
4 | let secret_number = rand::thread_rng().gen_range(1..=11);
| ^^^^ use of undeclared crate or module `rand`
error: aborting due to 2 previous errors
the cargo.toml file
[package]
name = "roller"
version = "0.1.0"
authors = ["User"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.3"
Basically the simplest reproducible example is this single line of code:
use rand::Rng;
fn main(){
let secret_number = rand::thread_rng().gen_range(1..=11);
print!("{}",secret_number);
}
What's wrong with it?
Just in case:
The **cargo.lock**file:
# This file is automatically #generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.86"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7282d924be3275cec7f6756ff4121987bc6481325397dde6ba3e7802b1a8b1c"
[[package]]
name = "ppv-lite86"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857"
[[package]]
name = "rand"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_hc",
]
[[package]]
name = "rand_chacha"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7"
dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [
"rand_core",
]
[[package]]
name = "roller"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"
In file Cargo.toml add lines:
[dependencies]
rand="0.3.14"
And rebuild project!
It worked with these versions:
rustc 1.53.0
cargo 1.53.0
Took answer from here https://github.com/rust-lang/rust/issues/34616
got the same error when I run it with vs code(rust-analyzer).
When you press the run button
rustic main.rs this command is called in terminal, it will cause error.
Type this in terminal,
cargo build
cargo run
It works well
If you think that you did everything right and get a strange unable to import error like this then you could try cargo clean.
That will allow you to fully re-build your binary/library after that.

Resources