microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
162.36k stars 28.6k forks source link

The git stage operation will change the file encoding #183769

Open victorwwd opened 1 year ago

victorwwd commented 1 year ago

Does this issue occur when all extensions are disabled?: Yes

Steps to Reproduce:

  1. The encoding of source file is GBK;
  2. Open Changes View, stage via 'Stage Selected Ranges'
  3. The encoding will be changed to UTF-8

If ‘stage changes’ the entire file, there will be no such issue. VSCode default encoding now is set to GBK.

VSCodeTriageBot commented 1 year ago

Thanks for creating this issue! It looks like you may be using an old version of VS Code, the latest stable release is 1.78.2. Please try upgrading to the latest version and checking whether this issue remains.

Happy Coding!

victorwwd commented 1 year ago

Can this issue be resolved?

lszomoru commented 1 year ago

@BiiigPanda, thanks for reporting this issue. I have tried to reproduce the issue based on your steps without any luck. do you mind sharing a recording (https://gifcap.dev) as I might have missed something. Thank you!

victorwwd commented 1 year ago

My description was inaccurate.

It didn't change the encoding of the original file, but when I use 'git diff' or 'git format-patch', it displays as UTF-8, and the Git history preview in VSCode is also displayed as UTF-8. abnormal

At the same time, if I stage the entire file(not 'Stage Selected Ranges'), this situation will not occur. ok

lszomoru commented 1 year ago

@BiiigPanda, is the problem with the content of the hover as looking at the file encoding in the status bar I always see that the file is using "GB2312". I am asking as the hover is contributed by the GitLens extension. Are you able to reproduce the problem with the GitLens extension disabled? Thanks!

victorwwd commented 1 year ago

If you look at the encoding in the status bar, it might be clearer to explain the operation in a different way. The following GIFs show the operation for 'Stage Selected Ranges' and 'Stage Changes', respectively. After staging is completed, you can view the differences using VSCode and notice that the encoding of the file has changed to UTF-8 in the diff view. utf gbk

maaarghk commented 11 months ago

More data points: I'm seeing the same behaviour on Windows-1252 and with files.autoGuessEncoding enabled. It correctly opens the file as Windows-1252, does not change the encoding on save (either normal save or save without formatting), does not change the encoding when the Stage Changes button is used.

Unstage is also affected: If I stage the entire file using Stage Changes, then in the Staged view, "Unstage selected range" the only change, you'd expect no changes to remain staged. However the staged file is converted to UTF-8, and the checked out file is not. (i.e. - a conversion of the file from HEAD to UTF-8 is staged, and the local changes remain unstaged, in Windows-1252, so the diff shows a conversion of the file back from UTF-8 to Windows-1252).

Pretty frustrating bug!

augustsaintfreytag commented 7 months ago

I can confirm this also happens with Windows-1251 Cyrillic, an encoding I need to write code for a certain old Ukrainian game engine. Even disabling autoGuessEncoding and setting it fixed to windows1251 in workspace and folder settings, opening the file diff will always show non-ASCII characters as changed, left side (committed) looks garbled (e.g. local DOT = "•") while the right side looks as expected (e.g. local DOT = "•").

The only way to work around this is to skip staging the affected parts line by line and eventually commit the whole file at once. Obviously if I had more than just a few non-ASCII characters in my code, this wouldn't even be an option.

Also, #155658, #149635 may be duplicates, there are probably more.

Scorg commented 2 months ago

Can confirm this unfixed as of 1.90.1

izacsc commented 1 month ago

Any updates on this? It's been a long time since I firts reported it on https://github.com/microsoft/vscode/issues/155658

victorwwd commented 9 hours ago

This issue still exists on version 1.93.0, @lszomoru