mirror of https://github.com/rust-lang/cargo
Compare commits
13 Commits
4642332e28
...
d2279fb30d
Author | SHA1 | Date |
---|---|---|
heisen | d2279fb30d | |
bors | eee4ea2f5a | |
bors | c4e19cc890 | |
Eric Huss | 06fb65e753 | |
Scott Schafer | cf197fc499 | |
Scott Schafer | c3b104e11e | |
heisen-li | 000267ddfc | |
heisen-li | 55a381892d | |
heisen-li | 6128f9cbfc | |
heisen-li | 2ff01c6aab | |
heisen | 060f29f33c | |
heisen-li | 0f806ab0fe | |
heisen-li | dd7f6d8b9b |
|
@ -1147,11 +1147,26 @@ impl<'gctx> Workspace<'gctx> {
|
|||
}
|
||||
|
||||
pub fn emit_warnings(&self) -> CargoResult<()> {
|
||||
let ws_lints = self
|
||||
.root_maybe()
|
||||
.workspace_config()
|
||||
.inheritable()
|
||||
.and_then(|i| i.lints().ok())
|
||||
.unwrap_or_default();
|
||||
|
||||
let ws_cargo_lints = ws_lints
|
||||
.get("cargo")
|
||||
.cloned()
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.replace('-', "_"), v))
|
||||
.collect();
|
||||
|
||||
for (path, maybe_pkg) in &self.packages.packages {
|
||||
let path = path.join("Cargo.toml");
|
||||
if let MaybePackage::Package(pkg) = maybe_pkg {
|
||||
if self.gctx.cli_unstable().cargo_lints {
|
||||
self.emit_lints(pkg, &path)?
|
||||
self.emit_lints(pkg, &path, &ws_cargo_lints)?
|
||||
}
|
||||
}
|
||||
let warnings = match maybe_pkg {
|
||||
|
@ -1179,22 +1194,12 @@ impl<'gctx> Workspace<'gctx> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
pub fn emit_lints(&self, pkg: &Package, path: &Path) -> CargoResult<()> {
|
||||
let ws_lints = self
|
||||
.root_maybe()
|
||||
.workspace_config()
|
||||
.inheritable()
|
||||
.and_then(|i| i.lints().ok())
|
||||
.unwrap_or_default();
|
||||
|
||||
let ws_cargo_lints = ws_lints
|
||||
.get("cargo")
|
||||
.cloned()
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k.replace('-', "_"), v))
|
||||
.collect();
|
||||
|
||||
pub fn emit_lints(
|
||||
&self,
|
||||
pkg: &Package,
|
||||
path: &Path,
|
||||
ws_cargo_lints: &manifest::TomlToolLints,
|
||||
) -> CargoResult<()> {
|
||||
let mut error_count = 0;
|
||||
let toml_lints = pkg
|
||||
.manifest()
|
||||
|
@ -1212,11 +1217,21 @@ impl<'gctx> Workspace<'gctx> {
|
|||
.map(|(name, lint)| (name.replace('-', "_"), lint))
|
||||
.collect();
|
||||
|
||||
// We should only be using workspace lints if the `[lints]` table is
|
||||
// present in the manifest, and `workspace` is set to `true`
|
||||
let ws_cargo_lints = pkg
|
||||
.manifest()
|
||||
.resolved_toml()
|
||||
.lints
|
||||
.as_ref()
|
||||
.is_some_and(|l| l.workspace)
|
||||
.then(|| ws_cargo_lints);
|
||||
|
||||
check_im_a_teapot(
|
||||
pkg,
|
||||
&path,
|
||||
&normalized_lints,
|
||||
&ws_cargo_lints,
|
||||
ws_cargo_lints,
|
||||
&mut error_count,
|
||||
self.gctx,
|
||||
)?;
|
||||
|
@ -1224,7 +1239,7 @@ impl<'gctx> Workspace<'gctx> {
|
|||
pkg,
|
||||
&path,
|
||||
&normalized_lints,
|
||||
&ws_cargo_lints,
|
||||
ws_cargo_lints,
|
||||
&mut error_count,
|
||||
self.gctx,
|
||||
)?;
|
||||
|
@ -1232,7 +1247,7 @@ impl<'gctx> Workspace<'gctx> {
|
|||
pkg,
|
||||
&path,
|
||||
&normalized_lints,
|
||||
&ws_cargo_lints,
|
||||
ws_cargo_lints,
|
||||
&mut error_count,
|
||||
self.gctx,
|
||||
)?;
|
||||
|
|
|
@ -88,7 +88,7 @@ impl Lint {
|
|||
pub fn level(
|
||||
&self,
|
||||
pkg_lints: &TomlToolLints,
|
||||
ws_lints: &TomlToolLints,
|
||||
ws_lints: Option<&TomlToolLints>,
|
||||
edition: Edition,
|
||||
) -> (LintLevel, LintLevelReason) {
|
||||
self.groups
|
||||
|
@ -188,7 +188,7 @@ fn level_priority(
|
|||
default_level: LintLevel,
|
||||
edition_lint_opts: Option<(Edition, LintLevel)>,
|
||||
pkg_lints: &TomlToolLints,
|
||||
ws_lints: &TomlToolLints,
|
||||
ws_lints: Option<&TomlToolLints>,
|
||||
edition: Edition,
|
||||
) -> (LintLevel, LintLevelReason, i8) {
|
||||
let (unspecified_level, reason) = if let Some(level) = edition_lint_opts
|
||||
|
@ -211,7 +211,7 @@ fn level_priority(
|
|||
LintLevelReason::Package,
|
||||
defined_level.priority(),
|
||||
)
|
||||
} else if let Some(defined_level) = ws_lints.get(name) {
|
||||
} else if let Some(defined_level) = ws_lints.and_then(|l| l.get(name)) {
|
||||
(
|
||||
defined_level.level().into(),
|
||||
LintLevelReason::Workspace,
|
||||
|
@ -234,7 +234,7 @@ pub fn check_im_a_teapot(
|
|||
pkg: &Package,
|
||||
path: &Path,
|
||||
pkg_lints: &TomlToolLints,
|
||||
ws_lints: &TomlToolLints,
|
||||
ws_lints: Option<&TomlToolLints>,
|
||||
error_count: &mut usize,
|
||||
gctx: &GlobalContext,
|
||||
) -> CargoResult<()> {
|
||||
|
@ -306,7 +306,7 @@ pub fn check_implicit_features(
|
|||
pkg: &Package,
|
||||
path: &Path,
|
||||
pkg_lints: &TomlToolLints,
|
||||
ws_lints: &TomlToolLints,
|
||||
ws_lints: Option<&TomlToolLints>,
|
||||
error_count: &mut usize,
|
||||
gctx: &GlobalContext,
|
||||
) -> CargoResult<()> {
|
||||
|
@ -390,7 +390,7 @@ pub fn unused_dependencies(
|
|||
pkg: &Package,
|
||||
path: &Path,
|
||||
pkg_lints: &TomlToolLints,
|
||||
ws_lints: &TomlToolLints,
|
||||
ws_lints: Option<&TomlToolLints>,
|
||||
error_count: &mut usize,
|
||||
gctx: &GlobalContext,
|
||||
) -> CargoResult<()> {
|
||||
|
|
|
@ -90,13 +90,15 @@ impl<T> SleepTracker<T> {
|
|||
#[test]
|
||||
fn returns_in_order() {
|
||||
let mut s = SleepTracker::new();
|
||||
s.push(3, 3);
|
||||
s.push(30_000, 30_000);
|
||||
s.push(1, 1);
|
||||
s.push(6, 6);
|
||||
s.push(5, 5);
|
||||
s.push(2, 2);
|
||||
s.push(10000, 10000);
|
||||
assert_eq!(s.len(), 6);
|
||||
std::thread::sleep(Duration::from_millis(100));
|
||||
assert_eq!(s.to_retry(), &[1, 2, 3, 5, 6]);
|
||||
assert_eq!(s.len(), 2);
|
||||
std::thread::sleep(Duration::from_millis(2));
|
||||
assert_eq!(s.to_retry(), &[1]);
|
||||
assert!(s.to_retry().is_empty());
|
||||
let next = s.time_to_next().expect("should be next");
|
||||
assert!(
|
||||
next < Duration::from_millis(30_000),
|
||||
"{next:?} should be less than 30s"
|
||||
);
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
* [Package Layout](guide/project-layout.md)
|
||||
* [Cargo.toml vs Cargo.lock](guide/cargo-toml-vs-cargo-lock.md)
|
||||
* [Tests](guide/tests.md)
|
||||
* [Creating a Workspace](guide/creating-a-new-workspace.md)
|
||||
* [Continuous Integration](guide/continuous-integration.md)
|
||||
* [Cargo Home](guide/cargo-home.md)
|
||||
* [Build Cache](guide/build-cache.md)
|
||||
|
|
|
@ -0,0 +1,141 @@
|
|||
# Creating a New Workspace
|
||||
|
||||
A [workspace][def-workspace] is a collection of one or more packages,
|
||||
called workspace members, that are managed together.
|
||||
|
||||
In this chapter, we will create a workspace `new_workspace` containing
|
||||
binary member `foo` and library member `bar`.
|
||||
|
||||
As mentioned in [`[workspace]` section][workspace-section], the workspace must
|
||||
have at least one member, either the [root package] or a [virtual manifest].
|
||||
|
||||
Next we create a workspace containing [root package].
|
||||
For convenience, you can first create a package using the command `cargo new new_workspace`.
|
||||
Then add the `[workspace]` section to the `Cargo.toml` file in the root directory
|
||||
to make it a manifest of the workspace:
|
||||
|
||||
```toml
|
||||
# [new_workspace]/Cargo.toml
|
||||
[workspace]
|
||||
|
||||
[package]
|
||||
name = "new_workspace"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
```
|
||||
|
||||
Then, continue adding members `foo` and `bar` to the workspace:
|
||||
|
||||
```console
|
||||
$ cd new_workspace
|
||||
$ cargo new foo
|
||||
$ cargo new bar --lib
|
||||
```
|
||||
|
||||
Cargo will automatically add members to `Cargo.toml`。
|
||||
At this point, the workspace will contain three members: `foo` and `bar` and
|
||||
the default member `new_workspace`.
|
||||
|
||||
```toml
|
||||
# [new_workspace]/Cargo.toml
|
||||
[workspace]
|
||||
members = [ "bar", "foo" ]
|
||||
|
||||
[package]
|
||||
name = "new_workspace"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
```
|
||||
|
||||
The package at this point contains the following files:
|
||||
|
||||
```console
|
||||
$ cd new_workspace
|
||||
$ tree .
|
||||
.
|
||||
├── bar
|
||||
│ ├── Cargo.toml
|
||||
│ └── src
|
||||
│ └── lib.rs
|
||||
├── Cargo.toml
|
||||
├── foo
|
||||
│ ├── Cargo.toml
|
||||
│ └── src
|
||||
│ └── main.rs
|
||||
└── src
|
||||
└── main.rs
|
||||
|
||||
5 directories, 6 files
|
||||
```
|
||||
|
||||
Let's move on and create a virtual workspace.
|
||||
|
||||
In the another `new_workspace` empty directory, create a new `Cargo.toml` file and
|
||||
add the `[workspace]` section:
|
||||
|
||||
```toml
|
||||
# [new_workspace]/Cargo.toml
|
||||
[workspace]
|
||||
```
|
||||
|
||||
If using a virtual workspace, then the version of [resolver] needs to be specified
|
||||
in the table (if not, the default version of resolver for a workspace is `1`,
|
||||
even if the default resolver version for workspace members is `2`), for example:
|
||||
|
||||
```toml
|
||||
# [new_workspace]/Cargo.toml
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
```
|
||||
|
||||
Likewise, you can then use the `cargo new <package>` command to create
|
||||
binary member `foo` and library member `bar`.
|
||||
|
||||
|
||||
```toml
|
||||
# [new_workspace]/Cargo.toml
|
||||
[workspace]
|
||||
resolver = "2"
|
||||
members = [ "bar","foo"]
|
||||
|
||||
```
|
||||
|
||||
The package at this point contains the following files:
|
||||
|
||||
```console
|
||||
$ cd new_workspace
|
||||
$ tree .
|
||||
.
|
||||
├── bar
|
||||
│ ├── Cargo.toml
|
||||
│ └── src
|
||||
│ └── lib.rs
|
||||
├── Cargo.toml
|
||||
└── foo
|
||||
├── Cargo.toml
|
||||
└── src
|
||||
└── main.rs
|
||||
|
||||
4 directories, 5 files
|
||||
```
|
||||
|
||||
Up to this point, we have a workspace with two members.
|
||||
Whenever you run `cargo build` under the workspace root directory, Cargo builds
|
||||
all member at once.
|
||||
Instead of building the entire workspace, you could use the `--package`/`-p` flag
|
||||
to select certain packages.
|
||||
For example, `cargo build -p foo` will build only `foo` package.
|
||||
|
||||
[workspace-section]: ../reference/workspaces.md#the-workspace-section
|
||||
[root package]: ../reference/workspaces.md#root-package
|
||||
[virtual manifest]: ../reference/workspaces.md#virtual-workspace
|
||||
[def-workspace]: ../appendix/glossary.md#workspace '"workspace" (glossary entry)'
|
||||
[resolver]: ../reference/resolver.md
|
|
@ -5,6 +5,7 @@ develop Rust packages.
|
|||
|
||||
* [Why Cargo Exists](why-cargo-exists.md)
|
||||
* [Creating a New Package](creating-a-new-project.md)
|
||||
* [Creating a Workspace](creating-a-new-workspace.md)
|
||||
* [Working on an Existing Cargo Package](working-on-an-existing-project.md)
|
||||
* [Dependencies](dependencies.md)
|
||||
* [Package Layout](project-layout.md)
|
||||
|
|
|
@ -982,3 +982,43 @@ error: `im_a_teapot` is specified
|
|||
)
|
||||
.run();
|
||||
}
|
||||
|
||||
#[cargo_test]
|
||||
fn dont_always_inherit_workspace_lints() {
|
||||
let p = project()
|
||||
.file(
|
||||
"Cargo.toml",
|
||||
r#"
|
||||
[workspace]
|
||||
members = ["foo"]
|
||||
|
||||
[workspace.lints.cargo]
|
||||
im-a-teapot = "warn"
|
||||
"#,
|
||||
)
|
||||
.file(
|
||||
"foo/Cargo.toml",
|
||||
r#"
|
||||
cargo-features = ["test-dummy-unstable"]
|
||||
|
||||
[package]
|
||||
name = "foo"
|
||||
version = "0.0.1"
|
||||
edition = "2015"
|
||||
authors = []
|
||||
im-a-teapot = true
|
||||
"#,
|
||||
)
|
||||
.file("foo/src/lib.rs", "")
|
||||
.build();
|
||||
|
||||
p.cargo("check -Zcargo-lints")
|
||||
.masquerade_as_nightly_cargo(&["cargo-lints"])
|
||||
.with_stderr(
|
||||
"\
|
||||
[CHECKING] foo v0.0.1 ([CWD]/foo)
|
||||
[FINISHED] [..]
|
||||
",
|
||||
)
|
||||
.run();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue