From d68b740af70d2beba8088328c0d5de46ee42efa9 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 9 May 2024 15:54:10 -0400 Subject: [PATCH] Read and write `uv.lock` based on project root (#3497) ## Summary The `uv.lock` location is no longer based on the current working directory, but rather, on the discovered project name. --- crates/uv/src/commands/project/discovery.rs | 20 ++++++++++++++------ crates/uv/src/commands/project/lock.rs | 2 +- crates/uv/src/commands/project/sync.rs | 2 +- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/crates/uv/src/commands/project/discovery.rs b/crates/uv/src/commands/project/discovery.rs index ea1d6bf3af8..77a5625ef27 100644 --- a/crates/uv/src/commands/project/discovery.rs +++ b/crates/uv/src/commands/project/discovery.rs @@ -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 { @@ -56,6 +58,7 @@ impl Project { return Ok(Some(Self { name, path: pyproject_path, + root: ancestor.to_path_buf(), })); } } @@ -63,18 +66,23 @@ impl Project { 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 { 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. diff --git a/crates/uv/src/commands/project/lock.rs b/crates/uv/src/commands/project/lock.rs index a89c155d3a4..cd0b61ee3dc 100644 --- a/crates/uv/src/commands/project/lock.rs +++ b/crates/uv/src/commands/project/lock.rs @@ -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) } diff --git a/crates/uv/src/commands/project/sync.rs b/crates/uv/src/commands/project/sync.rs index bc27cd54449..142250ea7ba 100644 --- a/crates/uv/src/commands/project/sync.rs +++ b/crates/uv/src/commands/project/sync.rs @@ -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()) };