Skip to content

fix: write and read temporary files in the buffer file encoding - #5185

Open
travisim wants to merge 1 commit into
dense-analysis:masterfrom
travisim:fix/temp-file-encoding
Open

travisim wants to merge 1 commit into
dense-analysis:masterfrom
travisim:fix/temp-file-encoding

Conversation

@travisim

Copy link
Copy Markdown

Fixes #5139

Root cause

ale#util#Writefile() passed the buffer lines straight to writefile(), only fixing up \r for fileformat=dos. writefile() writes the bytes Vim holds in memory, which are in &encoding (UTF-8 by default). For a buffer whose &fileencoding is 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:

call ale#util#Writefile(bufnr(''), getline(1, '$'), '/tmp/gb-out.txt')

Before this patch the temporary file was UTF-8:

$ xxd /tmp/gb-out.txt
00000000: e4b8 ade6 9687 0a

The file on disk is:

$ xxd /tmp/gb-in.txt
00000000: d6d0 cec4 0a

After the patch the temporary file matches:

$ xxd /tmp/gb-out.txt
00000000: d6d0 cec4 0a

Change

  • ale#util#Writefile() converts lines with iconv() from &encoding to &fileencoding when &fileencoding is set and differs, skipped when the build has no iconv().
  • Fixer output is written by the external tool in the same encoding, so the reverse conversion is needed when ALE reads the temporary file back. ale#util#Readfile() does that and ale#fix# uses it instead of readfile(). 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 in autoload/ale/fix.vim.

Tests

  • test/test_writefile_function.vader: a gb18030 buffer now produces iconv('中文', 'utf-8', 'gb18030') in the temporary file, and an empty fileencoding still 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 when fileencoding is empty.
  • test/fix/test_ale_fix.vader: end-to-end :ALEFix with a fixer that writes 中 as GB18030 (printf '\326\320\n' > %t); the buffer must end up as 中. This fails without the Readfile() change: ['��'] should be equal to ['中'].

Real output, Vim 9.1 (Vader against this branch):

Success/Total: 8/8    test/test_writefile_function.vader
Success/Total: 2/2    test/test_readfile_function.vader
Success/Total: 44/44  test/fix/test_ale_fix.vader
Success/Total: 54/54 (assertions: 37/37)

The same three files also pass with Neovim 0.12.6: Success/Total: 54/54 (assertions: 37/37).

vint and test/script/block-padding-checker report no issues for the changed files.

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.
@travisim
travisim requested a review from w0rp as a code owner November 27, 2026 22:47
Copilot AI balanced review requested due to automatic review settings November 27, 2026 22:47

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T22:50:57.570438Z fb85824 PR opened
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread autoload/ale/util.vim
let l:fileencoding = getbufvar(a:buffer, '&fileencoding')

if exists('*iconv') && !empty(l:fileencoding) && l:fileencoding isnot# &encoding
let l:corrected_lines = s:ConvertEncodings(

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Comment thread autoload/ale/util.vim
" the buffer.
function! ale#util#Readfile(buffer, filename) abort
let l:lines = readfile(a:filename)
let l:fileencoding = getbufvar(a:buffer, '&fileencoding')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linting fails on non-UTF-8 files because ale#util#Writefile always writes in Vim internal encoding

3 participants