📌

C# の Azure Functions をデプロイしようとすると local.settings.json が無いというエラーになる

2023/03/20に公開

C# の Azure Functions をデプロイしようとすると以下のようなエラーがでることがあります。

/opt/dotnet/6.0.407/sdk/6.0.407/Microsoft.Common.CurrentVersion.targets(5097,5): error MSB3030: Could not copy the file "/path/GraphACSFunctions/local.settings.json" because it was not found. [/path/GraphACSFunctions.csproj] | 1 | Please build your app locally before publishing. | https://docs.microsoft.com/en-us/azure/app-service/configure-language-dotnetcore?pivots=platform-linux

CI/CD のパイプラインとかみたいにリポジトリから clone してきてビルドをすると local.settings.json がないのでビルドエラーになってしまいます。local.settings.json は通常はローカルでの開発用の設定ファイルなので、リポジトリには含めないのが普通です。でも無いとビルドエラーになってしまいます…。

以下のようなバグ報告も行われていますが、イマイチ反応が悪い感じです。

https://developercommunity.visualstudio.com/t/bug-with-copytopublishdirectory-and-localsettingsj/1580141

回避策

というわけで回避策です。といっても大した事はしてなくてリリースビルド時には local.settings.json を無視するように設定を追加するだけです。プロジェクトファイルにある以下のタグを少し変更します。

<ItemGroup>
  <Content Include="local.settings.json">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    <CopyToPublishDirectory>Never</CopyToPublishDirectory>
  </Content>
</ItemGroup>

ItemGroup タグの属性に Condition="'$(Configuration)' == 'Debug'" を追加することでリリースビルド時には local.settings.json を無視するようになります。

<ItemGroup Condition="'$(Configuration)' == 'Debug'">
  <Content Include="local.settings.json">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    <CopyToPublishDirectory>Never</CopyToPublishDirectory>
  </Content>
</ItemGroup>

これでローカル開発では local.settings.json を使いつつリリースビルド時は無視するようになりました。欠点は、ローカル開発中にリリースビルドをして実行するとちゃんと動かないところでしょうか…。
でもまぁ、レアケースだと思うのでこれでお茶を濁しておこうと思います。

Microsoft (有志)

Discussion