Skip to content

Commit dd94e9d

Browse files
committed
Auto merge of #12944 - linyihai:issue_12790, r=weihanglo
Only filter out target if its in the package root ### What does this PR try to resolve? Only filter out target if its in the package root. Fixed #12790 ### How should we test and review this PR? Add two testcase in tests/testsuite/package.rs for this PR. - `include_files_called_target_project` testcase test the logic for none git repository. By the way, our PR was based on git repository, so this testcase was a little irrelevant. - `include_files_called_target_git` testcase was made for git repository. There are two cases here, one is the target in the uncommitted state, at this time should not be included, and one is the target in the committed state, at this time should be included ### Additional information
2 parents 360059e + b2b026b commit dd94e9d

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

src/cargo/sources/path.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,12 @@ impl<'cfg> PathSource<'cfg> {
328328

329329
match file_path.file_name().and_then(|s| s.to_str()) {
330330
// The `target` directory is never included.
331-
Some("target") => continue,
331+
Some("target") => {
332+
// Only filter out target if its in the package root.
333+
if file_path.parent().unwrap() == pkg_path {
334+
continue;
335+
}
336+
}
332337

333338
// Keep track of all sub-packages found and also strip out all
334339
// matches we've found so far. Note, though, that if we find

tests/testsuite/package.rs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use cargo_test_support::publish::validate_crate_contents;
55
use cargo_test_support::registry::{self, Package};
66
use cargo_test_support::{
77
basic_manifest, cargo_process, git, path2url, paths, project, symlink_supported, t,
8+
ProjectBuilder,
89
};
910
use flate2::read::GzDecoder;
1011
use std::fs::{self, read_to_string, File};
@@ -3132,3 +3133,93 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for
31323133
&[],
31333134
);
31343135
}
3136+
3137+
#[cargo_test]
3138+
fn include_files_called_target_project() {
3139+
// https://github.com/rust-lang/cargo/issues/12790
3140+
// files and folders called "target" should be included, unless they're the actual target directory
3141+
let p = init_and_add_inner_target(project())
3142+
.file("target/foo.txt", "")
3143+
.build();
3144+
3145+
p.cargo("package -l")
3146+
.with_stdout(
3147+
"\
3148+
Cargo.lock
3149+
Cargo.toml
3150+
Cargo.toml.orig
3151+
data/not_target
3152+
data/target
3153+
derp/not_target/foo.txt
3154+
derp/target/foo.txt
3155+
src/main.rs
3156+
",
3157+
)
3158+
.run();
3159+
}
3160+
3161+
#[cargo_test]
3162+
fn include_files_called_target_git() {
3163+
// https://github.com/rust-lang/cargo/issues/12790
3164+
// files and folders called "target" should be included, unless they're the actual target directory
3165+
let (p, repo) = git::new_repo("foo", |p| init_and_add_inner_target(p));
3166+
// add target folder but not committed.
3167+
_ = fs::create_dir(p.build_dir()).unwrap();
3168+
_ = fs::write(p.build_dir().join("foo.txt"), "").unwrap();
3169+
p.cargo("package -l")
3170+
.with_stdout(
3171+
"\
3172+
.cargo_vcs_info.json
3173+
Cargo.lock
3174+
Cargo.toml
3175+
Cargo.toml.orig
3176+
data/not_target
3177+
data/target
3178+
derp/not_target/foo.txt
3179+
derp/target/foo.txt
3180+
src/main.rs
3181+
",
3182+
)
3183+
.run();
3184+
3185+
// if target is committed, it should be include.
3186+
git::add(&repo);
3187+
git::commit(&repo);
3188+
p.cargo("package -l")
3189+
.with_stdout(
3190+
"\
3191+
.cargo_vcs_info.json
3192+
Cargo.lock
3193+
Cargo.toml
3194+
Cargo.toml.orig
3195+
data/not_target
3196+
data/target
3197+
derp/not_target/foo.txt
3198+
derp/target/foo.txt
3199+
src/main.rs
3200+
target/foo.txt
3201+
",
3202+
)
3203+
.run();
3204+
}
3205+
3206+
fn init_and_add_inner_target(p: ProjectBuilder) -> ProjectBuilder {
3207+
p.file(
3208+
"Cargo.toml",
3209+
r#"
3210+
[package]
3211+
name = "foo"
3212+
version = "0.0.1"
3213+
authors = []
3214+
license = "MIT"
3215+
description = "foo"
3216+
"#,
3217+
)
3218+
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
3219+
// file called target, should be included
3220+
.file("data/target", "")
3221+
.file("data/not_target", "")
3222+
// folder called target, should be included
3223+
.file("derp/target/foo.txt", "")
3224+
.file("derp/not_target/foo.txt", "")
3225+
}

0 commit comments

Comments
 (0)