VS Code errors: "Files beneath GOMODCACHE must not be replaced."
Describe the bug
Even in a minimal "hello world" project, the templ-vscode IDE integration fails with 61 error messages, all variations of:
initialization failed: packages.Load
error: err: exit status 1: stderr: go: overlay contains a replacement for
{PWD}/.direnv/go/pkg/mod/github.com/a-h/[email protected]/benchmarks/templ/template_templ.go.
Files beneath GOMODCACHE ({PWD}/.direnv/go/pkg/mod) must not be replaced.This is triggered by the project installing its Go dependencies in a sub-directory of the project itself ($PWD/.direnv in my case).
To Reproduce
git clone https://github.com/mislav/templtestcd templtest- Set up Go environment:
export GOPATH="$PWD/.direnv/go" export GOBIN="$GOPATH/bin" export PATH="$GOBIN:$PATH" go mod download go install github.com/a-h/templ/cmd/templ@latest code --profile templtest .(starts a fresh VS Code profile)- Install the "Go" VS Code extension
- Install the
templ-vscodeextension - Notice many errors eventually show up in VS Code's "Problems" tab, likely after
templ lspboots up.
I would say that the key trigger for this problem was setting GOPATH to a sub-directory of the project directory. I use direnv for managing environment variables for my various coding projects, and this GOPATH setup is the default behavior of direnv's layout go preset.
I was using this approach to manage my Go projects for years now without any problems, until a project I am a contributor to started using templ as its templating engine. Now I'm having difficulties getting the project to work in VS Code.
I think the problem here is that the templ LSP server is trying to process all *_templ.go files under the current project directory, but since GOPATH is under the current project directory too, that search also includes all *_templ.go files that the templ source code ships with, and that somehow causes the LSP integration to choke.
The templ source code contains 61 *_templ.go files and I think that's why I'm getting 61 error messages in VS Code.
$ find $GOPATH -name '*_templ.go' | wc -l
61Expected behavior
The templ-vscode integration works regardless of how and where Go dependencies were installed to.
Screenshots n/a
Logs
[Info - 12:19:28 PM] 2026/08/03 12:19:28 Created View (#1)
directory=/Users/mislav/Projects/templ-test
view_type="GoMod"
root_dir="file:///Users/mislav/Projects/templ-test"
go_version="go version go1.26.5 darwin/arm64"
build_flags=[]
env={GOOS:darwin GOARCH:arm64 GOCACHE:/Users/mislav/Library/Caches/go-build GOMODCACHE:/Users/mislav/Projects/templ-test/.direnv/go/pkg/mod GOPATH:/Users/mislav/Projects/templ-test/.direnv/go GOPRIVATE: GOFLAGS: GO111MODULE: GOTOOLCHAIN:auto GOROOT:/opt/homebrew/Cellar/go/1.26.5/libexec GoVersion:26 GoVersionOutput:go version go1.26.5 darwin/arm64
ExplicitGOWORK: EffectiveGOPACKAGESDRIVER:}
env_overlay=[]
[Info - 12:19:28 PM] 2026/08/03 12:19:28 go/packages.Load #1
view_id="1"
snapshot=0
directory=/Users/mislav/Projects/templ-test
query=[/Users/mislav/Projects/templ-test/... builtin]
packages=2
duration=104.490417ms
[Info - 12:19:28 PM] 2026/08/03 12:19:28 Created View (#2)
directory=/Users/mislav/Projects/templ-test
view_type="GoMod"
root_dir="file:///Users/mislav/Projects/templ-test/.direnv/go/pkg/mod/github.com/a-h/[email protected]"
go_version="go version go1.26.5 darwin/arm64"
build_flags=[]
env={GOOS:darwin GOARCH:arm64 GOCACHE:/Users/mislav/Library/Caches/go-build GOMODCACHE:/Users/mislav/Projects/templ-test/.direnv/go/pkg/mod GOPATH:/Users/mislav/Projects/templ-test/.direnv/go GOPRIVATE: GOFLAGS: GO111MODULE: GOTOOLCHAIN:auto GOROOT:/opt/homebrew/Cellar/go/1.26.5/libexec GoVersion:26 GoVersionOutput:go version go1.26.5 darwin/arm64
ExplicitGOWORK: EffectiveGOPACKAGESDRIVER:}
env_overlay=[]
[Error - 12:19:28 PM] 2026/08/03 12:19:28 go/packages.Load #2: err: exit status 1: stderr: go: overlay contains a replacement for /Users/mislav/Projects/templ-test/.direnv/go/pkg/mod/github.com/a-h/[email protected]/benchmarks/templ/template_templ.go. Files beneath GOMODCACHE (/Users/mislav/Projects/templ-test/.direnv/go/pkg/mod) must not be replaced.
view_id="2"
snapshot=0
directory=/Users/mislav/Projects/templ-test
query=[/Users/mislav/Projects/templ-test/.direnv/go/pkg/mod/github.com/a-h/[email protected]/... builtin]
packages=0
duration=11.186875ms
[Error - 12:19:28 PM] 2026/08/03 12:19:28 initial workspace load failed: packages.Load error: err: exit status 1: stderr: go: overlay contains a replacement for /Users/mislav/Projects/templ-test/.direnv/go/pkg/mod/github.com/a-h/[email protected]/benchmarks/templ/template_templ.go. Files beneath GOMODCACHE (/Users/mislav/Projects/templ-test/.direnv/go/pkg/mod) must not be replaced.
(... similar errors appear many times more)templ info output
$ templ info
(✓) os [ goos=darwin goarch=arm64 ]
(✓) go [ location=/opt/homebrew/bin/go version=go version go1.26.5 darwin/arm64 ]
(✓) gopls [ location=/Users/mislav/Projects/templ-test/.direnv/go/bin/gopls version=golang.org/x/tools/gopls v0.23.0 ]
(✓) templ [ location=/Users/mislav/Projects/templ-test/.direnv/go/bin/templ version=v0.3.1020 ]
(!) prettier [ location= version= message=failed to find prettier: exec: "prettier": executable file not found in $PATH ]Desktop (please complete the following information):
This information is already answered by the templ info output above
Source: a-h/templ