【VSCode】拡張機能開発 デバッグでブレークポイントで止まらない
更新日:2025/04/10
VSCodeの拡張機能を作成しようと思い、環境構築をしました。
とても簡単に構築できたんだけど、ブレークポイントで停止しません。
こままではデバッグできないのでどうしよう、というお話です。
拡張機能の開発環境を構築
まずは、次のVSCode拡張機能を解説したページに従って拡張機能の開発環境を構築しました。
そして、次のコマンドを実行。
npm install -g yo generator-code yo code
質問に答えていくと、拡張機能のひな型を格納したフォルダ(ディレクトリ)が作成されるので、そのフォルダをVSCodeで開きます。
そしてアクティビティーバーの実行とデバッグで『Run Extension』を実行。
『タスク 'npm: watch' は終了せず、'problemMatcher'が定義されていません。ウォッチタスクの問題マッチャ―を定義してください』と表示されたけど、気にせずに続行。(推奨拡張機能のamodio.tsl-problem-matcherをインストールすればOK)
すると、別画面でVSCodeが起動します。
Ctrl + Shift + P でコマンドパレットを開いてHello World を実行すると、『Hello World from HelloWorld!』と通知されます。
ブレークポイントで停止しない
次にブレークポイントを設置して、『Run Extension』します。
今回はTypescriptでひな型を作ったので、『src/extension.ts』にブレークポイントを設置します。
JavaScriptならフォルダー直下の『extension.js』です。
コードを開くと次のようになっているので、activate()の最初の行にブレークポイントを設置しました。
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed
export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, your extension "test" is now active!');
// The command has been defined in the package.json file
// Now provide the implementation of the command with registerCommand
// The commandId parameter must match the command field in package.json
const disposable = vscode.commands.registerCommand('test.helloWorld', () => {
// The code you place here will be executed every time your command is executed
// Display a message box to the user
vscode.window.showInformationMessage('Hello World from test!');
});
context.subscriptions.push(disposable);
}
// This method is called when your extension is deactivated
export function deactivate() {}
『Run Extension』すると、起動したVSCode側で次の通知が発生。
『拡張ホストが10秒以内に開始されませんでした。先頭行で停止している可能性があり、続行するにはデバッガ―が必要です』
ブレークポイントで停止したのかな?と思って、ソースコードを見ても停止していない。
パスマッピングが合ってないか?とか、いろいろ確認したけれど原因不明でした。
このままでは、デバッグできません。
解決策
ブレークポイントを設置しないで『Run Extension』して、後からブレークポイントを設置すれば動作します。
ただし、その時点ではactivate()実行後のため、activate()はデバッグできません。
これ以上悩んでも時間の無駄なので、activate()内はconsole.log()でごまかしました。
更新日:2025/04/10
関連記事
スポンサーリンク
記事の内容について

こんにちはけーちゃんです。
説明するのって難しいですね。
「なんか言ってることおかしくない?」
たぶん、こんなご意見あると思います。
裏付けを取りながら記事を作成していますが、僕の勘違いだったり、そもそも情報源の内容が間違えていたりで、正確でないことが多いと思います。
そんなときは、ご意見もらえたら嬉しいです。
掲載コードについては事前に動作確認をしていますが、貼り付け後に体裁を整えるなどをした結果動作しないものになっていることがあります。
生暖かい視線でスルーするか、ご指摘ください。
ご意見、ご指摘はこちら。
https://note.affi-sapo-sv.com/info.php
このサイトは、リンクフリーです。大歓迎です。