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

Unexpected pyproject.toml changes after poetry install #204

Closed
jonyscathe opened this issue Jan 7, 2025 · 4 comments · Fixed by #207
Closed

Unexpected pyproject.toml changes after poetry install #204

jonyscathe opened this issue Jan 7, 2025 · 4 comments · Fixed by #207
Labels
bug Something isn't working
Milestone

Comments

@jonyscathe
Copy link

Hi,
I understand that the way poetry-dynamic-versioning works is that it edits the pyproject.toml file at the start of a poetry install and then edits it again at the end.

I was wondering if there is some way to preserve formatting a bit better than is done currently.

Where I work our standard use of toml is to use literal strings where possible, and to use a spacing of 2.

We don't really mind that poetry-dynamic-versioning changes our version from '0.0.0' to "0.0.0". Really doesn't matter that the version isn't a literal string.
But now with the poetry v2 changes we are having:

dynamic = [
  'version',
]

change to

dynamic = [
    "version"
]

With the notable change being from 2 spaces to 4. This then causes our toml file linting to fail, causes us CI issues.

Obviously there are some fairly easy ways that we can fix this, but it wold be nice if this tool didn't make formatting changes to our file.

@mtkennerly
Copy link
Owner

Hi! Unfortunately, we're at the mercy of tomlkit here, specifically:

There was another formatting issue related to Poetry v2 here: #203 . I'm open to suggestions, but not sure how much we can really do.

@mtkennerly mtkennerly added the bug Something isn't working label Jan 7, 2025
@edgarrmondragon
Copy link
Contributor

@mtkennerly could this particular problem be addressed by saving and then restoring the full project.dynamic array in:

dynamic_index = pyproject["project"]["dynamic"].index("version")

and

if state.dynamic_index is not None:
index = state.dynamic_index
pyproject["project"]["dynamic"].insert(index, "version") # type: ignore

?

@jonyscathe
Copy link
Author

jonyscathe commented Jan 7, 2025

Hi,
Ah yes, understand that tomlkit is particularly average for a lot of cases.
And while just completely reverting the pyproject.toml file would be fine for things like poetry install it obviously isn't fine for other commands like poetry add.
I guess for now we will just work around this.

Edit: Though something like @edgarrmondragon's solution looks like it would work to me for the basic vs literal string aspect. Though I don't think you need to save and restore the whole array, just save and restore the actual "version" string. That way it stays as a tomlkit.items.String object which keeps its _t property, rather than inserting a python string which defaults to a toml basic string (double quotes).
Edit again...: actually, saving and restoring the version string would only fix the basic vs literal, but saving and restoring the whole array should preserve the indentation (I think...). And it should be safe to do that. I don't think any poetry commands would ever change the dynamic array

@edgarrmondragon
Copy link
Contributor

@jonyscathe on local testing the approach seems to work so I'll just open a PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
3 participants