Skip to content

Commit ceb1389

Browse files
committed
Auto merge of #6924 - ehuss:include-exclude-patterns, r=alexcrichton
Migrate package include/exclude to gitignore patterns. This moves to the next phase of #4268. This also includes a fdew more changes which can be removed if desired: - Add support for `!` negate gitignore patterns. - Add a warning if both package.include and package.exclude are specified.
2 parents 2e09266 + db3328e commit ceb1389

File tree

4 files changed

+293
-69
lines changed

4 files changed

+293
-69
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ pub fn package(ws: &Workspace<'_>, opts: &PackageOpts<'_>) -> CargoResult<Option
5656

5757
verify_dependencies(pkg)?;
5858

59+
if !pkg.manifest().exclude().is_empty() && !pkg.manifest().include().is_empty() {
60+
config.shell().warn(
61+
"both package.include and package.exclude are specified; \
62+
the exclude list will be ignored",
63+
)?;
64+
}
5965
// `list_files` outputs warnings as a side effect, so only do it once.
6066
let src_files = src.list_files(pkg)?;
6167

src/cargo/sources/path.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ impl<'cfg> PathSource<'cfg> {
101101
/// stages are:
102102
///
103103
/// 1) Only warn users about the future change iff their matching rules are
104-
/// affected. (CURRENT STAGE)
104+
/// affected.
105105
///
106106
/// 2) Switch to the new strategy and update documents. Still keep warning
107-
/// affected users.
107+
/// affected users. (CURRENT STAGE)
108108
///
109109
/// 3) Drop the old strategy and no more warnings.
110110
///
@@ -122,22 +122,34 @@ impl<'cfg> PathSource<'cfg> {
122122
p
123123
};
124124
Pattern::new(pattern)
125-
.map_err(|e| failure::format_err!("could not parse glob pattern `{}`: {}", p, e))
126125
};
127126

128127
let glob_exclude = pkg
129128
.manifest()
130129
.exclude()
131130
.iter()
132131
.map(|p| glob_parse(p))
133-
.collect::<Result<Vec<_>, _>>()?;
132+
.collect::<Result<Vec<_>, _>>();
134133

135134
let glob_include = pkg
136135
.manifest()
137136
.include()
138137
.iter()
139138
.map(|p| glob_parse(p))
140-
.collect::<Result<Vec<_>, _>>()?;
139+
.collect::<Result<Vec<_>, _>>();
140+
141+
// Don't warn if using a negate pattern, since those weren't ever
142+
// previously supported.
143+
let has_negate = pkg
144+
.manifest()
145+
.exclude()
146+
.iter()
147+
.chain(pkg.manifest().include().iter())
148+
.any(|p| p.starts_with("!"));
149+
// Don't warn about glob mismatch if it doesn't parse.
150+
let glob_is_valid = glob_exclude.is_ok() && glob_include.is_ok() && !has_negate;
151+
let glob_exclude = glob_exclude.unwrap_or_else(|_| Vec::new());
152+
let glob_include = glob_include.unwrap_or_else(|_| Vec::new());
141153

142154
let glob_should_package = |relative_path: &Path| -> bool {
143155
fn glob_match(patterns: &[Pattern], relative_path: &Path) -> bool {
@@ -176,21 +188,15 @@ impl<'cfg> PathSource<'cfg> {
176188
{
177189
Match::None => Ok(true),
178190
Match::Ignore(_) => Ok(false),
179-
Match::Whitelist(pattern) => Err(failure::format_err!(
180-
"exclude rules cannot start with `!`: {}",
181-
pattern.original()
182-
)),
191+
Match::Whitelist(_) => Ok(true),
183192
}
184193
} else {
185194
match ignore_include
186195
.matched_path_or_any_parents(relative_path, /* is_dir */ false)
187196
{
188197
Match::None => Ok(false),
189198
Match::Ignore(_) => Ok(true),
190-
Match::Whitelist(pattern) => Err(failure::format_err!(
191-
"include rules cannot start with `!`: {}",
192-
pattern.original()
193-
)),
199+
Match::Whitelist(_) => Ok(false),
194200
}
195201
}
196202
};
@@ -210,46 +216,45 @@ impl<'cfg> PathSource<'cfg> {
210216
let glob_should_package = glob_should_package(relative_path);
211217
let ignore_should_package = ignore_should_package(relative_path)?;
212218

213-
if glob_should_package != ignore_should_package {
219+
if glob_is_valid && glob_should_package != ignore_should_package {
214220
if glob_should_package {
215221
if no_include_option {
216222
self.config.shell().warn(format!(
217-
"Pattern matching for Cargo's include/exclude fields is changing and \
218-
file `{}` WILL be excluded in a future Cargo version.\n\
223+
"Pattern matching for Cargo's include/exclude fields has changed and \
224+
file `{}` is now excluded.\n\
219225
See <https://github.com/rust-lang/cargo/issues/4268> for more \
220226
information.",
221227
relative_path.display()
222228
))?;
223229
} else {
224230
self.config.shell().warn(format!(
225-
"Pattern matching for Cargo's include/exclude fields is changing and \
226-
file `{}` WILL NOT be included in a future Cargo version.\n\
231+
"Pattern matching for Cargo's include/exclude fields has changed and \
232+
file `{}` is no longer included.\n\
227233
See <https://github.com/rust-lang/cargo/issues/4268> for more \
228234
information.",
229235
relative_path.display()
230236
))?;
231237
}
232238
} else if no_include_option {
233239
self.config.shell().warn(format!(
234-
"Pattern matching for Cargo's include/exclude fields is changing and \
235-
file `{}` WILL NOT be excluded in a future Cargo version.\n\
240+
"Pattern matching for Cargo's include/exclude fields has changed and \
241+
file `{}` is NOT excluded.\n\
236242
See <https://github.com/rust-lang/cargo/issues/4268> for more \
237243
information.",
238244
relative_path.display()
239245
))?;
240246
} else {
241247
self.config.shell().warn(format!(
242-
"Pattern matching for Cargo's include/exclude fields is changing and \
243-
file `{}` WILL be included in a future Cargo version.\n\
248+
"Pattern matching for Cargo's include/exclude fields has changed and \
249+
file `{}` is now included.\n\
244250
See <https://github.com/rust-lang/cargo/issues/4268> for more \
245251
information.",
246252
relative_path.display()
247253
))?;
248254
}
249255
}
250256

251-
// Update to `ignore_should_package` for Stage 2.
252-
Ok(glob_should_package)
257+
Ok(ignore_should_package)
253258
};
254259

255260
// Attempt Git-prepopulate only if no `include` (see rust-lang/cargo#4135).

src/doc/src/reference/manifest.md

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ examples, etc.
7373

7474
#### The `build` field (optional)
7575

76-
This field specifies a file in the package root which is a [build script][1] for
77-
building native code. More information can be found in the build script
78-
[guide][1].
76+
This field specifies a file in the package root which is a [build script] for
77+
building native code. More information can be found in the [build script
78+
guide][build script].
7979

80-
[1]: reference/build-scripts.html
80+
[build script]: reference/build-scripts.html
8181

8282
```toml
8383
[package]
@@ -121,15 +121,39 @@ may be replaced by docs.rs links.
121121

122122
#### The `exclude` and `include` fields (optional)
123123

124-
You can explicitly specify to Cargo that a set of [globs][globs] should be
125-
ignored or included for the purposes of packaging and rebuilding a package. The
126-
globs specified in the `exclude` field identify a set of files that are not
127-
included when a package is published as well as ignored for the purposes of
128-
detecting when to rebuild a package, and the globs in `include` specify files
129-
that are explicitly included.
130-
131-
If a VCS is being used for a package, the `exclude` field will be seeded with
132-
the VCS’ ignore settings (`.gitignore` for git for example).
124+
You can explicitly specify that a set of file patterns should be ignored or
125+
included for the purposes of packaging. The patterns specified in the
126+
`exclude` field identify a set of files that are not included, and the
127+
patterns in `include` specify files that are explicitly included.
128+
129+
The patterns should be [gitignore]-style patterns. Briefly:
130+
131+
- `foo` matches any file or directory with the name `foo` anywhere in the
132+
package. This is equivalent to the pattern `**/foo`.
133+
- `/foo` matches any file or directory with the name `foo` only in the root of
134+
the package.
135+
- `foo/` matches any *directory* with the name `foo` anywhere in the package.
136+
- Common glob patterns like `*`, `?`, and `[]` are supported:
137+
- `*` matches zero or more characters except `/`. For example, `*.html`
138+
matches any file or directory with the `.html` extension anywhere in the
139+
package.
140+
- `?` matches any character except `/`. For example, `foo?` matches `food`,
141+
but not `foo`.
142+
- `[]` allows for matching a range of characters. For example, `[ab]`
143+
matches either `a` or `b`. `[a-z]` matches letters a through z.
144+
- `**/` prefix matches in any directory. For example, `**/foo/bar` matches the
145+
file or directory `bar` anywhere that is directly under directory `foo`.
146+
- `/**` suffix matches everything inside. For example, `foo/**` matches all
147+
files inside directory `foo`, including all files in subdirectories below
148+
`foo`.
149+
- `/**/` matches zero or more directories. For example, `a/**/b` matches
150+
`a/b`, `a/x/b`, `a/x/y/b`, and so on.
151+
- `!` prefix negates a pattern. For example, a pattern of `src/**.rs` and
152+
`!foo.rs` would match all files with the `.rs` extension inside the `src`
153+
directory, except for any file named `foo.rs`.
154+
155+
If git is being used for a package, the `exclude` field will be seeded with
156+
the `gitignore` settings from the repository.
133157

134158
```toml
135159
[package]
@@ -148,21 +172,14 @@ The options are mutually exclusive: setting `include` will override an
148172
necessary source files may not be included. The package's `Cargo.toml` is
149173
automatically included.
150174

151-
[globs]: https://docs.rs/glob/0.2.11/glob/struct.Pattern.html
152-
153-
#### Migrating to `gitignore`-like pattern matching
175+
The include/exclude list is also used for change tracking in some situations.
176+
For targets built with `rustdoc`, it is used to determine the list of files to
177+
track to determine if the target should be rebuilt. If the package has a
178+
[build script] that does not emit any `rerun-if-*` directives, then the
179+
include/exclude list is used for tracking if the build script should be re-run
180+
if any of those files change.
154181

155-
The current interpretation of these configs is based on UNIX Globs, as
156-
implemented in the [`glob` crate](https://crates.io/crates/glob). We want
157-
Cargo's `include` and `exclude` configs to work as similar to `gitignore` as
158-
possible. [The `gitignore` specification](https://git-scm.com/docs/gitignore) is
159-
also based on Globs, but has a bunch of additional features that enable easier
160-
pattern writing and more control. Therefore, we are migrating the interpretation
161-
for the rules of these configs to use the [`ignore`
162-
crate](https://crates.io/crates/ignore), and treat them each rule as a single
163-
line in a `gitignore` file. See [the tracking
164-
issue](https://github.com/rust-lang/cargo/issues/4268) for more details on the
165-
migration.
182+
[gitignore]: https://git-scm.com/docs/gitignore
166183

167184
#### The `publish` field (optional)
168185

@@ -615,6 +632,8 @@ and also be a member crate of another workspace (contain `package.workspace`).
615632
Most of the time workspaces will not need to be dealt with as `cargo new` and
616633
`cargo init` will handle workspace configuration automatically.
617634

635+
[globs]: https://docs.rs/glob/0.2.11/glob/struct.Pattern.html
636+
618637
#### Virtual Manifest
619638

620639
In workspace manifests, if the `package` table is present, the workspace root

0 commit comments

Comments
 (0)