What is ``npm install ../my-another-package'' in Rust's Cargo? - rust

In my Cargo.toml, I defined [dependencies] like:
[dependencies]
my-another-package = "0.0.1"
But I don't want to put my_another_package on GitHub because it is proprietary. I want to install it from my local disk. Using NPM, I would do something like npm install ../my-another-package.

You want what Cargo calls a "path dependency":
[dependencies.my-another-package]
path = "path/to/my-another-package"

Related

Cargo package doesn't work with Rust's workspace?

I create a "workspace" with several folder within it following the tutorial I read here
It runs successfully with cargo run or cargo build
if all of the package were independent from each other, cargo package would run successfully. But as soon as one package depends on the other the cargo package will fail.
It displays: no matching package named "foo_2" found. location searched: registry "crates-io". Which is pretty weird, since I specifically add a local path on the dependencies.
Is this an intended behavior? if so, then why should I bother with workspace at all ?
The root Cargo.toml
[workspace]
members = [
"foo_1",
"foo_2",
]
foo_1/Cargo.toml
[package]
name = "foo_1"
version = "0.1.0"
edition = "2021"
# error here. It can't found the foo_2 package.
[dependencies]
foo_2 = { path = "../foo_2", version = "0.1.0" }
foo_2/Cargo.toml
[package]
name = "foo_2"
version = "0.1.0"
edition = "2021"
[dependencies]
Error message:
PS E:\Works\Experimentals\rust-workspace> cargo package --workspace
warning: manifest has no description, license, license-file, documentation, homepage or repository.
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
Packaging foo_1 v0.1.0 (E:\Works\Experimentals\rust-workspace\foo_1)
Verifying foo_1 v0.1.0 (E:\Works\Experimentals\rust-workspace\foo_1)
Updating crates.io index
error: failed to verify package tarball
Caused by:
no matching package named `foo_2` found
location searched: registry `crates-io`
required by package `foo_1 v0.1.0 (E:\Works\Experimentals\rust-workspace\target\package\foo_1-0.1.0)`
Packaging and publishing crates requires all dependencies of said crate to also be available in a registry. For publishing this is relatively obvious, since consumers also need to be able to fetch and build transitive dependencies. Creating tarballs also happens to have the same constraints at the moment, so it is not possible if they are not meant to be published.
Whenever you have a project with many crates in a single workspace and wish to publish them on crates.io, you would start with the crate without dependencies and work your way up to the other crates.
cargo publish -p foo_2
cargo publish -p foo_1
Or, using cargo-workspaces:
cargo workspaces publish
Is this an intended behavior?
One can still publish crates in a workspace, so long as this is done in the right order. For packaging, it is a limitation at the time of writing. The current behavior could be linked with packaging being primarily part of publishing, so this could probably be improved.
If so, then why should I bother with workspace at all?
Tangential to the matter here. Workspaces exist mainly to settle other concerns, such as having a single source of compiled dependencies with a shared dependency lock. This distinction is described in that same link.

No matching package found

I am trying to integrate an API in a yew project and facing the following issue:
Dark#Dark:/var/www/html/yew-practice$ wasm-pack build --target web
Error: Error during execution of `cargo metadata`: Updating crates.io index
Updating git repository `https://github.com/yewstack/yew`
error: no matching package found
searched package name: `yewtil`
perhaps you meant: yew
location searched: https://github.com/yewstack/yew
Cargo.toml:
[package]
name = "yew-practice"
version = "0.1.0"
edition = "2018"
[lib]
crate-type = ["cdylib"]
[dependencies]
wasm-bindgen = "^0.2"
serde="1"
yew = { git = "https://github.com/yewstack/yew" }
yewtil = { git = "https://github.com/yewstack/yew", features = ["fetch"] }
How do I solve the problem above?
The error tells you that no package yewtil was found in the Git repository. If you go to the repository and check its Cargo.toml file, you will indeed notice that it doesn't include a yewtil package.
I searched in the repository for yewtil, and found this pull request that refactored the project and merged yewtil into other packages: yewstack/yew#1842.
You have two options now:
Drop the dependency on yewtil, and use the documentation to figure out where the features have moved that you want to use.
Add a tag key to the dependency to pull in the latest release that included yewtil, or simply switch to the latest published version on crates.io.
If you want to get the latest features from yew, which appears to be the case given that you're pulling in the package from GitHub and not crates.io, go with option 1. You can use the documentation and the examples in the master branch to see how to use the package in its latest version.
Yew git repository is not a valid address, it must end with .git.
git = "https://github.com/yewstack/yew.git"

How do I fix mismatching dependencies in my Cargo file to work around native library collisions?

I'm setting up a Rust server with Rocket and I'm trying to use it with a JWT library. They use different versions of the *ring* crate and I get an error during cargo build:
error: multiple packages link to native library `ring-asm`, but a native library can be linked only once
package `ring v0.12.1`
... which is depended on by `jsonwebtoken v4.0.1`
... which is depended on by `auther v0.1.0 (file:///home/drpytho/x/downloadble/auther)`
links to native library `ring-asm`
package `ring v0.11.0`
... which is depended on by `cookie v0.9.2`
... which is depended on by `rocket v0.3.6`
... which is depended on by `rocket_codegen v0.3.6`
... which is depended on by `auther v0.1.0 (file:///home/drpytho/x/downloadble/auther)`
also links to native library `ring-asm`
My Cargo.toml
[package]
name = "auther"
version = "0.1.0"
authors = ["Name <Email#mail.se>"]
[dependencies]
rocket = "0.3.6"
rocket_codegen = "0.3.6"
jsonwebtoken = "4"
serde_derive = "1"
serde = "1"
I read that you are supposed to fix the mismatching dependencies in your Cargo file, but I can't figure out how to do it.
You have to fix this by not transitively depending on different versions of crates that link to a native library.
There's no newer version of rocket available that depends on version 0.10 of cookie, which depends on ring 0.12, so you'll need to downgrade jsonwebtoken to 2.0.3.
You can work this out by checking the crates.io pages for the crates in question (like with jsonwebtoken), going back through older versions, and looking to see what dependencies it needs.

"given version requirement is invalid" while downloading dependencies

Following this example:
$ cargo build --verbose
Updating registry `https://github.com/rust-lang/crates.io-index`
failed to parse registry's information for: wayland-client
Caused by:
the given version requirement is invalid
This happens on every example and builds from other repos. I am running Ubuntu 16.04 LTS and I installed Cargo and Rustc through apt. Using versions: rustc 1.7.0 and cargo 0.8.0.
My Cargo.toml from the example:
[package]
name = "spinning-square"
version = "0.1.0"
authors = [
"TyOverby <ty#pre-alpha.com>",
"Nikita Pekin <contact#nikitapek.in>"
]
[[bin]]
name = "spinning-square"
[dependencies]
piston = "0.31.1"
piston2d-graphics = "0.21.1"
pistoncore-glutin_window = "0.35.0"
piston2d-opengl_graphics = "0.40.0"
I ended up using
$ sudo apt install libsdl2-dev
and then it started to work. However, I do not know if this is the true solution because I removed libsdl2 and it continued to work after that. A theory of mine is that because Piston uses sdl2, it corrected something to make it work, maybe.
If anyone has found a better solution for the next guy to have issues, I'll leave this post unanswered for a while longer.

Build only `lib` target

I want to build a dynamic link library (dll).
My Cargo.toml currently looks like this:
[package]
name = "sample"
version = "0.1.0"
authors = ["author"]
[lib]
name = "main"
crate-type = ["dylib"]
[dependencies]
I use VS Code with the RustyCode plugin as my IDE on windows.
When I run the build command this builds into a sample.exe and main.dll.
I know I can run cargo build --lib to only build my lib target but I dont have access to this command inside VS Code (afaik).
Is there anyway to specify that I only want to build the lib target in my Cargo.toml file so I can use the VS Code build command which runs cargo build/cargo run?
Cargo builds files using convention over configuration approach. When it finds a main.rs it builds an executable, and when it encounters lib.rs it expects to build a library.
Calling your lib main managed to confuse Cargo. The only solution I managed to find is to either change name of your crate from name = "main" to name = "foo" (and then rename your main.rs into foo.rs) or to change its name to lib.rs, as you did.
Just figured it: Rename the src/main.rs to src/lib.rs and it only builds the lib target!

Resources