Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only filter out target if its in the package root #12944

Merged
merged 5 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/cargo/sources/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
92 changes: 92 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,94 @@ 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("target_uncommitted", |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();

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.
let p = git::new("target_committed", |p| {
init_and_add_inner_target(p).file("target/foo.txt", "")
});
linyihai marked this conversation as resolved.
Show resolved Hide resolved
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", "")
}