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

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

env file is not detected in vscode

问题

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

  1. - my-project
  2. - my-project-api-gateway
  3. - another-root-folder-we-dont-care-about

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

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

DEBUG 控制台输出:

  1. 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
  2. DAP server listening at: 127.0.0.1:53890
  3. Type 'dlv help' for list of commands.
  4. 2022/07/20 10:30:52 Error loading .env file
  5. Process 13526 has exited with status 1
  6. Detaching
  7. dlv dap (13508) exited with code: 0

谢谢你的帮助。


dlv、go、gopls 和 vscode 版本

  1. dlv version
  2. Delve Debugger
  3. Version: 1.8.2
  4. go version
  5. go version go1.18 darwin/amd64
  6. gopls -v version
  7. Build info
  8. ----------
  9. golang.org/x/tools/gopls v0.9.1
  10. code -v
  11. 1.69.2

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

Go: Locate Configured Go Tools 输出:

  1. Checking configured tools....
  2. GOBIN: /Users/me/go/bin
  3. toolsGopath:
  4. gopath: /Users/me/dev/go
  5. GOROOT: /usr/local/go
  6. 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
  7. 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
  8. go: /usr/local/go/bin/go: go version go1.18 darwin/amd64
  9. gotests: /Users/me/go/bin/gotests (version: v1.6.0 built with go: go1.18)
  10. gomodifytags: /Users/me/go/bin/gomodifytags (version: v1.16.0 built with go: go1.18)
  11. impl: /Users/me/go/bin/impl (version: v1.1.0 built with go: go1.18)
  12. goplay: /Users/me/go/bin/goplay (version: v1.0.0 built with go: go1.18)
  13. dlv: /Users/me/go/bin/dlv (version: v1.8.2 built with go: go1.18)
  14. golangci-lint: /Users/me/go/bin/golangci-lint (version: v1.45.2 built with go: go1.18)
  15. gopls: /Users/me/go/bin/gopls (version: v0.9.1 built with go: go1.18)
  16. go env
  17. Workspace Folder (my-project-api-gateway): /Users/me/dev/back/go/my-project/my-project-api-gateway
  18. GO111MODULE=""
  19. GOARCH="amd64"
  20. GOBIN="/Users/me/go/bin"
  21. GOCACHE="/Users/me/Library/Caches/go-build"
  22. GOENV="/Users/me/Library/Application Support/go/env"
  23. GOEXE=""
  24. GOEXPERIMENT=""
  25. GOFLAGS=""
  26. GOHOSTARCH="amd64"
  27. GOHOSTOS="darwin"
  28. GOINSECURE=""
  29. GOMODCACHE="/Users/me/dev/go/pkg/mod"
  30. GONOPROXY="github.com/BigBoulard/*"
  31. GONOSUMDB="github.com/BigBoulard/*"
  32. GOOS="darwin"
  33. GOPATH="/Users/me/dev/go"
  34. GOPRIVATE="github.com/BigBoulard/*"
  35. GOPROXY="https://proxy.golang.org,direct"
  36. GOROOT="/usr/local/go"
  37. GOSUMDB="sum.golang.org"
  38. GOTMPDIR=""
  39. GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
  40. GOVCS=""
  41. GOVERSION="go1.18"
  42. GCCGO="gccgo"
  43. GOAMD64="v1"
  44. AR="ar"
  45. CC="clang"
  46. CXX="clang++"
  47. CGO_ENABLED="1"
  48. GOMOD="/Users/me/dev/back/go/my-project/my-project-api-gateway/go.mod"
  49. GOWORK=""
  50. CGO_CFLAGS="-g -O2"
  51. CGO_CPPFLAGS=""
  52. CGO_CXXFLAGS="-g -O2"
  53. CGO_FFLAGS="-g -O2"
  54. CGO_LDFLAGS="-g -O2"
  55. PKG_CONFIG="pkg-config"
  56. 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:

  1. - my-project
  2. - my-project-api-gateway
  3. - another-root-folder-we-dont-care-about

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

  1. {
  2. // See https://code.visualstudio.com/docs/editor/debugging#_launchjson-attributes
  3. // and variables here https://code.visualstudio.com/docs/editor/variables-reference
  4. "version": "0.2.0",
  5. "configurations": [
  6. {
  7. "name": "my-project-api-gateway",
  8. "type": "go",
  9. "request": "launch",
  10. "mode": "debug",
  11. "envFile": "${workspaceFolder}/.env", ----> Error loading .env file
  12. "program": "${workspaceFolder}/src/main.go"
  13. }
  14. ]
  15. }

DEBUG Console Output

  1. 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
  2. DAP server listening at: 127.0.0.1:53890
  3. Type 'dlv help' for list of commands.
  4. 2022/07/20 10:30:52 Error loading .env file
  5. Process 13526 has exited with status 1
  6. Detaching
  7. dlv dap (13508) exited with code: 0

Thanks for your help


dlv, go, gopls and vscode versions

  1. dlv version
  2. Delve Debugger
  3. Version: 1.8.2
  4. go version
  5. go version go1.18 darwin/amd64
  6. gopls -v version
  7. Build info
  8. ----------
  9. golang.org/x/tools/gopls v0.9.1
  10. code -v
  11. 1.69.2

Extension Go for Visual Studio Code is v0.35.1

Go: Locate Configured Go Tools output:

  1. Checking configured tools....
  2. GOBIN: /Users/me/go/bin
  3. toolsGopath:
  4. gopath: /Users/me/dev/go
  5. GOROOT: /usr/local/go
  6. 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
  7. 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
  8. go: /usr/local/go/bin/go: go version go1.18 darwin/amd64
  9. gotests: /Users/me/go/bin/gotests (version: v1.6.0 built with go: go1.18)
  10. gomodifytags: /Users/me/go/bin/gomodifytags (version: v1.16.0 built with go: go1.18)
  11. impl: /Users/me/go/bin/impl (version: v1.1.0 built with go: go1.18)
  12. goplay: /Users/me/go/bin/goplay (version: v1.0.0 built with go: go1.18)
  13. dlv: /Users/me/go/bin/dlv (version: v1.8.2 built with go: go1.18)
  14. golangci-lint: /Users/me/go/bin/golangci-lint (version: v1.45.2 built with go: go1.18)
  15. gopls: /Users/me/go/bin/gopls (version: v0.9.1 built with go: go1.18)
  16. go env
  17. Workspace Folder (my-project-api-gateway): /Users/me/dev/back/go/my-project/my-project-api-gateway
  18. GO111MODULE=""
  19. GOARCH="amd64"
  20. GOBIN="/Users/me/go/bin"
  21. GOCACHE="/Users/me/Library/Caches/go-build"
  22. GOENV="/Users/me/Library/Application Support/go/env"
  23. GOEXE=""
  24. GOEXPERIMENT=""
  25. GOFLAGS=""
  26. GOHOSTARCH="amd64"
  27. GOHOSTOS="darwin"
  28. GOINSECURE=""
  29. GOMODCACHE="/Users/me/dev/go/pkg/mod"
  30. GONOPROXY="github.com/BigBoulard/*"
  31. GONOSUMDB="github.com/BigBoulard/*"
  32. GOOS="darwin"
  33. GOPATH="/Users/me/dev/go"
  34. GOPRIVATE="github.com/BigBoulard/*"
  35. GOPROXY="https://proxy.golang.org,direct"
  36. GOROOT="/usr/local/go"
  37. GOSUMDB="sum.golang.org"
  38. GOTMPDIR=""
  39. GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
  40. GOVCS=""
  41. GOVERSION="go1.18"
  42. GCCGO="gccgo"
  43. GOAMD64="v1"
  44. AR="ar"
  45. CC="clang"
  46. CXX="clang++"
  47. CGO_ENABLED="1"
  48. GOMOD="/Users/me/dev/back/go/my-project/my-project-api-gateway/go.mod"
  49. GOWORK=""
  50. CGO_CFLAGS="-g -O2"
  51. CGO_CPPFLAGS=""
  52. CGO_CXXFLAGS="-g -O2"
  53. CGO_FFLAGS="-g -O2"
  54. CGO_LDFLAGS="-g -O2"
  55. PKG_CONFIG="pkg-config"
  56. 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 的代码片段如下:

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

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

  1. "configurations": [
  2. {
  3. "name": "My Application",
  4. "type": "go",
  5. "request": "launch",
  6. "mode": "debug",
  7. "envFile": "${workspaceFolder}/.env",
  8. "program": "${workspaceFolder}/src/main.go",
  9. "cwd": "${workspaceFolder}"
  10. }
  11. ]

有用的资源:

<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:

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

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

  1. &quot;configurations&quot;: [
  2. {
  3. &quot;name&quot;: &quot;My Application&quot;,
  4. &quot;type&quot;: &quot;go&quot;,
  5. &quot;request&quot;: &quot;launch&quot;,
  6. &quot;mode&quot;: &quot;debug&quot;,
  7. &quot;envFile&quot;: &quot;${workspaceFolder}/.env&quot;,
  8. &quot;program&quot;: &quot;${workspaceFolder}/src/main.go&quot;,
  9. &quot;cwd&quot;: &quot;${workspaceFolder}&quot;
  10. }
  11. ]

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:

确定