Skip to content

Commit 9d161a2

Browse files
committed
Auto merge of #4378 - behnam:ignore, r=alexcrichton
[sources/path] Support leading slash in glob patterns Background: #4268 This diff takes us to **Stage 1.1** of the migration plan by allowing glob patterns to include a leading slash, so that glob patterns can be updated, if needed, to start with a slash, closer to the future behavior with gitignore-like matching. Why is this stage needed? It's common to have `package.include` set like this: ``` include = ["src/**"] ``` In old interpretation, this would only include all files under the `src` directory under the package root. With the new interpretation, this would match any path with some directory called `src`, even if it's not directly under the package root. After this patch, package owners can start marking glob patters with a leading slash to fix the warning thrown, if any. One thing to notice here is that there are no extra matchings, but, if a manifest has already a pattern with a leading slash, this would silently start matching it with the paths. I believe this is fine, since the old behavior would have been for the pattern to not match anything, therefore the item was useless. See also <#4377> for suggestion to throw warning on useless/invalid patterns in these fields.
2 parents d2e8587 + 5641cbd commit 9d161a2

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/cargo/sources/path.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,12 @@ impl<'cfg> PathSource<'cfg> {
110110
// glob-like matching rules
111111

112112
let glob_parse = |p: &String| {
113-
Pattern::new(p).map_err(|e| {
113+
let pattern: &str = if p.starts_with('/') {
114+
&p[1..p.len()]
115+
} else {
116+
&p
117+
};
118+
Pattern::new(pattern).map_err(|e| {
114119
CargoError::from(format!("could not parse glob pattern `{}`: {}", p, e))
115120
})
116121
};
@@ -128,15 +133,15 @@ impl<'cfg> PathSource<'cfg> {
128133
.collect::<Result<Vec<_>, _>>()?;
129134

130135
let glob_should_package = |relative_path: &Path| -> bool {
136+
fn glob_match(patterns: &Vec<Pattern>, relative_path: &Path) -> bool {
137+
patterns.iter().any(|pattern| pattern.matches_path(relative_path))
138+
}
139+
131140
// include and exclude options are mutually exclusive.
132141
if no_include_option {
133-
!glob_exclude.iter().any(|pattern| {
134-
pattern.matches_path(relative_path)
135-
})
142+
!glob_match(&glob_exclude, relative_path)
136143
} else {
137-
glob_include.iter().any(|pattern| {
138-
pattern.matches_path(relative_path)
139-
})
144+
glob_match(&glob_include, relative_path)
140145
}
141146
};
142147

@@ -197,7 +202,7 @@ impl<'cfg> PathSource<'cfg> {
197202
.shell()
198203
.warn(format!(
199204
"Pattern matching for Cargo's include/exclude fields is changing and \
200-
file `{}` WILL be excluded in the next Cargo version.\n\
205+
file `{}` WILL be excluded in a future Cargo version.\n\
201206
See https://github.com/rust-lang/cargo/issues/4268 for more info",
202207
relative_path.display()
203208
))?;
@@ -206,7 +211,7 @@ impl<'cfg> PathSource<'cfg> {
206211
.shell()
207212
.warn(format!(
208213
"Pattern matching for Cargo's include/exclude fields is changing and \
209-
file `{}` WILL NOT be included in the next Cargo version.\n\
214+
file `{}` WILL NOT be included in a future Cargo version.\n\
210215
See https://github.com/rust-lang/cargo/issues/4268 for more info",
211216
relative_path.display()
212217
))?;
@@ -217,7 +222,7 @@ impl<'cfg> PathSource<'cfg> {
217222
.shell()
218223
.warn(format!(
219224
"Pattern matching for Cargo's include/exclude fields is changing and \
220-
file `{}` WILL NOT be excluded in the next Cargo version.\n\
225+
file `{}` WILL NOT be excluded in a future Cargo version.\n\
221226
See https://github.com/rust-lang/cargo/issues/4268 for more info",
222227
relative_path.display()
223228
))?;
@@ -226,7 +231,7 @@ impl<'cfg> PathSource<'cfg> {
226231
.shell()
227232
.warn(format!(
228233
"Pattern matching for Cargo's include/exclude fields is changing and \
229-
file `{}` WILL be included in the next Cargo version.\n\
234+
file `{}` WILL be included in a future Cargo version.\n\
230235
See https://github.com/rust-lang/cargo/issues/4268 for more info",
231236
relative_path.display()
232237
))?;

tests/package.rs

-4
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,6 @@ See [..]
321321
See [..]
322322
[WARNING] [..] file `dir_root_3[/]some_dir[/]file` WILL be excluded [..]
323323
See [..]
324-
[WARNING] [..] file `file_root_2` WILL be excluded [..]
325-
See [..]
326324
[WARNING] [..] file `some_dir[/]dir_deep_1[/]some_dir[/]file` WILL be excluded [..]
327325
See [..]
328326
[WARNING] [..] file `some_dir[/]dir_deep_3[/]some_dir[/]file` WILL be excluded [..]
@@ -351,7 +349,6 @@ See [..]
351349
[ARCHIVING] [..]
352350
[ARCHIVING] [..]
353351
[ARCHIVING] [..]
354-
[ARCHIVING] [..]
355352
"));
356353

357354
assert_that(&p.root().join("target/package/foo-0.0.1.crate"), existing_file());
@@ -362,7 +359,6 @@ Cargo.toml
362359
dir_root_1[/]some_dir[/]file
363360
dir_root_2[/]some_dir[/]file
364361
dir_root_3[/]some_dir[/]file
365-
file_root_2
366362
file_root_3
367363
file_root_4
368364
file_root_5

0 commit comments

Comments
 (0)