ツナ缶雑記

ぐうたらSEのブログです。主にマイクロソフト系技術を中心に扱います。

Azure Pipelines の YAML テンプレートにある buildPlatform の設定に注意しろ

f:id:masatsuna:20200415160341p:plain

地味にはまったので供養のためメモを残しておきます。

本稿は 2020/4/15 現在の情報を示すものであり、 Azure Pipelines および Visual Studio の更新によって、将来当てはまらなくなる可能性を含んでいることに注意してください。

結論から言うと。。。

Azure Pipelines の ASP.NET や デスクトップ .NET アプリケーションの YAML テンプレートを使うとき、テンプレートとして出力される YAML を必ず見直しましょう。 具体的には以下の 3 つのテンプレートのことを言っています。

Azure DevOps での操作時、以下の画面で選択している方が多いのではないかと思います。

f:id:masatsuna:20200415152445p:plain
対象となるテンプレート

これらのテンプレートを使うと、 YAML に記載されている buildPlatform の変数値が

Any CPU

に設定されていると思います。 しかし、正しくは

AnyCPU

です。 Any と CPU の間に半角のスペースは不要です。

f:id:masatsuna:20200415152917p:plain
この部分が間違っている

この設定がリンクする先

この設定値は、プロジェクトファイル内に記載されている値と合わせなければなりません。 例えば Visual Studio 2019 でコンソールアプリケーションのプロジェクトを作成すると、以下のようなプロジェクトファイルが出力されます。

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{8686833F-8175-4D1C-B8E4-6DD111FC417A}</ProjectGuid>
    <OutputType>Exe</OutputType>
    <RootNamespace>ConsoleApp1</RootNamespace>
    <AssemblyName>ConsoleApp1</AssemblyName>
    <TargetFrameworkVersion>v4.8</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

良く設定を見てほしいのですが、ビルドプラットフォームの設定値は、どこも AnyCPU となっています。 Any と CPU の間に半角スペースが入っていません。 この設定が間違っていると、せっかくプロジェクトファイルに設定している上記の設定が生きなくなってしまいます。

なんでこんなのに気付いたのか

私がこの問題に気づいたのは ASP.NET の Web アプリケーションをプロジェクト指定でビルドしようとしたときに遭遇したエラーメッセージからでした。 ASP.NETYAML テンプレートを編集して、プロジェクトファイルを指定してビルドするように変更して実行しました。

- task: VSBuild@1
  inputs:
    solution: '**/WebApp.csproj'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:DesktopBuildPackageLocation="$(Build.Artifactstagingdirectory)\WebApp.zip"'
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

YAML 上変更しているのは solution の値だけで、他のはテンプレート通りです。 これを実行すると、以下のような警告メッセージが出てきます。

Warning : The OutputPath property is not set for project 'WebApp.csproj'. Please check to make sure that you have specified a valid combination of Configuration and Platform for this project. Configuration='Release' Platform='Any CPU'. You may be seeing this message because you are trying to build a project without a solution file, and have specified a non-default Configuration or Platform that doesn't exist for this project.

しかし、 ASP.NET のプロジェクトファイルには、 OutPutPath の設定が入っているんですよね。

  <!-- 前略 -->
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <!-- 後略 -->

それなのになぜこんな警告がでるんだ?と30分ほど時間を無駄にしました。。。 そりゃテンプレートだって間違うことはありますよね。。。 妄信してしまった私が悪い。

YAML 側を AnyCPU と 半角スペースなしにして実行すると、ちゃんと動くようになります。