Skip to content

Update .gitattributes for the wrongencoding files - #13811

Closed
ianhi wants to merge 1 commit into
sphinx-doc:masterfrom
ianhi:fix-encoding-test-files
Closed

ianhi wants to merge 1 commit into
sphinx-doc:masterfrom
ianhi:fix-encoding-test-files

Conversation

@ianhi

@ianhi ianhi commented Aug 4, 2025

Copy link
Copy Markdown
Contributor

Purpose

There was an existing .gitattributes for these files, but it wasn't working on my system (M2 mac). I was unable to restore or stash these files. Which made rebasing difficulty. This approach fixed the git issues for me.

I was getting errors like:

error: failed to encode 'tests/roots/test-root/wrongenc.inc' from UTF-8 to latin-1
error: failed to encode 'tests/roots/test-warnings/wrongenc.inc' from UTF-8 to latin-1

whenever I tried to restore, stash, or rebase.

Full disclosure - this was a level of git that was beyond me so this is an AI assisted PR. Claude summarizes the downsides as:

⚠️ Considerations:

  • Diff viewing - These files will show as "binary" in git diffs instead of text changes
  • Text editors - Some editors might treat them as binary files

Although I was able to open them in neovim without issue.

References

It looks like there was a recent similar change: 5cf62e5

@ianhi
ianhi force-pushed the fix-encoding-test-files branch from 0b6ccf6 to bc8f923 Compare August 4, 2025 21:34
Mark wrongenc.inc files as binary in .gitattributes to prevent encoding
conversion issues on different platforms. These files contain intentional
Latin-1 encoded content for testing Sphinx's encoding handling and should
remain byte-for-byte identical.

Fixes git errors like:
- "failed to encode from UTF-8 to latin-1"
- "patch does not apply" in pre-commit hooks
- stash/unstash failures with these files

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>

@AA-Turner AA-Turner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this is right; the files are text, not binary content. My attempt here was to properly mark the encoding as I understand that Git's internal index is stored in Unicode, whereas we have a few test files that we intentionally have in different encodings.

Does macOS support the Latin-1 codec in general? I imagine there's no support for Windows-1252? (the default Windows codepage)

A third option would be to remove these files from the repo and have the tests write them to disk each time, which might be somewhat clearer in the intent.

A

@jfbu

jfbu commented Aug 7, 2025

Copy link
Copy Markdown
Contributor

I am on macOS and I would like to help because I have encountered the issue too. The problem is that I do not know how to reproduce it. I did not encounter it while working on some PRs I merged last few days, but I definitely had the issue about five days ago, and it was a bit painful because one had to be careful to not do git commit -a. It seems it went away after I merged master into a PR of mine using the github web site interface, then I fetched it back from my fork of sphinx-doc/sphinx to my computer.

@jfbu

jfbu commented Aug 7, 2025 •

Copy link
Copy Markdown
Contributor

Does macOS support the Latin-1 codec in general? I imagine there's no support for Windows-1252? (the default Windows codepage)

Emacs on macOS definitely support the Latin-1 codec and probably Windows-1252 inclusive of the problem of EOLs. There are some issues with Unicode rather when one copies files to external hard disks where the macOS might automatically change the type of Unicode normalization, but this looks like something else.

@ianhi

ianhi commented Aug 7, 2025 •

Copy link
Copy Markdown
Contributor Author

Does macOS support the Latin-1 codec in general? I imagine there's no support for Windows-1252? (the default Windows codepage)

I thought yes

A third option would be to remove these files from the repo and have the tests write them to disk each time, which might be somewhat clearer in the intent.

This seems easiest

Although I also cannot replicate it now :( which is extremely confusing. Maybe this was a temporary bug somewhere else (git, iterm, mac?)?

So maybe this is just resolved, and we can close this, and if it comes up again hopefully someone will find this and not feel despair.

@jfbu

jfbu commented Aug 7, 2025

Copy link
Copy Markdown
Contributor

I am not 100% sure but it does look as if it is 5cf62e5 which fixed the issue for me once I had updated my locale. But trying to revert it I fail so far to trigger Git into complaining again about those wrongly encoded files.

@ianhi

ianhi commented Aug 7, 2025

Copy link
Copy Markdown
Contributor Author

I am not 100% sure but it does look as if it is 5cf62e5 which fixed the issue for me once I had updated my locale. But trying to revert it I fail so far to trigger Git into complaining again about those wrongly encoded files.

Confusingly I only made a fork a few days ago, so I would for sure have had this already when I started seeing the issue.

@Ch3ri0ur

Copy link
Copy Markdown
Contributor

I got the same error (Win11/WSL2). Once I touched those files they were "stuck" and could not be reverted/discarded anymore.

@miketheman

miketheman commented Sep 1, 2025 •

Copy link
Copy Markdown
Contributor

I suspect the issue lies with syntax in gitattributes - the value should be ISO-8859.

On both a modern Ubuntu Linux and macOS, the results are the same:

$ file --mime tests/roots/test-root/wrongenc.inc tests/roots/test-warnings/wrongenc.inc
tests/roots/test-root/wrongenc.inc:     text/plain; charset=iso-8859-1
tests/roots/test-warnings/wrongenc.inc: text/plain; charset=iso-8859-1

Setting .gitattributes config and running a git status should will restore the files away from dirty state:

diff --git a/.gitattributes b/.gitattributes
index c8acd1081..d44a1f750 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -51,8 +51,8 @@ tests/roots/test-pycode/cp_1251_coded.py dos

 # Non UTF-8 encodings
 tests/roots/test-pycode/cp_1251_coded.py working-tree-encoding=windows-1251
-tests/roots/test-root/wrongenc.inc working-tree-encoding=latin-1
-tests/roots/test-warnings/wrongenc.inc working-tree-encoding=latin-1
+tests/roots/test-root/wrongenc.inc working-tree-encoding=iso-8859
+tests/roots/test-warnings/wrongenc.inc working-tree-encoding=iso-8859

 # Generated files
 # https://github.com/github/linguist/blob/master/docs/overrides.md
$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   .gitattributes

no changes added to commit (use "git add" and/or "git commit -a")

P.S. I'm not sure why setting working-tree-encoding=iso-8859-1 doesn't work, but figured this is better for now.

gmilde added a commit to gmilde/sphinx that referenced this pull request Oct 21, 2025
Try to avoid files marked as changed due to an error when
auto-recoding from UTF-8 to ISO-8859-1

see sphinx-doc#13811 (comment)
@gmilde

gmilde commented Oct 21, 2025 •

Copy link
Copy Markdown
Contributor

Same problem here, on a Debian/stable system. I don't remember whether it started right from the beginning (setup of a local mirror of my fork) or after the first changes or a rebase.

The workaround proposed by miketheman in #13811 (comment) helped, although now an inofficial alias is replaced by an incomplete name¹.

¹ The standard ISO-8859 defines a series of several encodings but for some mysterious reason the correct name "ISO-8859-1" does not help :(.

@aureliobarbosa

Copy link
Copy Markdown

I don't think this is right; the files are text, not binary content. My attempt here was to properly mark the encoding as I understand that Git's internal index is stored in Unicode, whereas we have a few test files that we intentionally have in different encodings.

I took a look into gitattribute's documentation and came to think that the solution originally proposed on this PR is actually better than the alternatives. My argument is as follows: by construction the text file is assigned a wrong encoding, since git stores all of it's texts on UTF-8, in some platforms it will fail when trying to convert it internally to UFT-8 and keep it as is (according to git docs If a Git client, that does not support the working-tree-encoding attribute, adds a new file ... will be stored "as-is" internally). In addition to that, Reencoding content to non-UTF encodings can cause errors as the conversion might not be UTF-8 round trip safe.

Note that since it is trying to convert the file to UTF-8, in some platforms it will succeed and in other, maybe not (as is the case with some developers interested in contributing to this repository, just like me).

Continuing the argument, if one marks the text files with wrong encodings as binary the file will have the same hash for everybody, regardless of the developer platform. On the other hand, even if the encoding is changed to one that apparently is consistent in all platforms, nothing can guarantee that a future change in the operating system, or even on git, cannot break this test again. So, I my opinion marking this file as binary, as originally proposed is simple and solves the issue.

@AA-Turner

Copy link
Copy Markdown
Member

Applied @miketheman's suggestion in 4f23c99

A

@AA-Turner AA-Turner closed this Nov 30, 2025
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Dec 29, 2025
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.

7 participants