From eebed13f468aeb4405b8adb1ba91e8d8b2a7c6c1 Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Thu, 9 Nov 2023 13:55:48 +0800 Subject: [PATCH 1/5] Only filter out target if its in the package root --- src/cargo/sources/path.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/cargo/sources/path.rs b/src/cargo/sources/path.rs index 0cc6399766e..7ba7c3692a7 100644 --- a/src/cargo/sources/path.rs +++ b/src/cargo/sources/path.rs @@ -327,7 +327,12 @@ impl<'cfg> PathSource<'cfg> { match file_path.file_name().and_then(|s| s.to_str()) { // The `target` directory is never included. - Some("target") => continue, + Some("target") => { + // Only filter out target if its in the package root. + if file_path.parent().unwrap() == pkg_path { + continue; + } + } // Keep track of all sub-packages found and also strip out all // matches we've found so far. Note, though, that if we find From 2c503f89c4ec322c69722f69804470ca8e97383b Mon Sep 17 00:00:00 2001 From: Karel Peeters Date: Thu, 9 Nov 2023 16:09:30 +0100 Subject: [PATCH 2/5] Add unit test for files called target. https://github.com/rust-lang/cargo/issues/12790 --- tests/testsuite/package.rs | 71 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 4ec4fc0d6fc..434089f3957 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -5,6 +5,7 @@ use cargo_test_support::publish::validate_crate_contents; use cargo_test_support::registry::{self, Package}; use cargo_test_support::{ basic_manifest, cargo_process, git, path2url, paths, project, symlink_supported, t, + ProjectBuilder, }; use flate2::read::GzDecoder; use std::fs::{self, read_to_string, File}; @@ -3132,3 +3133,73 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for &[], ); } + +#[cargo_test] +fn include_files_called_target_project() { + // https://github.com/rust-lang/cargo/issues/12790 + // files and folders called "target" should be included, unless they're the actual target directory + + let p = init_project_files_called_target(project()).build(); + + p.cargo("package -l") + .with_stdout( + "\ +Cargo.lock +Cargo.toml +Cargo.toml.orig +data/not_target +data/target +derp/not_target/foo.txt +derp/target/foo.txt +src/main.rs +", + ) + .run(); +} + +#[cargo_test] +fn include_files_called_target_git() { + // https://github.com/rust-lang/cargo/issues/12790 + // files and folders called "target" should be included, unless they're the actual target directory + + let p = git::new("all", |p| init_project_files_called_target(p)); + + p.cargo("package -l") + .with_stdout( + "\ +.cargo_vcs_info.json +Cargo.lock +Cargo.toml +Cargo.toml.orig +data/not_target +data/target +derp/not_target/foo.txt +derp/target/foo.txt +src/main.rs +", + ) + .run(); +} + +fn init_project_files_called_target(p: ProjectBuilder) -> ProjectBuilder { + p.file( + "Cargo.toml", + r#" + [package] + name = "foo" + version = "0.0.1" + authors = [] + license = "MIT" + description = "foo" + "#, + ) + .file("src/main.rs", r#"fn main() { println!("hello"); }"#) + // actual target dir, should be excluded + .file("target/foo.txt", "") + // file called target, should be included + .file("data/target", "") + .file("data/not_target", "") + // folder called target, should be included + .file("derp/target/foo.txt", "") + .file("derp/not_target/foo.txt", "") +} From 4577c0ec6188c3e308d4cef82e4e6cfbfbf50096 Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Fri, 10 Nov 2023 14:22:03 +0800 Subject: [PATCH 3/5] Correct the situation with git repository test cases --- tests/testsuite/package.rs | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 434089f3957..03138bb6f39 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -4,7 +4,7 @@ use cargo_test_support::paths::CargoPathExt; use cargo_test_support::publish::validate_crate_contents; use cargo_test_support::registry::{self, Package}; use cargo_test_support::{ - basic_manifest, cargo_process, git, path2url, paths, project, symlink_supported, t, + basic_manifest, cargo_process, git, path2url, paths, project, project_in, symlink_supported, t, ProjectBuilder, }; use flate2::read::GzDecoder; @@ -3138,8 +3138,9 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for fn include_files_called_target_project() { // https://github.com/rust-lang/cargo/issues/12790 // files and folders called "target" should be included, unless they're the actual target directory - - let p = init_project_files_called_target(project()).build(); + let p = init_and_add_inner_target(project()) + .file("target/foo.txt", "") + .build(); p.cargo("package -l") .with_stdout( @@ -3161,9 +3162,29 @@ src/main.rs fn include_files_called_target_git() { // https://github.com/rust-lang/cargo/issues/12790 // files and folders called "target" should be included, unless they're the actual target directory + let (p, repo) = git::new_repo("all", |p| init_and_add_inner_target(p)); + // add target folder but not committed. + let _ = project_in(&repo.path().display().to_string()).file("target/foo.txt", ""); + p.cargo("package -l") + .with_stdout( + "\ +.cargo_vcs_info.json +Cargo.lock +Cargo.toml +Cargo.toml.orig +data/not_target +data/target +derp/not_target/foo.txt +derp/target/foo.txt +src/main.rs +", + ) + .run(); - let p = git::new("all", |p| init_project_files_called_target(p)); - + // if target is committed, it should be include. + let p = git::new("all", |p| { + init_and_add_inner_target(p).file("target/foo.txt", "") + }); p.cargo("package -l") .with_stdout( "\ @@ -3176,12 +3197,13 @@ data/target derp/not_target/foo.txt derp/target/foo.txt src/main.rs +target/foo.txt ", ) .run(); } -fn init_project_files_called_target(p: ProjectBuilder) -> ProjectBuilder { +fn init_and_add_inner_target(p: ProjectBuilder) -> ProjectBuilder { p.file( "Cargo.toml", r#" @@ -3194,8 +3216,6 @@ fn init_project_files_called_target(p: ProjectBuilder) -> ProjectBuilder { "#, ) .file("src/main.rs", r#"fn main() { println!("hello"); }"#) - // actual target dir, should be excluded - .file("target/foo.txt", "") // file called target, should be included .file("data/target", "") .file("data/not_target", "") From 722d8f1c1d31347f405b01ee3fe99950180384d4 Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Fri, 10 Nov 2023 15:49:44 +0800 Subject: [PATCH 4/5] Add target folder but not committed --- tests/testsuite/package.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 03138bb6f39..6c341d8154f 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -4,7 +4,7 @@ use cargo_test_support::paths::CargoPathExt; use cargo_test_support::publish::validate_crate_contents; use cargo_test_support::registry::{self, Package}; use cargo_test_support::{ - basic_manifest, cargo_process, git, path2url, paths, project, project_in, symlink_supported, t, + basic_manifest, cargo_process, git, path2url, paths, project, symlink_supported, t, ProjectBuilder, }; use flate2::read::GzDecoder; @@ -3162,9 +3162,10 @@ src/main.rs fn include_files_called_target_git() { // https://github.com/rust-lang/cargo/issues/12790 // files and folders called "target" should be included, unless they're the actual target directory - let (p, repo) = git::new_repo("all", |p| init_and_add_inner_target(p)); + let (p, repo) = git::new_repo("target_uncommitted", |p| init_and_add_inner_target(p)); // add target folder but not committed. - let _ = project_in(&repo.path().display().to_string()).file("target/foo.txt", ""); + let _ = fs::create_dir_all(&repo.workdir().unwrap().join("target/foo.txt")).unwrap(); + p.cargo("package -l") .with_stdout( "\ @@ -3182,7 +3183,7 @@ src/main.rs .run(); // if target is committed, it should be include. - let p = git::new("all", |p| { + let p = git::new("target_committed", |p| { init_and_add_inner_target(p).file("target/foo.txt", "") }); p.cargo("package -l") From b2b026bfbbd04ae72372756821ddfa8e976d11fe Mon Sep 17 00:00:00 2001 From: Lin Yihai Date: Tue, 14 Nov 2023 11:24:57 +0800 Subject: [PATCH 5/5] Improve testcase `include_files_called_target_git` --- tests/testsuite/package.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 6c341d8154f..2e7c05a342d 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -3162,10 +3162,10 @@ src/main.rs fn include_files_called_target_git() { // https://github.com/rust-lang/cargo/issues/12790 // files and folders called "target" should be included, unless they're the actual target directory - let (p, repo) = git::new_repo("target_uncommitted", |p| init_and_add_inner_target(p)); + let (p, repo) = git::new_repo("foo", |p| init_and_add_inner_target(p)); // add target folder but not committed. - let _ = fs::create_dir_all(&repo.workdir().unwrap().join("target/foo.txt")).unwrap(); - + _ = fs::create_dir(p.build_dir()).unwrap(); + _ = fs::write(p.build_dir().join("foo.txt"), "").unwrap(); p.cargo("package -l") .with_stdout( "\ @@ -3183,9 +3183,8 @@ src/main.rs .run(); // if target is committed, it should be include. - let p = git::new("target_committed", |p| { - init_and_add_inner_target(p).file("target/foo.txt", "") - }); + git::add(&repo); + git::commit(&repo); p.cargo("package -l") .with_stdout( "\