Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 836651 - net-misc/yt-dlp-2022.3.8.2 user applied patches fail with: Hunk #1 FAILED at xxx (different line endings).
Summary: net-misc/yt-dlp-2022.3.8.2 user applied patches fail with: Hunk #1 FAILED at ...
Status: RESOLVED INVALID
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal (vote)
Assignee: Gentoo Linux bug wranglers
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2022-04-02 14:36 UTC by gerion
Modified: 2022-04-08 13:58 UTC (History)
2 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments
this-fails.patch (this-fails.patch,265 bytes, patch)
2022-04-02 14:36 UTC, gerion
Details | Diff
this-works.patch (this-works.patch,258 bytes, patch)
2022-04-02 19:27 UTC, Ionen Wolkens
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description gerion 2022-04-02 14:36:32 UTC
Created attachment 768510 [details, diff]
this-fails.patch

When applying an arbitrary patch (like the attached one) to yt-dlp-2022.3.8.2 it fails with the error:
Hunk #1 FAILED at xxx (different line endings).

This is due to all files of yt-dlp have DOS endings (CRLF) and patch is terribly bad with such files.

What I have found out [1, 2, 3] is that it results in:
src file: LF, patch file: LF -> patch works
src file: CRLF, patch file: LF -> above error message
src file: CRLF, patch file: CRLF -> patch only works with flag --binary

Currently user patches are applied with a plain call to patch.

I see several ways to solve this:
1. Run dos2unix on the yt-dlp sources. Solves the actual problem but other user patches on other packages may fail.
2. Run dos2unix on every packages as part of source prepare. Would fix this specific problem but may introduce other ones.
3. Run dos2unix only on the files that should be patched, if there is a user patch. Would solve the problem in a generic way but is the most complicated one code wise.

[1] https://github.com/vaimo/composer-patches/issues/46
[2] https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=071d4cef4eb389c5c85d020abdc22ad542edc6b1
[3] https://unix.stackexchange.com/questions/239364/how-to-fix-hunk-1-failed-at-1-different-line-endings-message
Comment 1 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2022-04-02 14:38:14 UTC
Patches can work fine with CRLF but some editors mangle them.
Comment 2 gerion 2022-04-02 14:54:11 UTC
(In reply to Sam James from comment #1)
> Patches can work fine with CRLF but some editors mangle them.

For the specific case, I tried this:
```
# cd /usr/portage/net-misc/yt-dlp
# file /etc/portage/patches/net-misc/yt-dlp/this-fails.patch
/etc/portage/patches/net-misc/yt-dlp/this-fails.patch: unified diff output, ASCII text
# ebuild yt-dlp-2022.3.8.2.ebuild clean prepare
--> fails with:
patching file setup.py
Hunk #1 FAILED at 4 (different line endings).

# unix2dos /etc/portage/patches/net-misc/yt-dlp/this-fails.patch
unix2dos: Datei /etc/portage/patches/net-misc/yt-dlp/this-fails.patch wird ins DOS-Format umgewandelt …
# file /etc/portage/patches/net-misc/yt-dlp/this-fails.patch 
/etc/portage/patches/net-misc/yt-dlp/this-fails.patch: unified diff output, ASCII text, with CRLF line terminators
# ebuild yt-dlp-2022.3.8.2.ebuild clean prepare
--> fails with:
(Stripping trailing CRs from patch; use --binary to disable.)
patching file setup.py
Hunk #1 FAILED at 4 (different line endings).
```

I tried to test both cases, both of them fail. There is no editor involved.
I generated the patch with:
`git diff > this-fails.patch`

Of course, I did the initial change with an editor (neovim).
Comment 3 gerion 2022-04-02 14:56:29 UTC
I hope that I read the code correct but this has to be the line that applies the user patch (no --binary or any other flag): https://gitweb.gentoo.org/repo/gentoo.git/tree/eclass/epatch.eclass#n333
Comment 4 gerion 2022-04-02 15:04:02 UTC
Could also be handled by portage internally. There is a very similar code here:
https://github.com/gentoo/portage/blob/47fb9590b57162a786d668f663c1d0dfbfd1cfb0/bin/phase-helpers.sh#L1182
Comment 6 Ionen Wolkens gentoo-dev 2022-04-02 18:12:27 UTC
As far as yt-dlp is concerned I have no intention to do anything here, I typically dislike when ebuilds use dos2unix as-is and update their patches to have proper line terminators to remove it when I come across them.
Comment 7 Ionen Wolkens gentoo-dev 2022-04-02 19:27:30 UTC
Created attachment 768542 [details, diff]
this-works.patch

Also, for interest sake, here's your fixed patch.

 * Applying user patches from /etc/portage/patches ...
 * Applying this-works.patch ... [ ok ]
 * User patches applied.
Comment 8 gerion 2022-04-02 20:31:16 UTC
Ah, I see the fix. The patch file must not have CRLF in all lines but only in the ones that need to be patched.

Unfortunately, the yt-dlp git does _not_ have CRLF line endings, so here is a little Python script to convert existing patches with LF endings (like the ones created by git) to those with CRLF endings:
```
#!/usr/bin/env python

import sys
import unidiff


patch = unidiff.PatchSet.from_filename(sys.argv[1])
for p_file in patch:
    for hunk in p_file:
        for line in hunk:
            line.value = line.value[:-1] + '\r\n'

with open(sys.argv[1], 'w') as f:
    f.write(str(patch))
```
Comment 9 Ionen Wolkens gentoo-dev 2022-04-02 21:13:58 UTC
Yeah I'm not sure why they switch to CRLF for pypi tarball, and primary reason been using it (over git) was because it has pre-generated doc / man pages.

That aside, I realized they started doing git release tarballs with documentation as well (this used to be missing), so I'll probably use it for next release if pypi is still using CRLF.

Will allow to use less silly versioning too (aka use dates with leading 0).
Comment 10 Ionen Wolkens gentoo-dev 2022-04-02 21:20:44 UTC
(In reply to Ionen Wolkens from comment #9)
> That aside, I realized they started doing git release tarballs with
> documentation as well (this used to be missing), so I'll probably use it for
> next release if pypi is still using CRLF.
Or bleh, that tarball is missing a few things compared to pypi too -- will see the situation later. Think last release was a bit messy, they had to do a 2nd take on the pypi tarball with this .2 release.
Comment 11 gerion 2022-04-02 21:29:10 UTC
Thanks for the effort!

I'm know of the lazy extractors loading that is present in the pypi tarballs only. However, running `python devscripts/make_lazy_extractors.py` should do the same for a git installation.
Comment 12 Ionen Wolkens gentoo-dev 2022-04-08 13:58:32 UTC
Turns out the pypi tarball for 2022.4.8 switched to using unix terminators, so there's no major reason to change things around at the moment.