From fb9aed346a620337599ec3d353394f02e1db2461 Mon Sep 17 00:00:00 2001 From: j178 <10510431+j178@users.noreply.github.com> Date: Wed, 24 Jul 2024 00:33:27 +0800 Subject: [PATCH] Skip comments and empty lines in python versions file --- crates/uv-python/src/version_files.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/crates/uv-python/src/version_files.rs b/crates/uv-python/src/version_files.rs index 4067d1b4dd23..7a1b5f740b94 100644 --- a/crates/uv-python/src/version_files.rs +++ b/crates/uv-python/src/version_files.rs @@ -56,7 +56,17 @@ async fn read_versions_file() -> Result>, std::io::Error> { match fs::tokio::read_to_string(PYTHON_VERSIONS_FILENAME).await { Ok(content) => { debug!("Reading requests from `{PYTHON_VERSIONS_FILENAME}`"); - Ok(Some(content.lines().map(ToString::to_string).collect())) + Ok(Some( + content + .lines() + .filter(|line| { + // Skip comments and empty lines. + let trimmed = line.trim(); + !(trimmed.is_empty() || trimmed.starts_with('#')) + }) + .map(ToString::to_string) + .collect(), + )) } Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(None), Err(err) => Err(err), @@ -67,7 +77,15 @@ async fn read_version_file() -> Result, std::io::Error> { match fs::tokio::read_to_string(PYTHON_VERSION_FILENAME).await { Ok(content) => { debug!("Reading requests from `{PYTHON_VERSION_FILENAME}`"); - Ok(content.lines().next().map(ToString::to_string)) + Ok(content + .lines() + // Skip comments and empty lines. + .find(|line| { + // Skip comments and empty lines. + let trimmed = line.trim(); + !(trimmed.is_empty() || trimmed.starts_with('#')) + }) + .map(ToString::to_string)) } Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(None), Err(err) => Err(err),