Search code examples
docker-composedotnet-watch

How to tell dotnet watch to run a different build configuration?


Today I have a container running dotnet watch -v --project ".\\MyProject" run --no-launch-profile and the "MyProject" folder is mapped transparently to a .NET Core project of my solution. So whenever I alter a file in my Visual Studio it's picked up immediately inside the container and recompiled to allow quick alteration and testing. However I've noticed that this causes conflict issues when I build on my host machine causing a conflict over the bin\Debug\netcoreapp3.1\MyProject.exe

To solve this situation I wanted to see if I can pass an argument/parameter/configuration/... anything to dotnet watch that would tell it to use a different build configuration (copy of Debug really but called Container so that it'd create a separate folder and not conflict with the host Visual Studio)

Sadly I have not been able to locate an example or documentation on how to do this with dotnet watch. Is this possible? Is there a better solution?


Solution

  • Rather than target a different build configuration with dotnet watch, you can solve this by changing the output directory based on a built-in dotnet environment variable, DOTNET_RUNNING_IN_CONTAINER.

    <Project>
      <PropertyGroup Condition="'$(UsingMicrosoftNETSdk)' == 'true'">
        <DefaultItemExcludes>$(DefaultItemExcludes);$(MSBuildProjectDirectory)/obj/**/*</DefaultItemExcludes>
        <DefaultItemExcludes>$(DefaultItemExcludes);$(MSBuildProjectDirectory)/bin/**/*</DefaultItemExcludes>
      </PropertyGroup>
      <PropertyGroup Condition="'$(UsingMicrosoftNETSdk)' == 'true' AND '$(DOTNET_RUNNING_IN_CONTAINER)' == 'true'">
        <BaseIntermediateOutputPath>$(MSBuildProjectDirectory)/obj/container/</BaseIntermediateOutputPath>
        <BaseOutputPath>$(MSBuildProjectDirectory)/bin/container/</BaseOutputPath>
      </PropertyGroup>
      <PropertyGroup Condition="'$(UsingMicrosoftNETSdk)' == 'true' AND '$(DOTNET_RUNNING_IN_CONTAINER)' != 'true'">
        <BaseIntermediateOutputPath>$(MSBuildProjectDirectory)/obj/local/</BaseIntermediateOutputPath>
        <BaseOutputPath>$(MSBuildProjectDirectory)/bin/local/</BaseOutputPath>
      </PropertyGroup>
    </Project>