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

Canonicalize path_args src #7079

Closed
wants to merge 3 commits into from
Closed
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
14 changes: 14 additions & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ fn path_args(bcx: &BuildContext<'_, '_>, unit: &Unit<'_>) -> (PathBuf, PathBuf)
TargetSourcePath::Metabuild => unit.pkg.manifest().metabuild_path(bcx.ws.target_dir()),
};
assert!(src.is_absolute());
let src = canonicalize_path_arg(&src).unwrap_or(src);
if unit.pkg.package_id().source_id().is_path() {
if let Ok(path) = src.strip_prefix(ws_root) {
return (path.to_path_buf(), ws_root.to_path_buf());
Expand All @@ -696,6 +697,19 @@ fn path_args(bcx: &BuildContext<'_, '_>, unit: &Unit<'_>) -> (PathBuf, PathBuf)
(src, unit.pkg.root().to_path_buf())
}

#[cfg(windows)]
// On Windows cargo cannot read back the canonicalized paths. The paths are
// canonicalized to resolve symlinks, which aren't as ordinary on Windows as
// they are on other platforms, so we simply skip this.
pub fn canonicalize_path_arg<P: AsRef<Path>>(path: P) -> CargoResult<PathBuf> {
Ok(path.as_ref().to_path_buf())
}

#[cfg(not(windows))]
pub fn canonicalize_path_arg<P: AsRef<Path>>(path: P) -> CargoResult<PathBuf> {
Ok(fs::canonicalize(path)?)
}

fn add_path_args(bcx: &BuildContext<'_, '_>, unit: &Unit<'_>, cmd: &mut ProcessBuilder) {
let (arg, cwd) = path_args(bcx, unit);
cmd.arg(arg);
Expand Down
55 changes: 55 additions & 0 deletions tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4703,3 +4703,58 @@ d
)
.run();
}

#[cargo_test]
#[cfg(not(target_os = "windows"))]
fn symlink_rebuild() {
// Create a crate foo that depends on one crate: a.
// After the initial build we swap the 'a' directory for a symlink to 'b'.
// Since the underlying crate changed, cargo should perform a rebuild.
let project = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.1.0"
[dependencies]
a = { path = "a" }
"#,
)
.file("src/main.rs", "fn main(){}")
.file(
"a/Cargo.toml",
r#"
[package]
name = "a"
version = "0.1.0"
"#,
)
.file("a/src/lib.rs", "")
.file(
"b/Cargo.toml",
r#"
[package]
name = "a"
version = "0.1.0"
"#,
)
.file("b/src/lib.rs", "pub fn not_a(){}");

let foo = project.build();

foo.cargo("build -p foo")
.with_status(0)
.with_stderr_contains("[..] Compiling a v0.1.0 [..]")
.run();

let root = foo.root();

fs::remove_dir_all(root.join("a")).unwrap();
std::os::unix::fs::symlink(root.join("b"), root.join("a")).unwrap();

foo.cargo("build -p foo")
.with_status(0)
.with_stderr_contains("[..] Compiling a v0.1.0 [..]")
.run();
}