📝

Visual Studio Code で Azure Functions のデバッグ終了後に再デバッグ出来ない問題

に公開

以下の issue がクローズしていたら、この方法はとらなくて大丈夫になってると思います。

https://github.com/microsoft/vscode-azurefunctions/issues/4416

Visual Studio Code で Azure Functions のデバッグ実行を行い、デバッグ終了後に再度デバッグをしようとすると以下のようなエラーが出て再デバッグが出来ないことがあります。

Executing task: C:\Program Files\dotnet\dotnet.EXE build /property:GenerateFullPaths=true /consoleloggerparameters:NoSummary 

復元が完了しました (0.5 秒)
プレビュー版の .NET を使用しています。https://aka.ms/dotnet-support-policy をご覧ください
  WorkerExtensions 成功しました (1.4 秒) → obj\Debug\net8.0\WorkerExtensions\buildout\Microsoft.Azure.Functions.Worker.Extensions.dll
C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 1 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 2 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 3 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 4 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 5 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 6 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 7 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 8 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 9 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 10 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
  AzFuncVsCode 2 件のエラーと 10 件の警告で失敗しました (14.8 秒)
    C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 1 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
    C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 2 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
    C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 3 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
    C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 4 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
    C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 5 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
    C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 6 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
    C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 7 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
    C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 8 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
    C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 9 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
    C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): warning MSB3026: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。1000 ミリ秒以内に 10 回目の再試行を開始します。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process. このファイルは ".NET Host (36812)" によってロックされています。
    C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): error MSB3027: "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできませんでした。10 回の再試行回数を超えたため、失敗しました。このファイルは ".NET Host (36812)" によってロックされています。
    C:\Program Files\dotnet\sdk\10.0.100-preview.5.25277.114\Microsoft.Common.CurrentVersion.targets(4904,5): error MSB3021: ファイル "obj\Debug\net8.0\AzFuncVsCode.dll" を "bin\Debug\net8.0\AzFuncVsCode.dll" にコピーできません。The process cannot access the file 'D:\source\AzFuncVsCode\bin\Debug\net8.0\AzFuncVsCode.dll' because it is being used by another process.

15.8 秒後に 2 件のエラーと 10 件の警告で失敗しました をビルド

エラーメッセージの中にも出ていますがプロセスによりファイルがブロックされているのでクリーンに失敗しているためおきています。
ここに出ているプロセスを終了させても今度はポート番号が使われているというエラーで起動が出来ません。

問題の原因は func.exe が終了していないことで起きているのでタスクマネージャーから func という名前のプロセスを探して終了させることで再度デバッグ実行が出来るようになります。この作業を自動化する方法が該当の issue で EvilConsultant さんから提案されています。手順としては .vscode/tasks.json に以下のようなプロセスを終了させるタスクを追加します。

{
	"version": "2.0.0",
	"tasks": [
		{
			"label": "Release Blocked Port",
			"type": "shell",
			"command": "powershell.exe -ExecutionPolicy Bypass -Command \"Stop-Process -Id (Get-NetTCPConnection -LocalPort 7071).OwningProcess -Force\"",
			"problemMatcher": []
		},
        // 以下略

そして、このタスクをデバッグ実行の終了後に動かすように .vscode/launch.jsonpostDebugTask に指定します。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Attach to .NET Functions",
            "type": "coreclr",
            "request": "attach",
            "processId": "${command:azureFunctions.pickProcess}",
            "postDebugTask": "Release Blocked Port"
        }
    ]
}

これで強引ですがデバッグ終了後にプロセスが終了して次のデバッグ実行もできるようになります。

ということで自分が後で検索する用のメモでした。

Microsoft (有志)

Discussion