Repository navigation
Conversation
ale#util#Writefile() used writefile() directly, which writes the bytes of Vim's internal encoding. For buffers with a non-UTF-8 &fileencoding, the temporary files handed to linters and fixers did not match the file on disk, so tools that honour an in-file coding declaration read the wrong bytes. Convert lines with iconv() when &fileencoding is set and differs from &encoding, and add ale#util#Readfile() to convert fixer output back to the internal encoding before it is applied to the buffer.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: fb85824131
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| let l:fileencoding = getbufvar(a:buffer, '&fileencoding') | ||
|
|
||
| if exists('*iconv') && !empty(l:fileencoding) && l:fileencoding isnot# &encoding | ||
| let l:corrected_lines = s:ConvertEncodings( |
There was a problem hiding this comment.
Preserve encoded line boundaries in temporary files
When &fileencoding is a multibyte or non-ASCII-compatible encoding such as ucs-2le, this converts each line's contents but not the separators inserted by writefile(), and Readfile() calls readfile() before converting the output. A %t linter or fixer therefore receives a malformed file, or ALE corrupts a valid encoded fixer result when reading it back; handle line boundaries in the file encoding as well.
Useful? React with 👍 / 👎.
| " the buffer. | ||
| function! ale#util#Readfile(buffer, filename) abort | ||
| let l:lines = readfile(a:filename) | ||
| let l:fileencoding = getbufvar(a:buffer, '&fileencoding') |
There was a problem hiding this comment.
Decode fixer output using the encoding used to write it
When a temporary-file fixer runs asynchronously and &fileencoding changes before its exit callback, ale#util#Writefile() encoded the input using the old value but this lookup makes ale#util#Readfile() decode the output using the new one. ale#fix#ApplyFixes() checks for changed buffer lines but not an option-only change, so it can apply mojibake; capture the encoding for the job or discard its result if the option changes.
Useful? React with 👍 / 👎.
Fixes #5139
Root cause
ale#util#Writefile()passed the buffer lines straight towritefile(), only fixing up\rforfileformat=dos.writefile()writes the bytes Vim holds in memory, which are in&encoding(UTF-8 by default). For a buffer whose&fileencodingis something else (gb18030,latin1,shift_jis, ...), the temporary file ALE creates for linters and fixers therefore did not match the file on disk, so tools that honour the file's own coding declaration decoded the wrong bytes.Reproduced with a file containing a single line
中文encoded as GB18030, opened with++enc=gb18030:Before this patch the temporary file was UTF-8:
The file on disk is:
After the patch the temporary file matches:
Change
ale#util#Writefile()converts lines withiconv()from&encodingto&fileencodingwhen&fileencodingis set and differs, skipped when the build has noiconv().ale#util#Readfile()does that andale#fix#uses it instead ofreadfile(). Without this, a fixer on a GB18030 buffer would leave raw GB18030 bytes in a UTF-8 buffer.Net production change: +38 lines in
autoload/ale/util.vim, one line inautoload/ale/fix.vim.Tests
test/test_writefile_function.vader: agb18030buffer now producesiconv('中文', 'utf-8', 'gb18030')in the temporary file, and an emptyfileencodingstill writes UTF-8 unchanged. The first assertion fails on master.test/test_readfile_function.vader:ale#util#Readfile()converts GB18030 back to UTF-8, and does nothing whenfileencodingis empty.test/fix/test_ale_fix.vader: end-to-end:ALEFixwith a fixer that writes中as GB18030 (printf '\326\320\n' > %t); the buffer must end up as中. This fails without theReadfile()change:['��'] should be equal to ['中'].Real output, Vim 9.1 (Vader against this branch):
The same three files also pass with Neovim 0.12.6:
Success/Total: 54/54 (assertions: 37/37).vintandtest/script/block-padding-checkerreport no issues for the changed files.