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

Use copy-on-write when normalizing paths #9710

Merged
merged 1 commit into from
Dec 7, 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
15 changes: 7 additions & 8 deletions crates/uv-distribution/src/metadata/lowering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ impl LoweredRequirement {
let source = if let Some(git_member) = &git_member {
// If the workspace comes from a Git dependency, all workspace
// members need to be Git dependencies, too.
let subdirectory = uv_fs::normalize_path(
&uv_fs::relative_to(member.root(), git_member.fetch_root)
.expect("Workspace member must be relative"),
);
let subdirectory =
uv_fs::relative_to(member.root(), git_member.fetch_root)
.expect("Workspace member must be relative");
let subdirectory = uv_fs::normalize_path_buf(subdirectory);
RequirementSource::Git {
repository: git_member.git_source.git.repository().clone(),
reference: git_member.git_source.git.reference().clone(),
Expand Down Expand Up @@ -711,10 +711,9 @@ fn path_source(
};
if is_dir {
if let Some(git_member) = git_member {
let subdirectory = uv_fs::normalize_path(
&uv_fs::relative_to(install_path, git_member.fetch_root)
.expect("Workspace member must be relative"),
);
let subdirectory = uv_fs::relative_to(install_path, git_member.fetch_root)
.expect("Workspace member must be relative");
let subdirectory = uv_fs::normalize_path_buf(subdirectory);
return Ok(RequirementSource::Git {
repository: git_member.git_source.git.repository().clone(),
reference: git_member.git_source.git.reference().clone(),
Expand Down
31 changes: 28 additions & 3 deletions crates/uv-fs/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,33 @@ pub fn normalize_absolute_path(path: &Path) -> Result<PathBuf, std::io::Error> {
Ok(ret)
}

/// Normalize a path, removing things like `.` and `..`.
/// Normalize a [`Path`], removing things like `.` and `..`.
pub fn normalize_path(path: &Path) -> Cow<Path> {
// Fast path: if the path is already normalized, return it as-is.
if path.components().all(|component| match component {
Component::Prefix(_) | Component::RootDir | Component::Normal(_) => true,
Component::ParentDir | Component::CurDir => false,
}) {
Cow::Borrowed(path)
} else {
Cow::Owned(normalized(path))
}
}

/// Normalize a [`PathBuf`], removing things like `.` and `..`.
pub fn normalize_path_buf(path: PathBuf) -> PathBuf {
// Fast path: if the path is already normalized, return it as-is.
if path.components().all(|component| match component {
Component::Prefix(_) | Component::RootDir | Component::Normal(_) => true,
Component::ParentDir | Component::CurDir => false,
}) {
path
} else {
normalized(&path)
}
}

/// Normalize a [`Path`].
///
/// Unlike [`normalize_absolute_path`], this works with relative paths and does never error.
///
Expand All @@ -216,8 +242,7 @@ pub fn normalize_absolute_path(path: &Path) -> Result<PathBuf, std::io::Error> {
/// Out: `workspace-git-path-dep-test/packages/d`
///
/// In: `./a/../../b`
/// Out: `../b`
pub fn normalize_path(path: &Path) -> PathBuf {
fn normalized(path: &Path) -> PathBuf {
let mut normalized = PathBuf::new();
for component in path.components() {
match component {
Expand Down
6 changes: 4 additions & 2 deletions crates/uv-resolver/src/lock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3749,7 +3749,8 @@ fn normalize_requirement(
ext,
url: _,
} => {
let install_path = uv_fs::normalize_path(&workspace.install_path().join(&install_path));
let install_path =
uv_fs::normalize_path_buf(workspace.install_path().join(&install_path));
let url = VerbatimUrl::from_absolute_path(&install_path)
.map_err(LockErrorKind::RequirementVerbatimUrl)?;

Expand All @@ -3772,7 +3773,8 @@ fn normalize_requirement(
r#virtual,
url: _,
} => {
let install_path = uv_fs::normalize_path(&workspace.install_path().join(&install_path));
let install_path =
uv_fs::normalize_path_buf(workspace.install_path().join(&install_path));
let url = VerbatimUrl::from_absolute_path(&install_path)
.map_err(LockErrorKind::RequirementVerbatimUrl)?;

Expand Down
3 changes: 1 addition & 2 deletions crates/uv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ async fn run(mut cli: Cli) -> Result<ExitStatus> {
.as_deref()
.map(std::path::absolute)
.transpose()?
.as_deref()
.map(uv_fs::normalize_path)
.map(uv_fs::normalize_path_buf)
.map(Cow::Owned)
.unwrap_or_else(|| Cow::Borrowed(&*CWD));

Expand Down
Loading