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

fix: keep lock file eol #9468

Merged
merged 14 commits into from
Aug 18, 2024
Merged

fix: keep lock file eol #9468

merged 14 commits into from
Aug 18, 2024

Conversation

trim21
Copy link
Contributor

@trim21 trim21 commented Jun 3, 2024

Pull Request Check List

Resolves: #1488 again
Resolves: #7527

  • Added tests for changed code.
  • Updated documentation for changed code.

#1488 once be fixed but it's broken again.

appearly TOMLFile need to call read method to get line encoding, otherwise it just use default os eol.

@trim21 trim21 marked this pull request as draft June 3, 2024 21:56
@trim21 trim21 marked this pull request as ready for review June 3, 2024 21:59
@Secrus
Copy link
Member

Secrus commented Jun 3, 2024

Could you please make the fix in tomlkit instead? Sounds like it belongs there more than Poetry code.

@trim21
Copy link
Contributor Author

trim21 commented Jun 3, 2024

Could you please make the fix in tomlkit instead? Sounds like it belongs there more than Poetry code.

tomlkit already fixed this, many times, I think.

https://github.com/python-poetry/tomlkit/pulls?q=is%3Apr+line+ending+is%3Aclosed

they even have test for this

@Secrus
Copy link
Member

Secrus commented Jun 3, 2024

Well, if it was fixed in tomlkit, we wouldn't need this patch, right? For me, this sounds like a workaround at best.

@trim21
Copy link
Contributor Author

trim21 commented Jun 3, 2024

Well, if it was fixed in tomlkit, we wouldn't need this patch, right? For me, this sounds like a workaround at best.

Actually, I'm not sure if this is a bug of tomlkit, or poetry didn't use it as expected. I guess tomlkit would expect users to read a toml file with TomlFile then write to it, but we are actually reading lockfile with tomllib instead of TomlFile provided by tomlkit

(and personally I hope we just use Unix eol everywhere…)

@trim21
Copy link
Contributor Author

trim21 commented Jun 3, 2024

but you are right, this can be fiexd on both side. tomlkit could get line ending when TomlFile object is created.

@trim21
Copy link
Contributor Author

trim21 commented Aug 10, 2024

/ping

Copy link
Member

@radoering radoering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this has a negative impact on performance but I hope this is neglectable.

tests/packages/test_locker.py Outdated Show resolved Hide resolved
@Secrus
Copy link
Member

Secrus commented Aug 16, 2024

I still believe that this should be fixed in tomlkit not here

trim21 and others added 2 commits August 16, 2024 23:30
Co-authored-by: Randy Döring <30527984+radoering@users.noreply.github.com>
@radoering
Copy link
Member

I still believe that this should be fixed in tomlkit not here

I do not think that is possible because since #6562 we use tomli to read the lock file so that tomlkit cannot know what type of line endings are in the lock file. If we want to keep existing line endings we either have to read with tomlkit (what this PR is doing) or determine the type of line ending by ourself and somehow set it in tomlkit. I think I would prefer reading with tomlkit (as it is done in this PR) if it was not a performance issue.

#6562 suggests that there is a relevant performance impact:

Even for a modestly sized project like poetry itself, reading poetry.lock on my laptop takes 0.6 - 0.7 seconds (timings obtained with some time.time() statements inserted into the codebase). tomli manages it in ~0.02 seconds.

Thus, maybe we should really check the timings and investigate the alternative approach.

@trim21
Copy link
Contributor Author

trim21 commented Aug 16, 2024

I still believe that this should be fixed in tomlkit not here

I do not think that is possible because since #6562 we use tomli to read the lock file so that tomlkit cannot know what type of line endings are in the lock file. If we want to keep existing line endings we either have to read with tomlkit (what this PR is doing) or determine the type of line ending by ourself and somehow set it in tomlkit. I think I would prefer reading with tomlkit (as it is done in this PR) if it was not a performance issue.

#6562 suggests that there is a relevant performance impact:

Even for a modestly sized project like poetry itself, reading poetry.lock on my laptop takes 0.6 - 0.7 seconds (timings obtained with some time.time() statements inserted into the codebase). tomli manages it in ~0.02 seconds.

Thus, maybe we should really check the timings and investigate the alternative approach.

I agree, we can patch tomlkit, but it means tomlkit need to get EOL even using tomlfile.write, which is a bad idea.

this only impact perf when it write lockfile, which mean only add and lock. other command like install, update won't be affected. normally add/lock will spend more time on resolving.

@radoering
Copy link
Member

this only impact perf when it write lockfile, which mean only add and lock. other command like install, update won't be affected. normally add/lock will spend more time on resolving.

Good point. I think I will do some simple performance measurements later.

We could basically implement part of the logic of https://github.com/python-poetry/tomlkit/pull/201/files by ourselves, just using TOMLDocument.as_string() instead of TOMLFile.write(). However, that might not be worth it depending on what the timing measurements will reveal.

Copy link
Member

@radoering radoering left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I measured the timing for Poetry itself, and the penalty for reading the lock file with tomlkit is still about 0.7 s. Even though that is only done when the lock file is actually written, it still feels like a significant regression for an edge use case (i.e. keep line endings). IMO, we have two options:

src/poetry/packages/locker.py Outdated Show resolved Hide resolved
trim21 and others added 3 commits August 17, 2024 20:38
Co-authored-by: Randy Döring <30527984+radoering@users.noreply.github.com>
@dimbleby
Copy link
Contributor

IMO, we have two options:

or, abandon the notion that a particular formatting-style for lockfiles is important and write them with tomli-w - which always uses \n exclusively and therefore can never flip-flop.

I realise that I've suggested this in the past and I guess we likely still disagree on the merits of this - but I continue to favour eliminating tomlkit wherever it is possible to do so.

@dimbleby
Copy link
Contributor

to make the tomli-w comparison concrete I opened #9637

@trim21
Copy link
Contributor Author

trim21 commented Aug 17, 2024

im OK with forcing lf, I just want eol don't change from lf to crlf or crlf to lf.

@radoering radoering merged commit 5d19ab0 into python-poetry:main Aug 18, 2024
75 checks passed
@trim21 trim21 deleted the fix-rewrite-eol branch August 18, 2024 16:14
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 18, 2024
@trim21 trim21 restored the fix-rewrite-eol branch October 14, 2024 15:58
@trim21 trim21 deleted the fix-rewrite-eol branch December 6, 2024 16:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Preserve line breaks in poetry.lock (regression) keep old line breaker
4 participants