Skip to content

Commit

Permalink
Read and write uv.lock based on project root (#3497)
Browse files Browse the repository at this point in the history
## Summary

The `uv.lock` location is no longer based on the current working
directory, but rather, on the discovered project name.
  • Loading branch information
charliermarsh committed May 9, 2024
1 parent 3dd34e2 commit d68b740
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
20 changes: 14 additions & 6 deletions crates/uv/src/commands/project/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub(crate) struct Project {
name: PackageName,
/// The path to the `pyproject.toml` file.
path: PathBuf,
/// The path to the project root.
root: PathBuf,
}

impl Project {
Expand Down Expand Up @@ -56,25 +58,31 @@ impl Project {
return Ok(Some(Self {
name,
path: pyproject_path,
root: ancestor.to_path_buf(),
}));
}
}

Ok(None)
}

/// Return the [`PackageName`] for the project.
pub(crate) fn name(&self) -> &PackageName {
&self.name
}

/// Return the root path for the project.
pub(crate) fn root(&self) -> &Path {
&self.root
}

/// Return the requirements for the project.
pub(crate) fn requirements(&self) -> Vec<RequirementsSource> {
vec![
RequirementsSource::from_requirements_file(self.path.clone()),
RequirementsSource::from_source_tree(self.path.parent().unwrap().to_path_buf()),
RequirementsSource::from_source_tree(self.root.clone()),
]
}

/// Return the [`PackageName`] for the project.
pub(crate) fn name(&self) -> &PackageName {
&self.name
}
}

/// A pyproject.toml as specified in PEP 517.
Expand Down
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub(crate) async fn lock(
// Write the lockfile to disk.
let lock = resolution.lock()?;
let encoded = toml::to_string_pretty(&lock)?;
fs_err::tokio::write("uv.lock", encoded.as_bytes()).await?;
fs_err::tokio::write(project.root().join("uv.lock"), encoded.as_bytes()).await?;

Ok(ExitStatus::Success)
}
2 changes: 1 addition & 1 deletion crates/uv/src/commands/project/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub(crate) async fn sync(

// Read the lockfile.
let resolution = {
let encoded = fs_err::tokio::read_to_string("uv.lock").await?;
let encoded = fs_err::tokio::read_to_string(project.root().join("uv.lock")).await?;
let lock: Lock = toml::from_str(&encoded)?;
lock.to_resolution(markers, tags, project.name())
};
Expand Down

0 comments on commit d68b740

Please sign in to comment.