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

Ignore broken but excluded file during traversing #11008

Merged
merged 3 commits into from
Aug 21, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
18 changes: 11 additions & 7 deletions src/cargo/sources/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl<'cfg> PathSource<'cfg> {
}
};

let mut filter = |path: &Path, is_dir: bool| {
let filter = |path: &Path, is_dir: bool| {
let relative_path = match path.strip_prefix(root) {
Ok(p) => p,
Err(_) => return false,
Expand All @@ -169,10 +169,10 @@ impl<'cfg> PathSource<'cfg> {
// Attempt Git-prepopulate only if no `include` (see rust-lang/cargo#4135).
if no_include_option {
if let Some(repo) = git_repo {
return self.list_files_git(pkg, &repo, &mut filter);
return self.list_files_git(pkg, &repo, &filter);
}
}
self.list_files_walk(pkg, &mut filter)
self.list_files_walk(pkg, &filter)
}

/// Returns `Some(git2::Repository)` if found sibling `Cargo.toml` and `.git`
Expand Down Expand Up @@ -222,7 +222,7 @@ impl<'cfg> PathSource<'cfg> {
&self,
pkg: &Package,
repo: &git2::Repository,
filter: &mut dyn FnMut(&Path, bool) -> bool,
filter: &dyn Fn(&Path, bool) -> bool,
) -> CargoResult<Vec<PathBuf>> {
warn!("list_files_git {}", pkg.package_id());
let index = repo.index()?;
Expand Down Expand Up @@ -376,7 +376,7 @@ impl<'cfg> PathSource<'cfg> {
fn list_files_walk(
&self,
pkg: &Package,
filter: &mut dyn FnMut(&Path, bool) -> bool,
filter: &dyn Fn(&Path, bool) -> bool,
) -> CargoResult<Vec<PathBuf>> {
let mut ret = Vec::new();
self.walk(pkg.root(), &mut ret, true, filter)?;
Expand All @@ -388,7 +388,7 @@ impl<'cfg> PathSource<'cfg> {
path: &Path,
ret: &mut Vec<PathBuf>,
is_root: bool,
filter: &mut dyn FnMut(&Path, bool) -> bool,
filter: &dyn Fn(&Path, bool) -> bool,
) -> CargoResult<()> {
let walkdir = WalkDir::new(path)
.follow_links(true)
Expand Down Expand Up @@ -432,7 +432,11 @@ impl<'cfg> PathSource<'cfg> {
self.config.shell().warn(err)?;
}
Err(err) => match err.path() {
// If the error occurs with a path, simply recover from it.
// If an error occurs with a path, filter it again.
// If it is excluded, Just ignore it in this case.
// See issue rust-lang/cargo#10917
Some(path) if !filter(path, path.is_dir()) => {}
// Otherwise, simply recover from it.
// Don't worry about error skipping here, the callers would
// still hit the IO error if they do access it thereafter.
Some(path) => ret.push(path.to_path_buf()),
Expand Down
49 changes: 49 additions & 0 deletions tests/testsuite/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,55 @@ Caused by:
.run();
}

#[cargo_test]
/// Tests if a broken but excluded symlink is ignored.
/// See issue rust-lang/cargo#10917
///
/// This test requires you to be able to make symlinks.
/// For windows, this may require you to enable developer mode.
fn broken_but_excluded_symlink() {
#[cfg(unix)]
use std::os::unix::fs::symlink;
#[cfg(windows)]
use std::os::windows::fs::symlink_dir as symlink;

if !symlink_supported() {
return;
}

let p = project()
.file(
"Cargo.toml",
r#"
[project]
name = "foo"
version = "0.0.1"
authors = []
license = "MIT"
description = 'foo'
documentation = 'foo'
homepage = 'foo'
repository = 'foo'
exclude = ["src/foo.rs"]
"#,
)
.file("src/main.rs", r#"fn main() { println!("hello"); }"#)
.build();
t!(symlink("nowhere", &p.root().join("src/foo.rs")));

p.cargo("package -v --list")
// `src/foo.rs` is excluded.
.with_stdout(
"\
Cargo.lock
Cargo.toml
Cargo.toml.orig
src/main.rs
",
)
.run();
}

#[cargo_test]
#[cfg(not(windows))] // https://github.com/libgit2/libgit2/issues/6250
/// Test that /dir and /dir/ matches symlinks to directories.
Expand Down