My Cargo.toml is displaying some red lines with error couldn't compile serde_derive - rust

I try to build a project using rust-lang recently (this is my first rust project, and my boss is supporting me to use a new technology in my company). But, I suddenly get some red lines on my Cargo.toml :
could not compile `serde_derive`.
error: could not compile `async-trait`.
To learn more, run the command again with --verbose.
error: could not compile `rand_chacha`.
To learn more, run the command again with --verbose.
error: could not compile `proc-macro-hack`.
To learn more, run the command again with --verbose.
error: could not compile `diesel_derives`.
To learn more, run the command again with --verbose.
I run a command Cargo Run and my project is running well, but these red lines prevent me to tracking an error on my other code in my project (So if there is an error in code, it won't display as there are still exists some errors in another file, it is Cargo.toml)
I am using cargo 1.43.0-nightly (bda50510d 2020-03-02), rustc 1.43.0-nightly (c20d7eecb 2020-03-11), and vs code 1.43 version.
This is my Cargo.toml :
[package]
name = "app_base"
version = "0.1.0"
authors = ["yonathan"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.4.3"
rocket_codegen = "0.4.3"
rocket_contrib = "0.4.3"
rocket_http = "0.4.3"
cookie = "0.11.2"
rocket-json-response = "0.5.10"
diesel = { version = "1.4.3", features = ["postgres"] }
dotenv = "0.15.0"
postgres = { version = "0.17.2", features = ["with-chrono-0_4"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
json-gettext = "3.1.7"
debug-helper = "0.3.8"
serializers = "0.2.3"
rocket_cors = { git = "https://github.com/lawliet89/rocket_cors", branch = "master" }
chrono = "0.4"

As this is an RLS (rust language server) bug [which is closed apparently, see here], a temporary alternative can be using the rust-analyzer extension [website here].
To install the extension, you can launch VSCode, click on the Extensions tab on the left and search for rust-analyzer in the Marketplace.
Please note that as mentioned on the website [as of 04/01/2020], this project is still in ALPHA, which means it may be prone to breakages.

Related

Unable to compile Rust AWS Lambda using rusoto

Im having some problems compiling a Rust lambda. The problems started after I included the rusoto package, and it is complaining about linker problems (options and libraries).
Im running:
cargo lambda build
Getting:
= note: warning: unsupported linker arg: -znoexecstack
warning: unsupported linker arg: -zrelro
warning: unsupported linker arg: -znow
ld.lld: error: unable to find library -lssl
ld.lld: error: unable to find library -lcrypto
My Cargo.toml:
[package]
name = "super-lambda"
version = "0.1.0"
edition = "2021"
# Starting in Rust 1.62 you can use `cargo add` to add dependencies
# to your project.
#
# If you're using an older Rust version,
# download cargo-edit(https://github.com/killercup/cargo-edit#installation)
# to install the `add` subcommand.
#
# Running `cargo add DEPENDENCY_NAME` will
# add the latest version of a dependency to the list,
# and it will keep the alphabetic ordering for you.
[dependencies]
lambda_http = "0.7"
lambda_runtime = "0.7"
lazy_static = "1.4.0"
rusoto_core = "0.48.0"
rusoto_dynamodb = "0.48.0"
serde = "1.0.149"
tokio = { version = "1", features = ["macros"] }
tracing = { version = "0.1", features = ["log"] }
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }
I did try to disable the zig-linker:
cargo lambda build --disable-zig-linker
Then it compiles, but AWS will complain:
/var/task/bootstrap: error while loading shared libraries: libssl.so.3: cannot open shared object file: No such file or directory
What can I do to resolve this?
Cargo-lambda will do a cross-compilation targeting x86_64-unknown-linux-gnu. To avoid relying on a libssl dependency, you can try building the project using rustls for rusoto. You should be able to enable rustls feature for rusoto as follows:
rusoto_core = {version = "0.48.0", features = ["rustls"], default-features = false}
rusoto_dynamodb = {version = "0.48.0", features = ["rustls"], default-features = false}
You can also see this GitHub issue which relates to this topic.
If you are on Linux, you might try to install libssl-dev and try to build it again. However, in my experience, zigbuild (used by cargo-lambda for cross-compilation) might struggle to find this dependency.

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.

use of undeclared crate or module in rust [duplicate]

If I define a dependency like foo = { version = "1.0.0", optional = true },
will it be available when I do "cargo run"? Can I check if it is enabled in the code?
if cfg!(feature = "foo") {}
Doesn't seem to be working, like the feature is missing all the time.
Moving answer to 60258216 here:
Optional dependencies do double as features: https://stackoverflow.com/a/39759592/8182118
They will not be enabled by default unless they're listed in the default feature, though you can enable the feature using cargo run --features foo.
For clarity and forward compatibility you may want to create an actual feature which enables the dependency though, that way if you need to "fluff up" the feature in the future and that requires new optional dependencies it's much easier.
In the code, both #[cfg] and cfg! should work depending whether you want to check for it at compile-time or runtime.
It's not hard to test either:
[package]
name = "testx"
version = "0.1.0"
edition = "2018"
[features]
default = ["boolinator"]
magic = ["boolinator"]
empty = []
[dependencies]
boolinator = { version = "*", optional = true }
and a main.rs of:
fn main() {
# macro and attributes would work the same here
if cfg!(feature = "boolinator") {
println!("Hello, bool!");
} else {
println!("Hello, world!");
}
}
you get
$ cargo run -q
Hello, bool!
$ cargo run -q --no-default-features
Hello, world!
$ cargo run -q --no-default-features --features boolinator
Hello, bool!
$ cargo run -q --no-default-features --features magic
Hello, bool!
$ cargo run -q --no-default-features --features empty
Hello, world!
See also https://github.com/rust-lang/edition-guide/issues/96

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.

Why building with rustc command cannot see crates?

Hi I'm trying to explore Rust. What I'm trying to do is using glob module, when i build code with $cargo build and $cargo run it's succesfully builds and runs the executable but if i try it with $rustc main.rs it gives me
error[E0432]: unresolved import `glob`
--> src/main.rs:1:5
|
1 | use glob::glob;
| ^^^^ use of undeclared type or module `glob`
Any ideas?
Version : ╰─ rustc --version rustc 1.43.1 (8d69840ab 2020-05-04)
Code is here:
use glob::glob;
fn main() {
for entry in glob("/home/user/*.jpg").unwrap(){
match entry {
Ok(path) => println!("{:?}", path.display()),
Err(e) => println!("{:?}",e),
}
};
}
My toml
[package]
name = "test1"
version = "0.1.0"
authors = ["ieuD <example#mail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
glob = "0.3.0"
rustc on its own doesn't handle your dependencies, it just compiles stuff. When you run rustc on your file, it will start compiling it and encounter the unknown glob crate. Dependencies are handled by cargo via Cargo.toml.
Though you could only use rustc (see the answer here) it's a task that's considerably more difficult, that's why cargo is around.

Resources