3
3

More than 3 years have passed since last update.

VSCodeでGoのDebugにちょっとハマった話 ~firebase絡み~

Last updated at Posted at 2019-12-14

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": []
    }
  ]
}

公式ドキュメントで設定の詳細を見たら、明らかにそれっぽいのがあった。
image.png

というわけで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使ってる人どれぐらいいるんだろ・・
仲間が増えると良いなー

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3