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

Avoid overwriting symlinks in pip compile output #6487

Merged
merged 1 commit into from
Aug 23, 2024
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
5 changes: 5 additions & 0 deletions crates/uv/src/commands/pip/compile.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::env;
use std::io::stdout;
use std::path::Path;
Expand Down Expand Up @@ -643,6 +644,10 @@ impl<'a> OutputWriter<'a> {
/// Commit the buffer to the output file.
async fn commit(self) -> std::io::Result<()> {
if let Some(output_file) = self.output_file {
// If the output file is an existing symlink, write to the destination instead.
let output_file = fs_err::read_link(output_file)
.map(Cow::Owned)
.unwrap_or(Cow::Borrowed(output_file));
let stream = anstream::adapter::strip_bytes(&self.buffer).into_vec();
uv_fs::write_atomic(output_file, &stream).await?;
}
Expand Down
51 changes: 51 additions & 0 deletions crates/uv/tests/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11907,3 +11907,54 @@ fn invalid_extra() -> Result<()> {

Ok(())
}

/// Respect symlinks of output files.
#[test]
#[cfg(not(windows))]
fn symlink() -> Result<()> {
let context = TestContext::new("3.8");
let requirements_in = context.temp_dir.child("requirements.in");
requirements_in.write_str("anyio")?;

// Create an output file.
let requirements_txt = context.temp_dir.child("requirements.txt");
requirements_txt.write_str("anyio")?;

// Create a symlink to the output file.
let symlink = context.temp_dir.child("requirements-symlink.txt");
symlink.symlink_to_file(requirements_txt.path())?;

// Write to the symlink.
uv_snapshot!(context.pip_compile()
.arg("requirements.in")
.arg("--output-file")
.arg("requirements-symlink.txt"), @r###"
success: true
exit_code: 0
----- stdout -----
# This file was autogenerated by uv via the following command:
# uv pip compile --cache-dir [CACHE_DIR] requirements.in --output-file requirements-symlink.txt
anyio==4.3.0
# via -r requirements.in
exceptiongroup==1.2.0
# via anyio
idna==3.6
# via anyio
sniffio==1.3.1
# via anyio
typing-extensions==4.10.0
# via anyio

----- stderr -----
Resolved 5 packages in [TIME]
"###
);

// The symlink should still be a symlink.
assert!(symlink.path().symlink_metadata()?.file_type().is_symlink());

// The destination of the symlink should be the same as the output file.
assert_eq!(symlink.path().read_link()?, requirements_txt.path());

Ok(())
}
Loading