From ef38ccf5c52647d0e85ee89f3e6f4ffa359bf91e Mon Sep 17 00:00:00 2001 From: Zanie Blue Date: Thu, 7 Nov 2024 14:06:40 -0600 Subject: [PATCH] Only write `.python-version` files during `uv init` for workspace members if the version differs (#8897) We'll read these from the workspace root anyway! --- crates/uv/src/commands/project/init.rs | 144 ++++++++++++------------- 1 file changed, 67 insertions(+), 77 deletions(-) diff --git a/crates/uv/src/commands/project/init.rs b/crates/uv/src/commands/project/init.rs index b7ccfe0cee3a..3560ad54b117 100644 --- a/crates/uv/src/commands/project/init.rs +++ b/crates/uv/src/commands/project/init.rs @@ -519,19 +519,16 @@ async fn init_project( (requires_python, python_request) }; - project_kind - .init( - name, - path, - &requires_python, - python_request.as_ref(), - vcs, - build_backend, - author_from, - no_readme, - package, - ) - .await?; + project_kind.init( + name, + path, + &requires_python, + vcs, + build_backend, + author_from, + no_readme, + package, + )?; if let Some(workspace) = workspace { if workspace.excludes(path)? { @@ -571,6 +568,40 @@ async fn init_project( workspace.install_path().simplified_display().cyan() )?; } + // Write .python-version if it doesn't exist in the workspace or if the version differs + if let Some(python_request) = python_request { + if PythonVersionFile::discover(path, &VersionFileDiscoveryOptions::default()) + .await? + .filter(|file| { + file.version() + .is_some_and(|version| *version == python_request) + && file.path().parent().is_some_and(|parent| { + parent == workspace.install_path() || parent == path + }) + }) + .is_none() + { + PythonVersionFile::new(path.join(".python-version")) + .with_versions(vec![python_request.clone()]) + .write() + .await?; + } + } + } else { + // Write .python-version if it doesn't exist in the project directory. + if let Some(python_request) = python_request { + if PythonVersionFile::discover(path, &VersionFileDiscoveryOptions::default()) + .await? + .filter(|file| file.version().is_some()) + .filter(|file| file.path().parent().is_some_and(|parent| parent == path)) + .is_none() + { + PythonVersionFile::new(path.join(".python-version")) + .with_versions(vec![python_request.clone()]) + .write() + .await?; + } + } } Ok(()) @@ -610,12 +641,11 @@ impl InitKind { impl InitProjectKind { /// Initialize this project kind at the target path. - async fn init( + fn init( self, name: &PackageName, path: &Path, requires_python: &RequiresPython, - python_request: Option<&PythonRequest>, vcs: Option, build_backend: Option, author_from: Option, @@ -623,44 +653,34 @@ impl InitProjectKind { package: bool, ) -> Result<()> { match self { - InitProjectKind::Application => { - self.init_application( - name, - path, - requires_python, - python_request, - vcs, - build_backend, - author_from, - no_readme, - package, - ) - .await - } - InitProjectKind::Library => { - self.init_library( - name, - path, - requires_python, - python_request, - vcs, - build_backend, - author_from, - no_readme, - package, - ) - .await - } + InitProjectKind::Application => InitProjectKind::init_application( + name, + path, + requires_python, + vcs, + build_backend, + author_from, + no_readme, + package, + ), + InitProjectKind::Library => InitProjectKind::init_library( + name, + path, + requires_python, + vcs, + build_backend, + author_from, + no_readme, + package, + ), } } /// Initialize a Python application at the target path. - async fn init_application( - self, + fn init_application( name: &PackageName, path: &Path, requires_python: &RequiresPython, - python_request: Option<&PythonRequest>, vcs: Option, build_backend: Option, author_from: Option, @@ -716,21 +736,6 @@ impl InitProjectKind { } fs_err::write(path.join("pyproject.toml"), pyproject)?; - // Write .python-version if it doesn't exist in the target or is empty. - if let Some(python_request) = python_request { - if PythonVersionFile::discover(path, &VersionFileDiscoveryOptions::default()) - .await? - .filter(|file| file.version().is_some()) - .filter(|file| file.path().parent().is_some_and(|parent| parent == path)) - .is_none() - { - PythonVersionFile::new(path.join(".python-version")) - .with_versions(vec![python_request.clone()]) - .write() - .await?; - } - } - // Initialize the version control system. init_vcs(path, vcs)?; @@ -738,12 +743,10 @@ impl InitProjectKind { } /// Initialize a library project at the target path. - async fn init_library( - self, + fn init_library( name: &PackageName, path: &Path, requires_python: &RequiresPython, - python_request: Option<&PythonRequest>, vcs: Option, build_backend: Option, author_from: Option, @@ -772,19 +775,6 @@ impl InitProjectKind { // Generate `src` files generate_package_scripts(name, path, build_backend, true)?; - // Write .python-version if it doesn't exist. - if let Some(python_request) = python_request { - if PythonVersionFile::discover(path, &VersionFileDiscoveryOptions::default()) - .await? - .is_none() - { - PythonVersionFile::new(path.join(".python-version")) - .with_versions(vec![python_request.clone()]) - .write() - .await?; - } - } - // Initialize the version control system. init_vcs(path, vcs)?;