Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report UTF-16 encoding of Go source file as an error #3690

Open
dolmen opened this issue Feb 21, 2025 · 1 comment
Open

Report UTF-16 encoding of Go source file as an error #3690

dolmen opened this issue Feb 21, 2025 · 1 comment
Milestone

Comments

@dolmen
Copy link

dolmen commented Feb 21, 2025

Is your feature request related to a problem? Please describe.

The Go language specification allows only UTF-8 as a valid encoding of source files. Spec: https://go.dev/ref/spec#Source_code_representation

However VS Code currently doesn't report a .go file not encoded as UTF-8. As a result, if a source code has been created outside VS Code as UTF-16 and is edited with VS code, editing works fine, but compiling doesn't.

Describe the solution you'd like

  • When opening a .go file, alert if encoding is not UTF-8.
  • Set an error on line 1 about the encoding (maybe a task for gopls?).

Additional context
See also golang/go#71872 about the compiler not being kind to diagnose this encoding issue.

Use case: a Go source file incorrectly created on Windows using the echo command from Windows PowerShell.

Windows PowerShell
Copyright (C) Microsoft Corporation. Tous droits réservés.

Installez la dernière version de PowerShell pour de nouvelles fonctionnalités et améliorations ! https://aka.ms/PSWindows

PS C:\Users\dolmen> cd Code
PS C:\Users\dolmen\Code> mkdir hello


    Répertoire : C:\Users\dolmen\Code


Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d-----        21/02/2025     13:46                hello


PS C:\Users\dolmen\Code> cd hello
PS C:\Users\dolmen\Code\hello> go mod init github.com/dolmen-go/hello
go: creating new go.mod: module github.com/dolmen-go/hello
PS C:\Users\dolmen\Code\hello> echo "package main" > hello.go
PS C:\Users\dolmen\Code\hello> type hello.go
package main
PS C:\Users\dolmen\Code\hello> go build .
read C:\Users\dolmen\Code\hello\hello.go: unexpected NUL in input
PS C:\Users\dolmen\Code\hello> Format-Hex hello.go


           Chemin d'accès : C:\Users\dolmen\Code\hello\hello.go

           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   FF FE 70 00 61 00 63 00 6B 00 61 00 67 00 65 00  .þp.a.c.k.a.g.e.
00000010   20 00 6D 00 61 00 69 00 6E 00 0D 00 0A 00         .m.a.i.n.....


PS C:\Users\dolmen\Code\hello> go fmt .
read C:\Users\dolmen\Code\hello\hello.go: unexpected NUL in input

Screenshot of VS Code showing the hello.go file encoded as UTF-16 LE (bottom right) and no error reported. Sorry about the french locale.
Image

@gopherbot gopherbot added this to the Untriaged milestone Feb 21, 2025
@findleyr
Copy link
Member

I'm actually surprised that gopls doesn't fail in this case. Could you check if go list succeeds?

Ideally, go list would report an error, which gopls would surface as a diagnostic or popup.

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

No branches or pull requests

3 participants