Repository: vscode
Issue: Language Detection crashes language server due to misdetection.
Labels: feature-request,languages-guessing

<!-- ⚠️⚠️ Do Not Delete This! bug_report_template ⚠️⚠️ -->
<!-- Please read our Rules of Conduct: https://opensource.microsoft.com/codeofconduct/ -->
<!-- 🕮 Read our guide about submitting issues: https://github.com/microsoft/vscode/wiki/Submitting-Bugs-and-Suggestions -->
<!-- 🔎 Search existing issues to avoid creating duplicates. -->
<!-- 🧪 Test using the latest Insiders build to see if your issue has already been fixed: https://code.visualstudio.com/insiders/ -->
<!-- 💡 Instead of creating your report here, use 'Report Issue' from the 'Help' menu in VS Code to pre-fill useful information. -->
<!-- 🔧 Launch with `code --disable-extensions` to check. -->
Does this issue occur when all extensions are disabled?: Yes

<!-- 🪓 If you answered No above, use 'Help: Start Extension Bisect' from Command Palette to try to identify the cause. -->
<!-- 📣 Issues caused by an extension need to be reported directly to the extension publisher. The 'Help > Report Issue' dialog can assist with this. -->
- VS Code Version: 1.64.2
- OS Version: macOS 12.01

Steps to Reproduce:

1. Make a workspace with below included script:
```sh
#!/bin/bash
mkdir pob; if [[ $? != 0 ]]; then 
cat<<EOF
  Could not create a new ./pob directory!
  Possibly it already exists, you should rm -rf it first.
EOF

else; cd pob
cat>plain<<EOF
/*
#!/bin/false

Settings: "gopls": {
    "build.experimentalWorkspaceModule": true,
    "verboseOutput": true,
},
"go.languageServerFlags": [
    "-rpc.trace"
],
Settings: "gopls": {
    "build.experimentalWorkspaceModule": true,
    "verboseOutput": true,
},
"go.languageServerFlags": [
    "-rpc.trace"
],
"[go]": {
    "editor.insertSpaces": false,
    "editor.formatOnSave": true,
    "editor.suggest.snippetsPreventQuickSuggestions": false,
},

t := [...]byte{ 0xFF, 0x7F, 0x6F, 0x5F }

*/

t := [...]byte{0xFF, 0x7F, 0x6F, 0x5F}

EOF

cat>go.mod<<EOF
module pob

go 1.17
EOF

cat>pob.go<<EOF
package main

import "fmt"

func main() {
	fmt.Println("Proof Of Bug!")
}
EOF

cat>pob.code-workspace<<EOF
{
    "folders": [
        {
            "path": "."
        }
    ],
}
EOF

loc=`pwd`
cat<<EOF
------------------------------------------------------------------------------

Now open VSCode workspace in $loc

Next do some edits, or wait for a while to see "intelligence" going astray.

Ie. VSC not only looks into the plain file, but it also tries to lint it
regardless of there being no extension, commented content, even a she-bang.
Not good, euphemistically spoken.

The Go project is there, try add some more go stuff above json then once for
a while VSC will "think" it should feed "plain" to the gopls and possibly crash it.
(if "Go" detection will win the race, otherwise json formatter/linter kicks in)

[ That's how I had stumbled upon this bug - preparing PoB for gopls crashings.]
[ I added config snippets, then suddenly VSC "recognized" report text as json.]

VSCode version: 1.64.2

For the PoB all extensions were disabled for the pob workspace. 
For gopls crashing PoB (as it turned out due to a non-go file fed to it)
these extensions were enabled:
  golang.go-0.31.1
  vscodevim.vim-1.22.1

------------------------------------------------------------------------------

EOF

cd ..
fi
```
*Bash script to be run on linux/macos or Windows WSL*.

2. Open workspace, wait a while.
3. See "plain" file recognized as json
4. move she-bang line to the top - VSCode still "thinks" it is a json file, because it found some json **within the comments**, so it now whines that *Comments are not permitted in JSON.*

Related: #133506, #132173 - there is a non exhaustive list of other related reports.

Edit 02/16/2021 - another possibly related: #143145

---
Replying here _to @TylerLeonhardt in https://github.com/microsoft/vscode/issues/133506#issuecomment-928414654_
@TylerLeonhardt wrote:

> The bug is that the editor is getting first treated as a plaintext file... which triggers auto-detection... then it's set to makefile as vscode finishes starting up... but auto-detect was already triggered so it gets changed to python.
>
> I'm wondering if I can somehow have language detection wait some amount of time before it's run for the first time... Maybe a global promise that resolves after X seconds that language detection waits on.

> @bpasero are you aware of any mechanisms for doing what I wanna do here? Or any advice?

I am aware, and I have an advice:

Before any heuristics voo-doo just respect human's will as expressed in the shebang clause.

Shebang line is used for identifying purposes for more than forty-two years still, and already has proven its usability. Ie. if there is a shebang that VSCode does not know about, this is an ultimate end of the heuristics. Yet unrecognizable shebang tells us that this is *a plaintext file of an unknown structure meant for an unknown interpreter*. Thus it MAY NOT be guessed at further, the least it being fed to an unfit pls/formatter/linter chosen at random.

Hope this helps,

> From dmr Thu Jan 10 04:25:49 1980 remote from research
>
> The system has been changed so that if a file being executed
> begins with the magic characters #! , the rest of the line is understood
> to be the name of an interpreter for the executed file.