0

msbuild outputs compiled artifacts to bin/Release/ and obj/Release when I've specified the output path as bin/. I'm trying to perform this build in CI so I'm using this command `

msbuild.exe /nologo /p:Configuration=Release

`

The .csproj file for this project contains this entry

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
    <OutputPath>bin\</OutputPath>
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
    <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <Optimize>true</Optimize>
    <DebugType>pdbonly</DebugType>
    <PlatformTarget>x64</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
    <Prefer32Bit>true</Prefer32Bit>
  </PropertyGroup>

I manually added the OutputPath.

After looking around SO, I tried adding the AppendTargetFrameworkToOutputPath and AppendRuntimeIdentifierToOutputPath keys but didn't seem to help. What option do I need to stop the "Release" directory from being appended to output path?

  • Is `x64` your default Platform? What happens if you specify `/p:Platform=x64` explicitly? – Joe Sewell Dec 01 '22 at 22:33
  • It should work, but I've always done it via the project's **Property** page inside Visual Studio and could enter values like **U:\Foo\out** so I don't see why yours wouldn't work. Try the editor. –  Dec 01 '22 at 22:35
  • 1
    Try specifying the OutputPath - msbuild mysolution.sln /p:OutputPath=c:\mydir - Previously answered here: https://stackoverflow.com/questions/4965507/msbuild-poutputdir-c-mydir-being-ignored – Anthony G. Dec 02 '22 at 03:14
  • @AnthonyG. Why isn't the OutputPath in the .csproj respected? Also, I don't want to build the entire solution since it contains multiple projects, I just want to build the one project. – user20660706 Dec 02 '22 at 08:52
  • @AnthonyG. I've tried `msbuild.exe /nologo /p:Configuration=Release /p:OutputDir=bin\` to no avail. Also, I need to use a relative path as this is for a CI/CD setup. – user20660706 Dec 02 '22 at 12:08
  • @JoeSewell That's not an option, this needs to build projects that may or may not be x64. – user20660706 Dec 02 '22 at 12:10
  • @user20660706 The reason that Joe is asking if you tested with `/p:Platform=x64` is because the part of the project file that you shared is only used when the value of `Platform` is x64 and the value of `Configuration` is release. – Jonathan Dodds Dec 02 '22 at 16:37

1 Answers1

1

In the project snippet provided, the PropertyGroup is conditional on the Configuration being Release and the Platform being x64. Out of the box, the default value of Platform in a C# project is 'AnyCPU'.

The following command line will set the Configuration and Platform appropriately for the PropertyGroup to be evaluated:

msbuild /nologo /p:Configuration=Release;Platform=x64

If you want to set the OutputPath regardless of the value of Platform but only when Configuration is Release, remove OutputPath from the existing PropertyGroup and create a new PropertyGroup as follows:

  <PropertyGroup Condition="'$(Configuration)' == 'Release'">
    <OutputPath>bin\</OutputPath>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
    <AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
    <AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <Optimize>true</Optimize>
    <DebugType>pdbonly</DebugType>
    <PlatformTarget>x64</PlatformTarget>
    <ErrorReport>prompt</ErrorReport>
    <CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
    <Prefer32Bit>true</Prefer32Bit>
  </PropertyGroup>

To always change the OutputPath, regardless of Configuration and Platform, remove the Condition:

  <PropertyGroup>
    <OutputPath>bin\</OutputPath>
  </PropertyGroup>
Jonathan Dodds
  • 2,654
  • 1
  • 10
  • 14
  • It should also be noted, that order is important. If the "unconditional" property group is followed by a conditional one (that matches the current settings) and that redefines the OutputPath to something else, that value will be taken. IOW, if you use the unconditional approach, make sure you don't set OutputPath again anywhere later. – Christian.K Dec 05 '22 at 05:37
  • I ended up doing something somewhat like this. I changed a PropertyGroup which contained conditionals that set the Platform to 'AnyCPU' if it was empty. I changed that to set it to 'x64' if empty. – user20660706 Dec 05 '22 at 15:37