英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论