在VSCode中未检测到.env文件。

huangapple go评论129阅读模式
英文:

env file is not detected in vscode

问题

我有一个名为my-project的多根工作区:

- my-project
   - my-project-api-gateway
   - another-root-folder-we-dont-care-about

这是我针对my-project-api-gatewaylaunch.json配置文件:

{
  // 请参阅 https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes
  // 和这里的变量 https://code.visualstudio.com/docs/editor/variables-reference
  "version": "0.2.0",
  "configurations": [
    {
      "name": "my-project-api-gateway",
      "type": "go",
      "request": "launch",
      "mode": "debug",
      "envFile": "${workspaceFolder}/.env", ----> 加载 .env 文件时出错
      "program": "${workspaceFolder}/src/main.go"
    }
  ]
}

DEBUG 控制台输出:

Starting: /Users/me/go/bin/dlv dap --check-go-version=false --listen=127.0.0.1:53890 --log-dest=3 from /Users/me/path/to/project/src
DAP server listening at: 127.0.0.1:53890
Type 'dlv help' for list of commands.
2022/07/20 10:30:52 Error loading .env file
Process 13526 has exited with status 1
Detaching
dlv dap (13508) exited with code: 0

谢谢你的帮助。


dlv、go、gopls 和 vscode 版本

❯ dlv version
Delve Debugger
Version: 1.8.2

❯ go version
go version go1.18 darwin/amd64

❯ gopls -v version
Build info
----------
golang.org/x/tools/gopls v0.9.1

❯ code -v
1.69.2

扩展程序 Go for Visual Studio Code 的版本是 v0.35.1

Go: Locate Configured Go Tools 输出:

Checking configured tools....
GOBIN: /Users/me/go/bin
toolsGopath: 
gopath: /Users/me/dev/go
GOROOT: /usr/local/go
PATH: /usr/local/go/bin:/Users/me/.nvm/versions/node/v16.14.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Postgres.app/Contents/Versions/latest/bin:/Users/me/.cargo/bin:/Users/me/go/bin:/Applications/Postgres.app/Contents/Versions/latest/bin:/usr/local/cassandra/bin
PATH (vscode launched with): /Users/me/.nvm/versions/node/v16.14.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Applications/Postgres.app/Contents/Versions/latest/bin:/Users/me/.cargo/bin:/Users/me/go/bin:/Applications/Postgres.app/Contents/Versions/latest/bin:/usr/local/cassandra/bin

	go:	/usr/local/go/bin/go: go version go1.18 darwin/amd64

	gotests:	/Users/me/go/bin/gotests	(version: v1.6.0 built with go: go1.18)
	gomodifytags:	/Users/me/go/bin/gomodifytags	(version: v1.16.0 built with go: go1.18)
	impl:	/Users/me/go/bin/impl	(version: v1.1.0 built with go: go1.18)
	goplay:	/Users/me/go/bin/goplay	(version: v1.0.0 built with go: go1.18)
	dlv:	/Users/me/go/bin/dlv	(version: v1.8.2 built with go: go1.18)
	golangci-lint:	/Users/me/go/bin/golangci-lint	(version: v1.45.2 built with go: go1.18)
	gopls:	/Users/me/go/bin/gopls	(version: v0.9.1 built with go: go1.18)

go env
Workspace Folder (my-project-api-gateway): /Users/me/dev/back/go/my-project/my-project-api-gateway
	GO111MODULE=""
	GOARCH="amd64"
	GOBIN="/Users/me/go/bin"
	GOCACHE="/Users/me/Library/Caches/go-build"
	GOENV="/Users/me/Library/Application Support/go/env"
	GOEXE=""
	GOEXPERIMENT=""
	GOFLAGS=""
	GOHOSTARCH="amd64"
	GOHOSTOS="darwin"
	GOINSECURE=""
	GOMODCACHE="/Users/me/dev/go/pkg/mod"
	GONOPROXY="github.com/BigBoulard/*"
	GONOSUMDB="github.com/BigBoulard/*"
	GOOS="darwin"
	GOPATH="/Users/me/dev/go"
	GOPRIVATE="github.com/BigBoulard/*"
	GOPROXY="https://proxy.golang.org,direct"
	GOROOT="/usr/local/go"
	GOSUMDB="sum.golang.org"
	GOTMPDIR=""
	GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
	GOVCS=""
	GOVERSION="go1.18"
	GCCGO="gccgo"
	GOAMD64="v1"
	AR="ar"
	CC="clang"
	CXX="clang++"
	CGO_ENABLED="1"
	GOMOD="/Users/me/dev/back/go/my-project/my-project-api-gateway/go.mod"
	GOWORK=""
	CGO_CFLAGS="-g -O2"
	CGO_CPPFLAGS=""
	CGO_CXXFLAGS="-g -O2"
	CGO_FFLAGS="-g -O2"
	CGO_LDFLAGS="-g -O2"
	PKG_CONFIG="pkg-config"
	GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/1g/fytltrn578gg3xgp3c_sglhw0000gq/T/go-build1439123167=/tmp/go-build -gno-record-gcc-switches -fno-common"
英文:

I have a my-project multi-root workspace:

- my-project
   - my-project-api-gateway
   - another-root-folder-we-dont-care-about

Here's my launch.json configuration file for my-project-api-gateway

{
  // See https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes
  // and variables here https://code.visualstudio.com/docs/editor/variables-reference
  "version": "0.2.0",
  "configurations": [
    {
      "name": "my-project-api-gateway",
      "type": "go",
      "request": "launch",
      "mode": "debug",
      "envFile": "${workspaceFolder}/.env", ----> Error loading .env file
      "program": "${workspaceFolder}/src/main.go"
    }
  ]
}

DEBUG Console Output

Starting: /Users/me/go/bin/dlv dap --check-go-version=false --listen=127.0.0.1:53890 --log-dest=3 from /Users/me/path/to/project/src
DAP server listening at: 127.0.0.1:53890
Type 'dlv help' for list of commands.
2022/07/20 10:30:52 Error loading .env file
Process 13526 has exited with status 1
Detaching
dlv dap (13508) exited with code: 0

Thanks for your help


dlv, go, gopls and vscode versions

❯ dlv version
Delve Debugger
Version: 1.8.2

❯ go version
go version go1.18 darwin/amd64

❯ gopls -v version
Build info
----------
golang.org/x/tools/gopls v0.9.1

❯ code -v
1.69.2

Extension Go for Visual Studio Code is v0.35.1

Go: Locate Configured Go Tools output:

Checking configured tools....
GOBIN: /Users/me/go/bin
toolsGopath: 
gopath: /Users/me/dev/go
GOROOT: /usr/local/go
PATH: /usr/local/go/bin:/Users/me/.nvm/versions/node/v16.14.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Postgres.app/Contents/Versions/latest/bin:/Users/me/.cargo/bin:/Users/me/go/bin:/Applications/Postgres.app/Contents/Versions/latest/bin:/usr/local/cassandra/bin
PATH (vscode launched with): /Users/me/.nvm/versions/node/v16.14.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Applications/Postgres.app/Contents/Versions/latest/bin:/Users/me/.cargo/bin:/Users/me/go/bin:/Applications/Postgres.app/Contents/Versions/latest/bin:/usr/local/cassandra/bin

	go:	/usr/local/go/bin/go: go version go1.18 darwin/amd64

	gotests:	/Users/me/go/bin/gotests	(version: v1.6.0 built with go: go1.18)
	gomodifytags:	/Users/me/go/bin/gomodifytags	(version: v1.16.0 built with go: go1.18)
	impl:	/Users/me/go/bin/impl	(version: v1.1.0 built with go: go1.18)
	goplay:	/Users/me/go/bin/goplay	(version: v1.0.0 built with go: go1.18)
	dlv:	/Users/me/go/bin/dlv	(version: v1.8.2 built with go: go1.18)
	golangci-lint:	/Users/me/go/bin/golangci-lint	(version: v1.45.2 built with go: go1.18)
	gopls:	/Users/me/go/bin/gopls	(version: v0.9.1 built with go: go1.18)

go env
Workspace Folder (my-project-api-gateway): /Users/me/dev/back/go/my-project/my-project-api-gateway
	GO111MODULE=""
	GOARCH="amd64"
	GOBIN="/Users/me/go/bin"
	GOCACHE="/Users/me/Library/Caches/go-build"
	GOENV="/Users/me/Library/Application Support/go/env"
	GOEXE=""
	GOEXPERIMENT=""
	GOFLAGS=""
	GOHOSTARCH="amd64"
	GOHOSTOS="darwin"
	GOINSECURE=""
	GOMODCACHE="/Users/me/dev/go/pkg/mod"
	GONOPROXY="github.com/BigBoulard/*"
	GONOSUMDB="github.com/BigBoulard/*"
	GOOS="darwin"
	GOPATH="/Users/me/dev/go"
	GOPRIVATE="github.com/BigBoulard/*"
	GOPROXY="https://proxy.golang.org,direct"
	GOROOT="/usr/local/go"
	GOSUMDB="sum.golang.org"
	GOTMPDIR=""
	GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
	GOVCS=""
	GOVERSION="go1.18"
	GCCGO="gccgo"
	GOAMD64="v1"
	AR="ar"
	CC="clang"
	CXX="clang++"
	CGO_ENABLED="1"
	GOMOD="/Users/me/dev/back/go/my-project/my-project-api-gateway/go.mod"
	GOWORK=""
	CGO_CFLAGS="-g -O2"
	CGO_CPPFLAGS=""
	CGO_CXXFLAGS="-g -O2"
	CGO_FFLAGS="-g -O2"
	CGO_LDFLAGS="-g -O2"
	PKG_CONFIG="pkg-config"
	GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/1g/fytltrn578gg3xgp3c_sglhw0000gq/T/go-build1439123167=/tmp/go-build -gno-record-gcc-switches -fno-common"

答案1

得分: 2

这里的技巧是,我的 .env 文件位于工作空间文件夹下:${workspaceFolder}/.env,但是 delve 不使用 ${workspaceFolder} 作为 当前工作目录,所以我使用 os.Getwd() 来获取这里的 .env 文件。

load_eng.go 的代码片段如下:

curDir, err := os.Getwd()
if err != nil {
	log.Println(err)
}
loadErr := godotenv.Load(curDir + "/.env")
if loadErr != nil {
  log.Fatalln("无法从当前目录加载环境文件:" + curDir)
}

因此,我只需要在我的 launch.json 文件中添加 "cwd": "${workspaceFolder}" 来设置当前工作目录 $cwd

"configurations": [
    {
      "name": "My Application",
      "type": "go",
      "request": "launch",
      "mode": "debug",
      "envFile": "${workspaceFolder}/.env",
      "program": "${workspaceFolder}/src/main.go",
      "cwd": "${workspaceFolder}"
    }
  ]

有用的资源:

<https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables>

<https://github.com/golang/vscode-go/wiki/debugging>

英文:

The trick here is that my .env is under my workspace folder: ${workspaceFolder}/.env, but delve doesn't use ${workspaceFolder} as the current working directory that why I'm using os.Getwd() to fetch the .env from here.

load_eng.go excerpt below:

curDir, err := os.Getwd()
if err != nil {
	log.Println(err)
}
loadErr := godotenv.Load(curDir + &quot;/.env&quot;)
if loadErr != nil {
  log.Fatalln(&quot;can&#39;t load env file from current directory: &quot; + curDir)
}

So, I just had to set the current working directory $cwd by adding &quot;cwd&quot;: &quot;${workspaceFolder}&quot; in my launch.json file:

&quot;configurations&quot;: [
    {
      &quot;name&quot;: &quot;My Application&quot;,
      &quot;type&quot;: &quot;go&quot;,
      &quot;request&quot;: &quot;launch&quot;,
      &quot;mode&quot;: &quot;debug&quot;,
      &quot;envFile&quot;: &quot;${workspaceFolder}/.env&quot;,
      &quot;program&quot;: &quot;${workspaceFolder}/src/main.go&quot;,
      &quot;cwd&quot;: &quot;${workspaceFolder}&quot;
    }
  ]

Useful resources:

<https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables>

<https://github.com/golang/vscode-go/wiki/debugging>

huangapple
  • 本文由 发表于 2022年7月20日 17:00:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/73048696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定