英文:
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-gateway
的launch.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 + "/.env")
if loadErr != nil {
log.Fatalln("can't load env file from current directory: " + curDir)
}
So, I just had to set the current working directory $cwd
by adding "cwd": "${workspaceFolder}"
in my launch.json
file:
"configurations": [
{
"name": "My Application",
"type": "go",
"request": "launch",
"mode": "debug",
"envFile": "${workspaceFolder}/.env",
"program": "${workspaceFolder}/src/main.go",
"cwd": "${workspaceFolder}"
}
]
Useful resources:
<https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables>
<https://github.com/golang/vscode-go/wiki/debugging>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论