Search code examples
macosbuildvisual-studio-mac

Visual Studio Mac: Output obj / bin folder elsewhere not supported?


I have a multiple-project solution and wish to put all the generated in/obj artifacts in a another folder.

I try to set:

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

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugType>portable</DebugType>
    <OutputPath>..\Bin\Core\Debug\</OutputPath>
    <IntermediateOutputPath>..\Bin\Core\obj\</IntermediateOutputPath>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <OutputPath>..\Bin\Core\Release\</OutputPath>
    <IntermediateOutputPath>..\Bin\Core\obj\</IntermediateOutputPath>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="Library.fs" />
  </ItemGroup>

</Project>

However:

  1. The obj folder is still re-created in the original location. Is also on the desired location but EMPTY.
  2. The "restore packages" trigger "1" and not put them on the desired location
  3. When the project is build, the obj folder is populated.

This is the versions:

Visual Studio Community 2017 for Mac Version 7.3.2 (build 12) Runtime: Mono 5.4.1.7 (2017-06/e66d9abbb27) (64-bit)

NuGet Versión: 4.3.1.4445

.NET Core Runtime: /usr/local/share/dotnet/dotnet Versión de tiempo de ejecución: 2.0.0 SDK: /usr/local/share/dotnet/sdk/2.0.0/Sdks Versión del SDK: 2.0.0 SDK de MSBuild: /Library/Frameworks/Mono.framework/Versions/5.4.1/lib/mono/msbuild/15.0/bin/Sdks


Solution

  • This problem is not specific to Visual Studio for Mac. The same behaviour occurs if you use the dotnet from the command line.

    With Visual Studio for Mac, and the dotnet CLI, the obj directory gets created in the project directory on restoring the NuGet packages and contains just the files generated by the restore:

    • MyProject.csproj.nuget.cache
    • MyProject.csproj.nuget.g.targets
    • MyProject.csproj.nuget.g.props
    • project.assets.json

    On building files are created in the ../Bin/Core/Debug and the ../Bin/Core/obj directories.

    You can get the behaviour you want by setting the BaseIntermediateOutputPath. However setting it in the project file itself can cause problems.

    The simplest solution is to create a Directory.Build.props file in the solution directory and set the BaseIntermediateOutputPath property there.

    <Project>
      <PropertyGroup>
        <BaseIntermediateOutputPath>..\Bin\Core\obj\</BaseIntermediateOutputPath>
      </PropertyGroup>
    </Project>
    

    Then the generated NuGet restore files will be created in the directory you want.

    Visual Studio for Mac seems to still want to create an empty obj directory in the project when the project is opened but this has no files after the restore or build.