Skip to content

Initial uv add --script produces invalid result with existing metadata section #12499

Closed
@merlinz01

Description

@merlinz01

Summary

When running uv add --script <script> <package> on a script which has an existing non-script metadata section and doesn't have the script section, uv fails to put a blank line between the sections, resulting in an unintuitive error when running the script or adding more dependencies.

MRE:

$ cat myscript.py
# /// nonstandardsection
# Hello,
# World!
# ///

print('Hello, world')

$ uv run --script myscript.py 
Hello, world
$ uv add --script myscript.py requests
Updated `myscript.py`
$ cat myscript.py 
# /// script
# requires-python = ">=3.13"
# dependencies = [
#     "requests",
# ]
# ///
# /// nonstandardsection
# Hello,
# World!
# ///

print('Hello, world')

$ uv run --script myscript.py 
error: TOML parse error at line 5, column 1
  |
5 | ///
  | ^
invalid key
$ uv add --script myscript.py fastapi
error: TOML parse error at line 5, column 1
  |
5 | ///
  | ^
invalid key
$ nano myscript.py # add a blank line between the sections
$ cat myscript.py 
# /// script
# requires-python = ">=3.13"
# dependencies = [
#     "requests",
# ]
# ///

# /// nonstandardsection
# Hello,
# World!
# ///

print('Hello, world')

$ uv run --script myscript.py 
Installed 5 packages in 6ms
Hello, world

Possibly related: #6366

Platform

Linux Mint 22.1

Version

uv 0.6.9

Python version

Python 3.13.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions