Skip to content

Commit 419d77a

Browse files
akshaykamscolnick
andauthored
fix: no diff churn when adding requires-python to a sandboxed file (#8054)
Use a regex instead of overwriting the whole script header --------- Co-authored-by: Myles Scolnick <myles@marimo.io>
1 parent db628cc commit 419d77a

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

‎marimo/_cli/sandbox.py‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
has_marimo_in_script_metadata,
2525
is_marimo_dependency,
2626
)
27-
from marimo._utils.scripts import with_python_version_requirement
2827
from marimo._utils.uv import find_uv_bin
2928
from marimo._utils.versions import is_editable
3029
from marimo._version import __version__
@@ -324,14 +323,12 @@ def construct_uv_command(
324323
def _ensure_python_version_in_script_metadata(name: str) -> None:
325324
"""Add requires-python to script metadata if not present.
326325
327-
Reads the file, parses the PEP 723 metadata, adds requires-python
328-
if missing, and writes the updated metadata back.
326+
Inserts a requires-python line directly into the existing PEP 723
327+
metadata block without re-serializing, to avoid reformatting diffs.
329328
"""
330-
from marimo._utils.scripts import (
331-
REGEX,
332-
read_pyproject_from_script,
333-
write_pyproject_to_script,
334-
)
329+
import re
330+
331+
from marimo._utils.scripts import read_pyproject_from_script
335332

336333
with open(name, encoding="utf-8") as f:
337334
content = f.read()
@@ -342,17 +339,22 @@ def _ensure_python_version_in_script_metadata(name: str) -> None:
342339
return
343340

344341
if "requires-python" in project:
345-
# Already has Python version
346342
return
347343

348-
# Generate new script metadata block
349-
project = with_python_version_requirement(project)
350-
new_block = write_pyproject_to_script(project)
351-
352-
# Replace the old block with the new one
353-
import re
344+
version_tuple = platform.python_version_tuple()
345+
requires_line = (
346+
f'# requires-python = ">={version_tuple[0]}.{version_tuple[1]}"\n'
347+
)
354348

355-
new_content = re.sub(REGEX, new_block, content, count=1)
349+
# Insert directly after the opening "# /// script" marker to avoid
350+
# re-serializing the entire block and causing formatting churn.
351+
new_content = re.sub(
352+
r"^# /// script$",
353+
"# /// script\n" + requires_line.rstrip(),
354+
content,
355+
count=1,
356+
flags=re.MULTILINE,
357+
)
356358

357359
if new_content != content:
358360
with open(name, "w", encoding="utf-8") as f:

0 commit comments

Comments
 (0)