VSCodeでFirebase Admin SDKを用いたGoのDebugをしようとしてちょっとハマりました。
SDK初期化でいきなりfirebase-adminsdk.jsonを読み込めなくて怒られる。。
// SDK初期化
app, err := firebase.NewApp(ctx, nil, opt)
// err: "cannot read credentials file: open firebase-adminsdk.json: no such file or directory"
普通にgo runで起動した時はエラー出ないので、launch.json
の設定で現在のディレクトリを明示する必要があるのではないかとアタリをつけてみる。
自分のプロジェクトの場合、{root}/cmd/api
の下にmain.go、プロジェクト直下にfirebase-adminsdk.jsonを置いています。
├── cmd
│ └── api
│ └── main.go
├── ***
└── firebase-adminsdk.json
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/api",
"env": {},
"args": []
}
]
}
公式ドキュメントで設定の詳細を見たら、明らかにそれっぽいのがあった。
というわけでlaunch.jsonにcwd
を追加します。
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/api",
"cwd": "${workspaceFolder}",
"env": {},
"args": []
}
]
}
デバッグ再起動したら無事成功した!
そもそもGoでFirebase Admin SDK使ってる人どれぐらいいるんだろ・・
仲間が増えると良いなー