Skip to content

Commit b326bb9

Browse files
Retain trailing comments after PEP 723 metadata block (#13460)
## Summary Closes #13447.
1 parent 395039a commit b326bb9

File tree

2 files changed

+74
-9
lines changed

2 files changed

+74
-9
lines changed

crates/uv-scripts/src/lib.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -455,14 +455,9 @@ impl ScriptTag {
455455
// > consists of only a single #).
456456
let mut toml = vec![];
457457

458-
// Extract the content that follows the metadata block.
459-
let mut python_script = vec![];
460-
461-
while let Some(line) = lines.next() {
458+
for line in lines {
462459
// Remove the leading `#`.
463460
let Some(line) = line.strip_prefix('#') else {
464-
python_script.push(line);
465-
python_script.extend(lines);
466461
break;
467462
};
468463

@@ -474,8 +469,6 @@ impl ScriptTag {
474469

475470
// Otherwise, the line _must_ start with ` `.
476471
let Some(line) = line.strip_prefix(' ') else {
477-
python_script.push(line);
478-
python_script.extend(lines);
479472
break;
480473
};
481474

@@ -517,7 +510,12 @@ impl ScriptTag {
517510
// Join the lines into a single string.
518511
let prelude = prelude.to_string();
519512
let metadata = toml.join("\n") + "\n";
520-
let postlude = python_script.join("\n") + "\n";
513+
let postlude = contents
514+
.lines()
515+
.skip(index + 1)
516+
.collect::<Vec<_>>()
517+
.join("\n")
518+
+ "\n";
521519

522520
Ok(Some(Self {
523521
prelude,

crates/uv/tests/it/edit.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5762,6 +5762,73 @@ fn add_script_settings() -> Result<()> {
57625762
Ok(())
57635763
}
57645764

5765+
#[test]
5766+
fn add_script_trailing_comment_lines() -> Result<()> {
5767+
let context = TestContext::new("3.12");
5768+
5769+
let script = context.temp_dir.child("script.py");
5770+
script.write_str(indoc! {r#"
5771+
# /// script
5772+
# requires-python = ">=3.11"
5773+
# dependencies = [
5774+
# "requests<3",
5775+
# "rich",
5776+
# ]
5777+
# ///
5778+
#
5779+
# Additional description
5780+
5781+
import requests
5782+
from rich.pretty import pprint
5783+
5784+
resp = requests.get("https://peps.python.org/api/peps.json")
5785+
data = resp.json()
5786+
pprint([(k, v["title"]) for k, v in data.items()][:10])
5787+
"#})?;
5788+
5789+
uv_snapshot!(context.filters(), context.add().arg("anyio").arg("--script").arg("script.py"), @r###"
5790+
success: true
5791+
exit_code: 0
5792+
----- stdout -----
5793+
5794+
----- stderr -----
5795+
Updated `script.py`
5796+
"###);
5797+
5798+
let script_content = context.read("script.py");
5799+
5800+
insta::with_settings!({
5801+
filters => context.filters(),
5802+
}, {
5803+
assert_snapshot!(
5804+
script_content, @r##"
5805+
# /// script
5806+
# requires-python = ">=3.11"
5807+
# dependencies = [
5808+
# "anyio",
5809+
# "requests<3",
5810+
# "rich",
5811+
# ]
5812+
# ///
5813+
#
5814+
# Additional description
5815+
5816+
import requests
5817+
from rich.pretty import pprint
5818+
5819+
resp = requests.get("https://peps.python.org/api/peps.json")
5820+
data = resp.json()
5821+
pprint([(k, v["title"]) for k, v in data.items()][:10])
5822+
"##
5823+
);
5824+
});
5825+
5826+
// Adding to a script without a lockfile shouldn't create a lockfile.
5827+
assert!(!context.temp_dir.join("script.py.lock").exists());
5828+
5829+
Ok(())
5830+
}
5831+
57655832
/// Add to a script without an existing metadata table.
57665833
#[test]
57675834
fn add_script_without_metadata_table() -> Result<()> {

0 commit comments

Comments
 (0)