Search code examples
c#.netvisual-studiodotnet-build

Visual Studio build fails but CLI dotnet build succeeds


I have a problem where my solution contains a node_modules folder with some libraries with long paths.

This files are not needed in output directory, becuase they are only needed in another build step.

When I run build command from Visual Studio, the build fails with errors MSB3027 nad MSB3021 both saying that files could not be copied to output directory. Error source file Microsoft.Common.CurrentVersion.targets.

I excluded the problematic folder from solution in a different ways:

<None Remove="App_Plugins\**\node_modules\**" />
<ItemGroup>
  <Content Remove="App_Plugins\**\node_modules\**" />
  <Compile Remove="App_Plugins\**\node_modules\**" />
  <EmbeddedResource Remove="App_Plugins\**\node_modules\**" />
  <None Remove="App_Plugins\**\node_modules\**" />
</ItemGroup>

but the Visual Studio tries to copy them anyway.

Error:

C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\Microsoft.Common.CurrentVersion.targets(5270,5): error MSB3027: Could not copy "C:\src\ORB\MyComp\src\Cms\Company.Cms\App_Plugins\welcome-dashboard\node_modules\@umbraco-cms\backoffice\dist-cms\packages\core\workspace\components\workspace-breadcrumb\workspace-variant-menu-breadcrumb\workspace-variant-menu-breadcrumb.kind.d.ts" to "bin\Debug\net8.0\App_Plugins\welcome-dashboard\node_modules\@umbraco-cms\backoffice\dist-cms\packages\core\workspace\components\workspace-breadcrumb\workspace-variant-menu-breadcrumb\workspace-variant-menu-breadcrumb.kind.d.ts". Exceeded retry count of 10. Failed.

Why does the dotnet build command work? Isn't MSBuild used in the end in both cases?

How to convince Visual Studio to leave those files?


Solution

  • It turns out that Visual Studio took another MSBuild binary as dotnet build CLI command.

    PS C:\src> msbuild -version
    Microsoft (R) Build Engine version 16.11.2+f32259642 for .NET Framework
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    PS C:\src> dotnet msbuild -version
    MSBuild version 17.10.4+10fbfbf2e for .NET
    17.10.4.21802
    

    After changing the PATH variable to the newer MSBuild binary (C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64\) the build started to work normally.

    Also the MSBuil version output is now different, but not the same as in CLI command - version number is the same though.

    PS C:\src> msbuild -version
    MSBuild version 17.10.4+10fbfbf2e for .NET Framework
    17.10.4.21802
    
    PS C:\src> dotnet msbuild -version
    MSBuild version 17.10.4+10fbfbf2e for .NET
    17.10.4.21802
    

    EDIT: The reason why the <ItemGroup> exclusion did not work is, because in one of the referenced packages there is a .targets file that copies that exact folder.