Skip to content

Commit 9391f97

Browse files
committed
Reject multi-dot pylock filenames (#20440)
Invalid names such as `pylock..toml` and `pylock.foo.bar.toml` are accepted even though the [`pylock.toml` filename specification](https://packaging.python.org/en/latest/specifications/pylock-toml/#file-name) permits only `pylock.toml` and single-name variants such as `pylock.dev.toml`. Validate the complete filename. This is breaking for existing dotted lock names, including files discovered by [Pipenv's `pylock.*.toml` glob](https://github.com/pypa/pipenv/blob/main/pipenv/utils/pylock.py#L746-L768); those files must be renamed.
1 parent 06bba12 commit 9391f97

6 files changed

Lines changed: 93 additions & 10 deletions

File tree

‎crates/uv-requirements/src/sources.rs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ impl RequirementsSource {
5959
.is_some_and(|ext| ext.eq_ignore_ascii_case("toml"))
6060
{
6161
Err(anyhow::anyhow!(
62-
"`{}` is not a valid PEP 751 filename: expected TOML file to start with `pylock.` and end with `.toml` (e.g., `pylock.toml`, `pylock.dev.toml`)",
62+
"`{}` is not a valid PEP 751 filename: expected `pylock.toml` or `pylock.<name>.toml`, where `<name>` is non-empty and contains no dots",
6363
path.user_display(),
6464
))
6565
} else if path
@@ -329,7 +329,13 @@ impl std::fmt::Display for RequirementsSource {
329329
}
330330

331331
/// Returns `true` if a file name matches the `pylock.toml` pattern defined in PEP 751.
332-
#[expect(clippy::case_sensitive_file_extension_comparisons)]
333332
pub fn is_pylock_toml(file_name: &str) -> bool {
334-
file_name.starts_with("pylock.") && file_name.ends_with(".toml")
333+
if file_name == "pylock.toml" {
334+
return true;
335+
}
336+
337+
file_name
338+
.strip_prefix("pylock.")
339+
.and_then(|name| name.strip_suffix(".toml"))
340+
.is_some_and(|name| !name.is_empty() && !name.contains('.'))
335341
}

‎crates/uv/src/commands/pip/compile.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ pub(crate) async fn pip_compile(
159159
{
160160
if !is_pylock_toml(file_name) {
161161
return Err(anyhow!(
162-
"Expected the output filename to start with `pylock.` and end with `.toml` (e.g., `pylock.toml`, `pylock.dev.toml`); `{file_name}` won't be recognized as a `pylock.toml` file in subsequent commands",
162+
"Expected the output filename to be `pylock.toml` or `pylock.<name>.toml`, where `<name>` is non-empty and contains no dots; found `{file_name}`",
163163
));
164164
}
165165
}

‎crates/uv/src/commands/project/export.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ pub(crate) async fn export(
360360
{
361361
if !is_pylock_toml(file_name) {
362362
return Err(anyhow!(
363-
"Expected the output filename to start with `pylock.` and end with `.toml` (e.g., `pylock.toml`, `pylock.dev.toml`); `{file_name}` won't be recognized as a `pylock.toml` file in subsequent commands",
363+
"Expected the output filename to be `pylock.toml` or `pylock.<name>.toml`, where `<name>` is non-empty and contains no dots; found `{file_name}`",
364364
));
365365
}
366366
}

‎crates/uv/tests/pip_compile/pip_compile.rs‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use flate2::write::GzEncoder;
1313
use fs_err::File;
1414
use futures::executor::block_on;
1515
use futures::io::AllowStdIo;
16+
#[cfg(feature = "test-python-managed")]
1617
use http::StatusCode;
1718
#[cfg(feature = "test-universal")]
1819
use indoc::formatdoc;
@@ -16951,7 +16952,33 @@ fn pep_751_filename() -> Result<()> {
1695116952
.arg("test.toml"), @"
1695216953
exit_code: 2 (failure)
1695316954
----- stderr -----
16954-
error: Expected the output filename to start with `pylock.` and end with `.toml` (e.g., `pylock.toml`, `pylock.dev.toml`); `test.toml` won't be recognized as a `pylock.toml` file in subsequent commands
16955+
error: Expected the output filename to be `pylock.toml` or `pylock.<name>.toml`, where `<name>` is non-empty and contains no dots; found `test.toml`
16956+
");
16957+
16958+
uv_snapshot!(context.filters(), context
16959+
.pip_compile()
16960+
.arg("requirements.txt")
16961+
.arg("--universal")
16962+
.arg("--format")
16963+
.arg("pylock.toml")
16964+
.arg("-o")
16965+
.arg("pylock..toml"), @"
16966+
exit_code: 2 (failure)
16967+
----- stderr -----
16968+
error: Expected the output filename to be `pylock.toml` or `pylock.<name>.toml`, where `<name>` is non-empty and contains no dots; found `pylock..toml`
16969+
");
16970+
16971+
uv_snapshot!(context.filters(), context
16972+
.pip_compile()
16973+
.arg("requirements.txt")
16974+
.arg("--universal")
16975+
.arg("--format")
16976+
.arg("pylock.toml")
16977+
.arg("-o")
16978+
.arg("pylock.foo.bar.toml"), @"
16979+
exit_code: 2 (failure)
16980+
----- stderr -----
16981+
error: Expected the output filename to be `pylock.toml` or `pylock.<name>.toml`, where `<name>` is non-empty and contains no dots; found `pylock.foo.bar.toml`
1695516982
");
1695616983

1695716984
Ok(())

‎crates/uv/tests/pip_install/pip_install.rs‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,34 @@ fn invalid_toml_filename() -> Result<()> {
589589
.arg("test.toml"), @"
590590
exit_code: 2 (failure)
591591
----- stderr -----
592-
error: `test.toml` is not a valid PEP 751 filename: expected TOML file to start with `pylock.` and end with `.toml` (e.g., `pylock.toml`, `pylock.dev.toml`)
592+
error: `test.toml` is not a valid PEP 751 filename: expected `pylock.toml` or `pylock.<name>.toml`, where `<name>` is non-empty and contains no dots
593+
"
594+
);
595+
596+
Ok(())
597+
}
598+
599+
#[test]
600+
fn invalid_pylock_toml_filename() -> Result<()> {
601+
let context = uv_test::test_context!("3.12");
602+
context.temp_dir.child("pylock..toml").touch()?;
603+
context.temp_dir.child("pylock.foo.bar.toml").touch()?;
604+
605+
uv_snapshot!(context.pip_install()
606+
.arg("-r")
607+
.arg("pylock..toml"), @"
608+
exit_code: 2 (failure)
609+
----- stderr -----
610+
error: `pylock..toml` is not a valid PEP 751 filename: expected `pylock.toml` or `pylock.<name>.toml`, where `<name>` is non-empty and contains no dots
611+
"
612+
);
613+
614+
uv_snapshot!(context.pip_install()
615+
.arg("-r")
616+
.arg("pylock.foo.bar.toml"), @"
617+
exit_code: 2 (failure)
618+
----- stderr -----
619+
error: `pylock.foo.bar.toml` is not a valid PEP 751 filename: expected `pylock.toml` or `pylock.<name>.toml`, where `<name>` is non-empty and contains no dots
593620
"
594621
);
595622

‎crates/uv/tests/project/export.rs‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4982,6 +4982,12 @@ fn pep_751_infer_output_format() -> Result<()> {
49824982
#[test]
49834983
fn pep_751_filename() -> Result<()> {
49844984
let context = uv_test::test_context!("3.12");
4985+
fs_err::copy(
4986+
context
4987+
.workspace_root
4988+
.join("test/links/ok-1.0.0-py3-none-any.whl"),
4989+
context.temp_dir.child("ok-1.0.0-py3-none-any.whl"),
4990+
)?;
49854991

49864992
let pyproject_toml = context.temp_dir.child("pyproject.toml");
49874993
pyproject_toml.write_str(
@@ -4990,7 +4996,10 @@ fn pep_751_filename() -> Result<()> {
49904996
name = "project"
49914997
version = "0.1.0"
49924998
requires-python = ">=3.12"
4993-
dependencies = ["anyio==3.7.0"]
4999+
dependencies = ["ok"]
5000+
5001+
[tool.uv.sources]
5002+
ok = { path = "ok-1.0.0-py3-none-any.whl" }
49945003
49955004
[build-system]
49965005
requires = ["uv_build>=0.7,<10000"]
@@ -5003,8 +5012,22 @@ fn pep_751_filename() -> Result<()> {
50035012
uv_snapshot!(context.filters(), context.export().arg("--format").arg("pylock.toml").arg("-o").arg("test.toml"), @"
50045013
exit_code: 2 (failure)
50055014
----- stderr -----
5006-
Resolved 4 packages in [TIME]
5007-
error: Expected the output filename to start with `pylock.` and end with `.toml` (e.g., `pylock.toml`, `pylock.dev.toml`); `test.toml` won't be recognized as a `pylock.toml` file in subsequent commands
5015+
Resolved 2 packages in [TIME]
5016+
error: Expected the output filename to be `pylock.toml` or `pylock.<name>.toml`, where `<name>` is non-empty and contains no dots; found `test.toml`
5017+
");
5018+
5019+
uv_snapshot!(context.filters(), context.export().arg("--format").arg("pylock.toml").arg("-o").arg("pylock..toml"), @"
5020+
exit_code: 2 (failure)
5021+
----- stderr -----
5022+
Resolved 2 packages in [TIME]
5023+
error: Expected the output filename to be `pylock.toml` or `pylock.<name>.toml`, where `<name>` is non-empty and contains no dots; found `pylock..toml`
5024+
");
5025+
5026+
uv_snapshot!(context.filters(), context.export().arg("--format").arg("pylock.toml").arg("-o").arg("pylock.foo.bar.toml"), @"
5027+
exit_code: 2 (failure)
5028+
----- stderr -----
5029+
Resolved 2 packages in [TIME]
5030+
error: Expected the output filename to be `pylock.toml` or `pylock.<name>.toml`, where `<name>` is non-empty and contains no dots; found `pylock.foo.bar.toml`
50085031
");
50095032

50105033
Ok(())

0 commit comments

Comments
 (0)