templ: VScode extension lsp error

I’m getting the following error on the extension start up

[Error - 9:45:21 PM] Stopping server failed
Error: Client is not running and can't be stopped. It's current state is: starting
    at LanguageClient.shutdown (/home/andrii/.vscode/extensions/a-h.templ-0.0.14/node_modules/vscode-languageclient/lib/common/client.js:914:1)
    at LanguageClient.stop (/home/andrii/.vscode/extensions/a-h.templ-0.0.14/node_modules/vscode-languageclient/lib/common/client.js:885:1)
    at LanguageClient.stop (/home/andrii/.vscode/extensions/a-h.templ-0.0.14/node_modules/vscode-languageclient/lib/node/main.js:150:1)
    at LanguageClient.handleConnectionError (/home/andrii/.vscode/extensions/a-h.templ-0.0.14/node_modules/vscode-languageclient/lib/common/client.js:1146:1)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
[Error - 9:45:21 PM] Server initialization failed.
  Message: write EPIPE
  Code: -32099 
[Error - 9:45:21 PM] templ client: couldn't create connection to server.
  Message: write EPIPE
  Code: -32099 
[Error - 9:45:21 PM] Connection to server got closed. Server will not be restarted.

About this issue

  • Original URL
  • State: open
  • Created 9 months ago
  • Comments: 23 (8 by maintainers)

Most upvoted comments

Thanks @kingjulienboss - @alexpower2002 / @WanderningMaster - is that the same issue for you?

The troubleshooting steps are:

Check that go, gopls and templ are installed and are present in the path.

which go gopls templ

You should see 3 lines returned, showing the location of each binary:

/run/current-system/sw/bin/go
/Users/adrian/go/bin/gopls
/Users/adrian/bin/templ

Enable LSP logging

Use the “Preferences: Open User Settings (JSON)” command in VS Code and enable logging in templ.

{
    // More settings...
    "templ.log": "/Users/adrian/logs/vscode-templ.txt",
    "templ.goplsLog": "/Users/adrian/logs/vscode-gopls.txt",
    "templ.http": "localhost:7575",
    "templ.goplsRPCTrace": true,
    "templ.pprof": false,
    // More stuff...
}

Start up the project and include the logs

Bear in mind that your code will be included in the logs. Try to make a minimal reproduction if possible.

I’ve updated the probing to include checks for gopls.exe as well as gopls in https://github.com/a-h/templ/commit/b6f0b3dad93a476f82657cf778ce7f7cef618752

I had same problems with the plugin for Goland. I Fixed it with the following steps:

  1. Make sure go is installed globally (not only in Goland IDE)
  2. Make sure the $PATH includes the folder that go install installs the binaries to. (for me it was /User/{username}/go/bin)
  3. Make sure gopls is also manually installed

maybe it would be nice to have a error message that gopls needs to be installed when starting the lsp server, otherwise it just exits without any message

I am having a related issue on arch:

Launching server using command templ failed. Error: spawn templ ENOENT

https://berms.onlyfans.je/cb0c7ff4-7cfa-4f71-ac09-630b41f7bf13.png

@a-h I would also add the part of adding to bin path to the profile. In my case gopls and templ was working inside the terminal but not inside the extension. Might be because vscode uses some some other user to run the extensions to isolate it from other folders.

Hi folks, just to help with the mental model of how the VS Code LSP works…

The main task that the extension does is to run the templ lsp command.

When the VS Code extension runs, it shows an information message to tell you what it’s going to execute at the command line, see https://github.com/a-h/templ-vscode/blob/aa9495fc6d1b0d132b19e8c5b1e6e362b94aa39d/src/main.ts#L67

vscode.window.showInformationMessage(`Starting LSP: templ ${args.join(' ')}`)

If you can’t run this command line yourself, then that’s the main thing to fix. So…

  • If you can’t run the templ command at the command line:
    • Check that the templ binary is on your path.
    • On MacOS / Linux, check that the file is executable and resolve it with chmod +x /path/to/templ.
    • On MacOS, you might need to go through the steps at https://support.apple.com/en-gb/guide/mac-help/mh40616/mac to enable binaries from an “unidentified developer” to run.
  • If you’re running VS Code using WSL, then templ must also be installed within the WSL environment, not just inside your Windows environment.
  • If you’re running VS Code in a Devcontainer, it must be installed in there.

I’ll add these additional troubleshooting steps to the docs.

@laneherby you can check if the path /home/herby/go/bin is included in $PATH. You can check using echo $PATH.

Thank you all guys @a-h @kingjulienboss, at least for me copying gopls binary to /usr/bin did the trick