Skip to content

Commit

Permalink
Auto merge of #12944 - linyihai:issue_12790, r=weihanglo
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Nov 14, 2023
2 parents 360059e + b2b026b commit dd94e9d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/cargo/sources/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,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
Expand Down
91 changes: 91 additions & 0 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -3132,3 +3133,93 @@ 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_and_add_inner_target(project())
.file("target/foo.txt", "")
.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, repo) = git::new_repo("foo", |p| init_and_add_inner_target(p));
// add target folder but not committed.
_ = fs::create_dir(p.build_dir()).unwrap();
_ = fs::write(p.build_dir().join("foo.txt"), "").unwrap();
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();

// if target is committed, it should be include.
git::add(&repo);
git::commit(&repo);
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
target/foo.txt
",
)
.run();
}

fn init_and_add_inner_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"); }"#)
// 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", "")
}

0 comments on commit dd94e9d

Please sign in to comment.