caxapexac
caxapexac

Reputation: 914

Completely change obj folder location in c# project

MSBuild is strange

I already tried this and another answer and I also tried this one

After that, I changed <IntermediateOutputPath> and <BaseIntermediateOutputPath> and <OutputPath> in the .csproj file but...

It keeps creating this piece of strange stuff in the old obj folder (I don't use nuget)

project.assets.json
project.nuget.cache
project.packagespec.json
...

I have already read about Visual Studio legacy workflow causes this behaviour but do any workarounds exist?

My current .csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <StartupObject>Program</StartupObject>
    <IntermediateOutputPath>..\..\obj\</IntermediateOutputPath>
    <BaseIntermediateOutputPath>..\..\obj\</BaseIntermediateOutputPath>
    <OutputPath>..\..\bin\Build\</OutputPath>
  </PropertyGroup>
  
  <ItemGroup>
    <ProjectReference Include="foo\dependency.csproj" />
  </ItemGroup>

</Project>

Upvotes: 8

Views: 7058

Answers (2)

J. Curl
J. Curl

Reputation: 31

I needed something similar. Having two .csproj files in the same directory, with different target frameworks would otherwise fail without this change I made in one of the .csproj files.

The project can have the MSBuildProjectExtensionsPath, but it must be defined before importing the SDK.

<?xml version="1.0" encoding="utf-8"?>

<Project>
  <!-- Implicit top import required explicitly to change build output path -->
  <PropertyGroup>
    <MSBuildProjectExtensionsPath>legacy</MSBuildProjectExtensionsPath>
    <BaseIntermediateOutputPath>$(MSBuildProjectExtensionsPath)\obj</BaseIntermediateOutputPath>
    <OutputPath Condition="'$(Configuration)' != ''">$(MSBuildProjectExtensionsPath)\bin\$(Configuration)</OutputPath>
  </PropertyGroup>
  <Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />

  ...

  <!-- Implicit top import required explicitly to change build output path -->
  <Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>

All changes were needed (not just the property MSBuildProjectExtensionsPath) for dotnet SDK builds, else cross pollination between projects of the project.assets.json file occurs resulting in build failures.

I could not use the Directory.Build.props file, as this would affect both my project files (and they needed to be kept separate).

Please note, this likely does not satisfy the use case if someone does try to override these variables in their own Directory.Build.props, so you'd have to extend this solution for that additional use case and still test.

Upvotes: 2

caxapexac
caxapexac

Reputation: 914

Solved by creating Directory.Build.props file in the root of project with:

<Project>
    <PropertyGroup>
        <MSBUildProjectExtensionsPath>..\..\obj\</MSBUildProjectExtensionsPath>
    </PropertyGroup>
</Project>

Very dirty and non-obvious microsoft-style hack

Found here

Is there any good .NET compiler for windows without penetrating youself?

Upvotes: 8

Related Questions