Skip to content

Fix cargo add overwriting symlinked Cargo.toml files #15281

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 12 additions & 4 deletions crates/cargo-util/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,22 @@ pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()>
/// Writes a file to disk atomically.
///
/// This uses `tempfile::persist` to accomplish atomic writes.
/// If the path is a symlink, it will follow the symlink and write to the actual target.
pub fn write_atomic<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Result<()> {
let path = path.as_ref();

// Check if the path is a symlink and follow it if it is
let actual_path = if path.is_symlink() {
std::fs::read_link(path)?
} else {
path.to_path_buf()
};

// On unix platforms, get the permissions of the original file. Copy only the user/group/other
// read/write/execute permission bits. The tempfile lib defaults to an initial mode of 0o600,
// and we'll set the proper permissions after creating the file.
#[cfg(unix)]
let perms = path.metadata().ok().map(|meta| {
let perms = actual_path.metadata().ok().map(|meta| {
use std::os::unix::fs::PermissionsExt;

// these constants are u16 on macOS
Expand All @@ -208,8 +216,8 @@ pub fn write_atomic<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Res
});

let mut tmp = TempFileBuilder::new()
.prefix(path.file_name().unwrap())
.tempfile_in(path.parent().unwrap())?;
.prefix(actual_path.file_name().unwrap())
.tempfile_in(actual_path.parent().unwrap())?;
tmp.write_all(contents.as_ref())?;

// On unix platforms, set the permissions on the newly created file. We can use fchmod (called
Expand All @@ -220,7 +228,7 @@ pub fn write_atomic<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> Res
tmp.as_file().set_permissions(perms)?;
}

tmp.persist(path)?;
tmp.persist(&actual_path)?;
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions tests/testsuite/cargo_add/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ mod script_bare;
mod script_frontmatter;
mod script_shebang;
mod sorted_table_with_dotted_item;
mod symlink;
mod target;
mod target_cfg;
mod unknown_inherited_feature;
Expand Down
51 changes: 51 additions & 0 deletions tests/testsuite/cargo_add/symlink.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use cargo_test_support::prelude::*;
use cargo_test_support::project;
use cargo_test_support::registry;
use std::fs;

#[cargo_test]
fn symlink_case() {
if !cargo_test_support::symlink_supported() {
return;
}

registry::init();
registry::Package::new("test-dep", "1.0.0").publish();

let project = project().file("src/lib.rs", "").build();

let target_dir = project.root().join("target_dir");
fs::create_dir_all(&target_dir).unwrap();

fs::copy(
project.root().join("Cargo.toml"),
target_dir.join("Cargo.toml"),
)
.unwrap();

fs::remove_file(project.root().join("Cargo.toml")).unwrap();

#[cfg(unix)]
{
use std::os::unix::fs::symlink;
symlink(
target_dir.join("Cargo.toml"),
project.root().join("Cargo.toml"),
)
.unwrap();
}

#[cfg(windows)]
{
use std::os::windows::fs::symlink_file;
symlink_file(
target_dir.join("Cargo.toml"),
project.root().join("Cargo.toml"),
)
.unwrap();
}

project.cargo("add test-dep").run();

assert!(project.root().join("Cargo.toml").is_symlink());
}
Loading