This is a followup from vscode-go/#191.
Go uses tabs for indention, and spaces for alignment. When I open a .go file and press tab, for example selecting a block of text and tabbing to indent, spaces are inserted instead. I can't think of a situation when I want the tab key to insert spaces in a .go file. Can the autodetection algorithm be improved in this case?
(As an aside, thanks for this project! I'm really enjoying it so far.)
Can you pls add a .go file as an example where the guessing was wrong? As you describe it, go files should have tabs and the guessing should pick tabs as well.
Create a .go file with e.g.
package main
func main() {
println("hello")
}
Note the tab literal prefixing println. Position the cursor after the final closing }, press enter twice, and type
func foobar() {
and press enter. You'll get
func foobar() {
····_
}
i.e. the autoindent will be with spaces.
Similarly, given
package main
type a struct {
i int
s string
f float64
}
type b struct {
hello string
world string
}
Try to type a new struct definition
type c struct {<ENTER>
····a int<ENTER>
····b int<ENTER>
····_
}
And GIF form! When I save, gofmt (actually goimports) is automatically applied, which is why you see the spaces turn into tabs.

Thanks for all the steps, but I cannot reproduce this in the insider build.
See below, only tabs are inserted with the settings shown on the right.
Assigning to @alexandrudima may be something changed since 0.10.6.

OK! I'll test on the next release.
@alexandrudima mentioned to me that the issue still exist. I'm must have tried on an older version.
@peterbourgon @egamma Currently, "auto" indentation settings are evaluated only once, on file open. So if the file is indented with spaces when it is opened, the guess will be to insert spaces. Further edits that bring in more tabs in a file are not taken into account until the file is closed (from the working files view) and the file is reopened.
@alexandrudima Go files are always indented with tabs, aligned with spaces. So, given your description of the algorithm, I would expect Code to detect tab indentation when opening Go files. — But I suggest a different heuristic: that "auto" always evaluate to tabs-for-indent when opening .go files, superseding file content analysis.
edit: for the record, using the normal (?) Code build (installed via brew cask) I'm still getting the original/broken behavior.
@peterbourgon I am sorry for the bad behaviour. We currently don't have per-file type settings, but there is something great that we work with: .editorconfig.
And the VSCode extension:
https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig
Thanks for the comment. No need to apologize. An extension like that is probably inappropriate to use for Go, as in Go there is nothing to configure: the language strictly defines valid code format rules, including but not limited to tabs for indentation.
Changed the title. According to @peterbourgon all .go files should always be indented with tabs, the go compiler cannot compile files indented with spaces. I guess this is similar to yaml, where yaml processors cannot handle files indented with tabs.
the go compiler cannot compile files indented with spaces
To be clear, it will compile, but it is not canonical format.
The Go language spec does not seem to touch on the topic of preferring tabs vs spaces for indentation, it simply defines them as whitespace and then defines the semantics of whitespace while parsing source code.
If I understood how this works, the go compiler ships with a formatter that prefers tabs, which is great.
But given this is not specified in the language, and the compiler can compile files indented with spaces (as per the language spec), I'm not sure what you would expect VSCode to do special regarding .go files.
If I understood how this works, the go compiler ships with a formatter that prefers tabs, which is great.
It's much stronger than "prefers" — it's more like canon. To some order of approximation, zero percent of Go code in the wild has any other format than the canonical gofmt style. I understand this is different than many other programming ecosystems.
I'm not sure what you would expect VSCode to do special regarding .go files.
When VSCode encounters a .go file, it should use tabs for indentation, and spaces for alignment within that file. This behavior should not be configurable, nor should it rely on the user installing/configuring an external plugin. Concretely, if this doesn't happen, Go users of VSCode (using the excellent vscode-go plugin) will observe indentation spaces replaced with tabs every time they save their files. Bad UX.
Maybe this is something the Go extension could do.
@lukehoban This snippet in the go extension would ensure all .go files use tabs and not spaces:
vscode.window.onDidChangeActiveTextEditor((editor) => {
if (editor && editor.document.languageId === 'go' && editor.options.insertSpaces) {
editor.options = {
insertSpaces: false
};
}
});
Would be happy with that!
This can be done via language specific settings. e.g.:

Marking as fixed based on comment https://github.com/microsoft/vscode/issues/2765#issuecomment-304635106
Most helpful comment
This can be done via language specific settings. e.g.: