Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

uv does not respect position of comments in pyproject.toml #8343

Closed
notatallshaw opened this issue Oct 18, 2024 · 2 comments · Fixed by #8384
Closed

uv does not respect position of comments in pyproject.toml #8343

notatallshaw opened this issue Oct 18, 2024 · 2 comments · Fixed by #8384
Labels
bug Something isn't working help wanted Contribution especially encouraged

Comments

@notatallshaw
Copy link
Collaborator

uv 0.4.24

Maybe this is already reported but I couldn't find it.

Steps to reproduce:

1, uv init temp
2. cd temp
3. uv add requests
4. uv add whenever~=0.6.9
5. Edit project.dependencies in pyproject.toml from:

dependencies = [
    "requests>=2.32.3",
    "whenever~=0.6.9",
]

To:

dependencies = [
    "requests>=2.32.3", # Should not expect to be updated frequently
    "whenever~=0.6.9", # Pinned to 0.6 due to API not yet being stable
]
  1. uv add uv zope
  2. Check project.dependencies in pyproject.toml:

Actual output:

dependencies = [
    "requests>=2.32.3",
    "uv>=0.4.24",
    # Should not expect to be updated frequently
    "whenever~=0.6.9",
    "zope>=5.11",
     # Pinned to 0.6 due to API not yet being stable
]

Expected output:

dependencies = [
    "requests>=2.32.3", # Should not expect to be updated frequently
    "uv>=0.4.24",
    "whenever~=0.6.9", # Pinned to 0.6 due to API not yet being stable
    "zope>=5.11", 
]

Context:

I often leave comments in my pyproject.toml to explain why a particular dependency is pinned or given an upper bound.

I appreciate that comments are a hard problem when reading and writing toml, and there will always be edge cases where the user needs to manually update them (e.g. a requirement is removed), but I don't think this example is an edge case.

@charliermarsh
Copy link
Member

charliermarsh commented Oct 18, 2024

I suppose that's a legitimate bug though end-of-line comments are my mortal enemy from working on the formatter and I don't know if I can work up the courage to fix it myself :)

@charliermarsh charliermarsh added bug Something isn't working help wanted Contribution especially encouraged labels Oct 18, 2024
@flyaroundme
Copy link
Contributor

flyaroundme commented Oct 20, 2024

Tricky one. The fun thing is that the "current dependency line comment" in this case is becoming a next dependency prefix, so in case it is the comment on the last line it is becoming part of trailing, that's why it is in the end of the file after adding zope.
I am digging into it so feel free to assign on me, but most probably can send the fix more close to the end of the weekend, so I'm totally fine if somebody solves it beforehand

MtkN1 pushed a commit to MtkN1/uv that referenced this issue Oct 21, 2024
…al-sh#8384)

## Summary

This PR is is to address the problem when the same-line comments in
`pyproject.toml` could be found in unpredictable positions after `uv
add` or `remove` reformats the `pyproject.toml` file.

Introduced the `Comment` structure in `pyproject_mut` module to
distinguish "same-line" comments and "full-line" comments while
reformatting, because logic for them differs.
Sorry, the implementation could be clumsy, I'm just learning Rust, but
it seems to work 😅

Closes astral-sh#8343

## Test Plan

Added the new test:
`add_preserves_comments_indentation_and_sameline_comments`

To test followed the actions from the issue ticket
astral-sh#8343

---------

Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Contribution especially encouraged
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants