Avoid writing invalid PEP 723 scripts on `tool.uv.sources` (#6706)

## Summary

We were writing empty lines between the dependencies and the
`tool.uv.sources` table, which led to the `/// script` tag being
unclosed and thus not recognized.

Closes https://github.com/astral-sh/uv/issues/6700.
This commit is contained in:
Charlie Marsh 2024-08-27 13:49:08 -04:00 committed by GitHub
parent a8f4e08d5b
commit 8d466db080
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 20 deletions

View File

@ -350,13 +350,12 @@ fn serialize_metadata(metadata: &str) -> String {
output.push('\n');
for line in metadata.lines() {
if line.is_empty() {
output.push('\n');
} else {
output.push_str("# ");
output.push('#');
if !line.is_empty() {
output.push(' ');
output.push_str(line);
output.push('\n');
}
output.push('\n');
}
output.push_str("# ///");

View File

@ -1,6 +1,7 @@
#![cfg(all(feature = "python", feature = "pypi"))]
use anyhow::Result;
use assert_cmd::assert::OutputAssertExt;
use assert_fs::prelude::*;
use indoc::indoc;
use insta::assert_snapshot;
@ -4188,16 +4189,12 @@ fn add_git_to_script() -> Result<()> {
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "rich",
# "anyio",
# ]
# ///
import requests
from rich.pretty import pprint
resp = requests.get("https://peps.python.org/api/peps.json")
data = resp.json()
pprint([(k, v["title"]) for k, v in data.items()][:10])
import anyio
import uv_public_pypackage
"#})?;
uv_snapshot!(context.filters(), context
@ -4223,23 +4220,23 @@ fn add_git_to_script() -> Result<()> {
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "rich",
# "anyio",
# "uv-public-pypackage",
# ]
#
# [tool.uv.sources]
# uv-public-pypackage = { git = "https://github.com/astral-test/uv-public-pypackage", tag = "0.0.1" }
# ///
import requests
from rich.pretty import pprint
resp = requests.get("https://peps.python.org/api/peps.json")
data = resp.json()
pprint([(k, v["title"]) for k, v in data.items()][:10])
import anyio
import uv_public_pypackage
"###
);
});
// Ensure that the script runs without error.
context.run().arg("script.py").assert().success();
Ok(())
}