Skip to content

Commit 510370a

Browse files
committed
don't copy /target directory when building local packages
This can save multiple gigabytes, especially when the files are then saved into a persistent database (like for docs.rs)
1 parent 8da6c44 commit 510370a

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

src/crates/local.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ fn copy_dir(src: &Path, dest: &Path) -> Result<(), Error> {
4848
let dest = crate::utils::normalize_path(dest);
4949

5050
let src_components = src.components().count();
51-
for entry in WalkDir::new(&src) {
51+
let mut entries = WalkDir::new(&src).into_iter();
52+
while let Some(entry) = entries.next() {
5253
let entry = entry?;
5354

5455
let mut components = entry.path().components();
@@ -58,7 +59,13 @@ fn copy_dir(src: &Path, dest: &Path) -> Result<(), Error> {
5859
let path = components.as_path();
5960

6061
if entry.file_type().is_dir() {
61-
std::fs::create_dir_all(dest.join(path))?;
62+
// don't copy /target directory
63+
if entry.file_name() == "target" && entry.depth() == 1 {
64+
info!("ignoring top-level target directory {}", path.display());
65+
entries.skip_current_dir();
66+
} else {
67+
std::fs::create_dir_all(dest.join(path))?;
68+
}
6269
} else {
6370
std::fs::copy(src.join(path), dest.join(path))?;
6471
}
@@ -90,4 +97,24 @@ mod tests {
9097

9198
Ok(())
9299
}
100+
101+
#[test]
102+
fn test_no_copy_target() -> Result<(), Error> {
103+
use log::debug;
104+
env_logger::init();
105+
106+
debug!("debugging works");
107+
let (src, dest) = (tempfile::tempdir()?, tempfile::tempdir()?);
108+
debug!("made root dirs");
109+
std::fs::create_dir(src.path().join("target"))?;
110+
std::fs::write(src.path().join("target").join("a.out"), b"this is not actually an ELF file")?;
111+
debug!("made subdirs and files");
112+
113+
super::copy_dir(src.path(), dest.path())?;
114+
debug!("copied");
115+
116+
assert!(!dest.path().join("target").exists());
117+
118+
Ok(())
119+
}
93120
}

0 commit comments

Comments
 (0)