Skip to content

Commit c8595e8

Browse files
committed
Auto merge of #7680 - stefanhoelzl:issue-7183, r=ehuss
include dotfiles in packages This PR solves #7183 It changes the behavior of `cargo package` to also include dotfiles by default. It should be discussed if this is intended or if the implementation should be changed to only include dotfiles which are specified in the `include` section. From the [existing comment](https://github.com/stefanhoelzl/cargo/blob/40885dfab40a1bf62b22aa03f732ef45163c013f/src/cargo/sources/path.rs#L358) it is a little bit unclear to me, but I supposed it was intended only to exclude directories starting with a dot?
2 parents 662a965 + 5efda6a commit c8595e8

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

src/cargo/sources/path.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ impl<'cfg> PathSource<'cfg> {
147147
if let Some(result) = self.discover_git_and_list_files(pkg, root, &mut filter) {
148148
return result;
149149
}
150+
// no include option and not git repo discovered (see rust-lang/cargo#7183).
151+
return self.list_files_walk_except_dot_files_and_dirs(pkg, &mut filter);
150152
}
151153
self.list_files_walk(pkg, &mut filter)
152154
}
@@ -329,6 +331,28 @@ impl<'cfg> PathSource<'cfg> {
329331
}
330332
}
331333

334+
fn list_files_walk_except_dot_files_and_dirs(
335+
&self,
336+
pkg: &Package,
337+
filter: &mut dyn FnMut(&Path) -> CargoResult<bool>,
338+
) -> CargoResult<Vec<PathBuf>> {
339+
let root = pkg.root();
340+
let mut exclude_dot_files_dir_builder = GitignoreBuilder::new(root);
341+
exclude_dot_files_dir_builder.add_line(None, ".*")?;
342+
let ignore_dot_files_and_dirs = exclude_dot_files_dir_builder.build()?;
343+
344+
let mut filter_ignore_dot_files_and_dirs = |path: &Path| -> CargoResult<bool> {
345+
let relative_path = path.strip_prefix(root)?;
346+
match ignore_dot_files_and_dirs
347+
.matched_path_or_any_parents(relative_path, /* is_dir */ false)
348+
{
349+
Match::Ignore(_) => Ok(false),
350+
_ => filter(path),
351+
}
352+
};
353+
self.list_files_walk(pkg, &mut filter_ignore_dot_files_and_dirs)
354+
}
355+
332356
fn list_files_walk(
333357
&self,
334358
pkg: &Package,
@@ -368,10 +392,6 @@ impl<'cfg> PathSource<'cfg> {
368392
entries.sort_unstable_by(|a, b| a.as_os_str().cmp(b.as_os_str()));
369393
for path in entries {
370394
let name = path.file_name().and_then(|s| s.to_str());
371-
// Skip dotfile directories.
372-
if name.map(|s| s.starts_with('.')) == Some(true) {
373-
continue;
374-
}
375395
if is_root && name == Some("target") {
376396
// Skip Cargo artifacts.
377397
continue;

tests/testsuite/package.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,12 @@ fn include() {
425425
version = "0.0.1"
426426
authors = []
427427
exclude = ["*.txt"]
428-
include = ["foo.txt", "**/*.rs", "Cargo.toml"]
428+
include = ["foo.txt", "**/*.rs", "Cargo.toml", ".dotfile"]
429429
"#,
430430
)
431431
.file("foo.txt", "")
432432
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
433+
.file(".dotfile", "")
433434
// Should be ignored when packaging.
434435
.file("src/bar.txt", "")
435436
.build();
@@ -442,6 +443,7 @@ fn include() {
442443
See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for more info.
443444
[WARNING] both package.include and package.exclude are specified; the exclude list will be ignored
444445
[PACKAGING] foo v0.0.1 ([..])
446+
[ARCHIVING] .dotfile
445447
[ARCHIVING] Cargo.toml
446448
[ARCHIVING] foo.txt
447449
[ARCHIVING] src/main.rs
@@ -1420,3 +1422,26 @@ fn gitignore_negate() {
14201422
",
14211423
);
14221424
}
1425+
1426+
#[cargo_test]
1427+
fn exclude_dot_files_and_directories_by_default() {
1428+
include_exclude_test(
1429+
"[]",
1430+
"[]",
1431+
&["src/lib.rs", ".dotfile", ".dotdir/file"],
1432+
"Cargo.toml\n\
1433+
src/lib.rs\n\
1434+
",
1435+
);
1436+
1437+
include_exclude_test(
1438+
r#"["Cargo.toml", "src/lib.rs", ".dotfile", ".dotdir/file"]"#,
1439+
"[]",
1440+
&["src/lib.rs", ".dotfile", ".dotdir/file"],
1441+
".dotdir/file\n\
1442+
.dotfile\n\
1443+
Cargo.toml\n\
1444+
src/lib.rs\n\
1445+
",
1446+
);
1447+
}

0 commit comments

Comments
 (0)