如何在build.rs脚本中使用本地项目文件?
如何在build.rs
中使用本地文件而不包含其绝对路径?我有,例如<code>让proto_file_glob_paths:paths = glob(“ ../ proto/**/*。proto”)。unwrap(); 。
我正在使用build.rs
中的相对路径,因为我不想编码文件的绝对路径,因此它可以在其他开发人员机器上使用。该构建可以与货物构建
和货物运行
一起工作,但是不是 货物出版
。这是因为当运行货物>运行
时,工作目录/路径是不同的。
在中,货物构建
,env :: current_dir()
is:
project_name/target/debug/build/project_name-70e21ac88134b5a1/build-script-build
in cargo Publish
,envy> encry_der_dir()是:
project_name/target/package/project_name-x.y.z/target/debug/build/project_name-xxxxxxx/build-script-build
我不知道我可以使用任何环境变量访问build.rs
或src/
的目录,以便我可以创建一个相对/绝对路径动态路径。即使cargo_manifest_dir
对于构建
和发布
也有所不同。
项目具体细节:我还有其他代码线可以使用其他软件包生成锈蚀代码,并且在货物构建
和货物运行
>。
How do I use local files without including their absolute paths in build.rs
? I have, for example let proto_file_glob_paths: Paths = glob("../proto/**/*.proto").unwrap();
.
I am using relative paths in my build.rs
, because I don't want to hardcode the absolute path of files, so that it works on other developer machines. The build works fine with cargo build
and cargo run
, but not cargo publish
. This is because the working directory/path is different when cargo publish
is run.
In cargo build
, the env::current_dir()
is:
project_name/target/debug/build/project_name-70e21ac88134b5a1/build-script-build
In cargo publish
, the env::current_dir()
is:
project_name/target/package/project_name-x.y.z/target/debug/build/project_name-xxxxxxx/build-script-build
I don't know of any environment variables I can use to access the directory of build.rs
, or src/
, so that I can create a relative/absolute path dynamically. Even CARGO_MANIFEST_DIR
is different for build
and publish
.
Project specifics: I've got other lines of code to generate the rust code using other packages, and this works fine in cargo build
and cargo run
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在@cerberus和@colonelthirtytwo的一些非常有用的提示之后,我只是:
货物构建
之前,我写了一个脚本来执行此操作。当然,我需要更新使用proto/
而不是../ proto/
的相对路径。proto
目录添加到gitignore
文件中proto
目录添加到package.include
in。
proto
文件添加到includ> include
in货物中。 toml
After some very helpful prompting from @Cerberus and then from @ColonelThirtyTwo, I just:
cargo build
. Of course, I needed to update the relative path to useproto/
instead of../proto/
.proto
directory to thegitignore
fileproto
directory topackage.include
inCargo.toml
because it would not be included as it is gitignored:My mistakes were that:
../proto
should work, but as Cerberus explained, it won't. Since the files would be missing for the users of the crate.proto
file toinclude
inCargo.toml