如何从 build.rs 定位 Cargo.lock 文件。

huangapple go评论64阅读模式
英文:

How to locate the Cargo.lock from build.rs

问题

我想要能够在build.rs中读取Cargo.lock文件,而不必使用字面路径。

build.rs中,可以使用std::env::var("CARGO_MANIFEST_DIR").unwrap();来查找Cargo.toml,传统上Cargo.lock位于该目录中,但是随着工作空间的引入,这不再总是正确的。

英文:

I want to be able to read the Cargo.lock file from build.rs without resorting to literal paths.

In build.rs, std::env::var("CARGO_MANIFEST_DIR").unwrap(); can be used to find the Cargo.toml, and traditionally the Cargo.lock was in this directory, however with the introduction of workspaces that is no longer always true.

答案1

得分: 1

https://crates.io/crates/project-root 可以用于从当前目录向上查找最近的Cargo.lock文件。

Cargo.toml中添加:

[build-dependencies]
project-root = "*"

然后在build.rs中使用project_root::get_project_root().unwrap()来获取Cargo.lock所在的目录。

(最好找到一个不需要额外依赖的解决方案)

英文:

https://crates.io/crates/project-root can be used to find the nearest Cargo.lock up the directory structure from the current directory.

In Cargo.toml add

[build-dependencies]
project-root = "*"

And in build.rs use project_root::get_project_root().unwrap() to obtain the directory where Cargo.lock is.

(It would be nice to find a solution which doesn't need additional dependencies)

huangapple
  • 本文由 发表于 2023年1月9日 07:43:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定