Skip to content

Commit

Permalink
Only write .python-version files during uv init for workspace mem…
Browse files Browse the repository at this point in the history
…bers if the version differs (#8897)

We'll read these from the workspace root anyway!
  • Loading branch information
zanieb committed Nov 7, 2024
1 parent e9dca7f commit ef38ccf
Showing 1 changed file with 67 additions and 77 deletions.
144 changes: 67 additions & 77 deletions crates/uv/src/commands/project/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)? {
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -610,57 +641,46 @@ 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<VersionControlSystem>,
build_backend: Option<ProjectBuildBackend>,
author_from: Option<AuthorFrom>,
no_readme: bool,
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<VersionControlSystem>,
build_backend: Option<ProjectBuildBackend>,
author_from: Option<AuthorFrom>,
Expand Down Expand Up @@ -716,34 +736,17 @@ 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)?;

Ok(())
}

/// 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<VersionControlSystem>,
build_backend: Option<ProjectBuildBackend>,
author_from: Option<AuthorFrom>,
Expand Down Expand Up @@ -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)?;

Expand Down

0 comments on commit ef38ccf

Please sign in to comment.