How to specify the path to a dependency located in my home directory in Cargo.toml?
Asked Answered
B

3

5

I want to use a local package which is located in my home directory. By default, Cargo searches for dependencies relative to Cargo.toml. If I know where my project is located relative to the home folder, I can do something like this:

[dependencies]
tools = { path = "../../rust_libs/tools" }

I don't always know where my project is located and I would like to do something like this:

[dependencies]
tools = { path = "${HOME}/rust_libs/tools" }

How can I get the home path inside of Cargo.toml? Maybe there are other ways to achieve this?

Beghard answered 5/4, 2021 at 10:6 Comment(2)
Did you try ~/rust_libs/tools?Calling
@Calling yes. It just appends this path to Cargo.toml path and shows error. "Unable to update /Users/vladas/dev/sand/rust_sand/~/rust_libs/tools"Beghard
C
7

You can try to workaround this issue with native linux soft links:

  1. Create a soft link to ~/rust_libs/tools in your Cargo.toml's directory using commad:
ln -s ~/rust_libs/tools
  1. In Cargo.toml just use relative path:
[dependencies]
tools = { path = "tools" }
Calling answered 6/4, 2021 at 12:3 Comment(0)
E
4

It should work if you use a path that starts with '/' which should be recognized as an absolute path. If the path starts with ~/ or $HOME/ and there is no env variable expansion, then those paths will look like a relative path, and cargo will prepend the current path. I don't think Cargo.toml supports substituting environment variables, so you will have to specify the whole path (ie. /Users/vladas/rust_libs/tools)

Epinasty answered 5/4, 2021 at 17:8 Comment(4)
It works for paths starting with '/'. Thank you. But with this approach i still need to know user name and also the path will be different on windows and linux. Is there a way to make it crossplatform?Beghard
@Beghard Sorry it's not clear what you did. How did you get it to work?Stepdame
[dependencies] tools = { path = "/Users/vladas/rust_libs/tools" } Epinasty
I can't get it to work on Windows. Complains about invalid path no matter what I try.Spiral
M
1

On Windows:

dioxus-web = {path = "D:/github/dioxus/packages/web"}
dioxus-desktop = {path = "D:\\github\\dioxus\\packages\\desktop"}

Both are working.

Mossbunker answered 24/10, 2023 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.