Retain trailing comments after PEP 723 metadata block (#13460)

## Summary

Closes https://github.com/astral-sh/uv/issues/13447.
This commit is contained in:
Charlie Marsh
2025-05-14 17:54:20 -04:00
committed by GitHub
parent 395039afd1
commit b326bb92a0
2 changed files with 74 additions and 9 deletions
+7 -9
View File
@@ -455,14 +455,9 @@ impl ScriptTag {
// > consists of only a single #).
let mut toml = vec![];
// Extract the content that follows the metadata block.
let mut python_script = vec![];
while let Some(line) = lines.next() {
for line in lines {
// Remove the leading `#`.
let Some(line) = line.strip_prefix('#') else {
python_script.push(line);
python_script.extend(lines);
break;
};
@@ -474,8 +469,6 @@ impl ScriptTag {
// Otherwise, the line _must_ start with ` `.
let Some(line) = line.strip_prefix(' ') else {
python_script.push(line);
python_script.extend(lines);
break;
};
@@ -517,7 +510,12 @@ impl ScriptTag {
// Join the lines into a single string.
let prelude = prelude.to_string();
let metadata = toml.join("\n") + "\n";
let postlude = python_script.join("\n") + "\n";
let postlude = contents
.lines()
.skip(index + 1)
.collect::<Vec<_>>()
.join("\n")
+ "\n";
Ok(Some(Self {
prelude,