在一个 Cargo 工作区中放置 .cargo/config.toml 文件应该放在哪里?

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

Where to put the .cargo/config.toml file in a cargo workspace?

问题

以下是您要翻译的内容:

在《Cargo Book》(第3.6. 配置页)中指出,当从工作区根目录调用 cargo 时,工作区内各个包指定的 .cargo/config.toml 文件不会被读取。

> 目前,当从工作区调用时,Cargo 不会读取工作区内的包中的配置文件。即,如果一个工作区包含两个包,命名为 /projects/foo/bar/baz/mylib/projects/foo/bar/baz/mybin,并且在 /projects/foo/bar/baz/mylib/.cargo/config.toml/projects/foo/bar/baz/mybin/.cargo/config.toml 处有 Cargo 配置,那么如果从工作区根目录 (/projects/foo/bar/baz/) 调用 Cargo,Cargo 不会读取这些配置文件。

此外,在14.3. Cargo Workspaces - The Rust Programming Language3.6. Configuration - The Cargo Book中也没有明确指定应该将 .cargo/config.toml 放在工作区中所有包的哪个位置。

我的目标是指定一个单一的文件夹,所有工作区中的包都应该在其中查找外部依赖的 vendored 代码。

例如,如果 path/to/workspace/root 是工作区根目录文件夹,则工作区中的包应该在 path/to/workspace/root/vendoredcode 中查找 vendored 外部依赖。

对于工作区外的单个包,我会将 .cargo/config.toml 放在包的根目录中,并在其中添加以下内容:

[source.crates-io]
replace-with="vendored-sources"

[source.vendored-sources]
directory="path/to/package/root/vendoredcode"

如何在所有工作区包中使用单个 .cargo/config.toml 文件执行相同操作?

附注
由于我一贯使用术语“package”,而一些人可能常用术语“crate”,可能会存在一些混淆。

我了解到,一个单一的 package(包含 Cargo.toml 和一个包含 main.rs 和/或 lib.rssrc 子文件夹的文件夹)实际上可以同时包含多个二进制 crate 和一个库 crate,因此我更喜欢使用术语“package”而不是“crate”。

这个选择是为了避免混淆,尽管我认识到,虽然它可能会避免与一组人混淆,但可能会在另一组人中引起混淆。因此,这是一种澄清。

英文:

It's stated in the Cargo Book (on page 3.6. Configuration) that when cargo is invoked from the workspace root, the .cargo/config.toml files specified for individual packages within the workspace do not get read.

>At present, when being invoked from a workspace, Cargo does not read config files from crates within the workspace. i.e. if a workspace has two crates in it, named /projects/foo/bar/baz/mylib and /projects/foo/bar/baz/mybin, and there are Cargo configs at /projects/foo/bar/baz/mylib/.cargo/config.toml and /projects/foo/bar/baz/mybin/.cargo/config.toml, Cargo does not read those configuration files if it is invoked from the workspace root (/projects/foo/bar/baz/).

Also, it's not exactly specified either in 14.3. Cargo Workspaces - The Rust Programming Language or 3.6. Configuration - The Cargo Book where to put a .cargo/config.toml that would be common to all the packages in the workspace.

My objective here is to specify a single folder where all packages in the workspace should look for the vendored code of external dependencies.

Like if path/to/workspace/root is the the workspace root folder, the packages in the workspace should look for vendored external dependencies in path/to/workspace/root/vendoredcode.

For a single package outside of a workspace, I'd put it .cargo/config.toml in the package root and put the following in it:

[source.crates-io]
replace-with="vendored-sources"

[source.vendored-sources]
directory="path/to/package/root/vendoredcode"

How to do the same for all packages in a workspace with a single .cargo/config.toml file?

PS:
There might be some confusion due to my consistent use of the term "package" where some people might commonly use the term "crate".

I read that a single package (a folder with Cargo.toml and an src subfolder which contains main.rs and/or lib.rs) can actually contain multiple binary crates and a single library crate at the same time, so I preferred to use the term "package" instead of "crate" for it.

This choice was to avoid confusion though I recognise that while it might avoid confusion with one set of people, it might create confusion with another set of people. Hence this clarification.

答案1

得分: 1

只需将.cargo/config.toml放在工作区的根目录即可。

英文:

Just put the .cargo/config.toml at the root of the workspace.

huangapple
  • 本文由 发表于 2023年6月5日 14:17:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76403909.html
匿名

发表评论

匿名网友

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

确定