Search code examples
c#.netmsbuildvisual-studio-2017postsharp

msbuild uses the wrong assembly name


I've had trouble building some of the C# projects in a solution I've never had problems with. Building fails with the error that a metadata file is missing. The error description and the msbuild log output at diagnostic level shows some surprising developments. One of the projects I have this problem with is called WpfControlLibrary. It references the following projects in the solution:

Helpers
HAX.Kioszk.Common
HAX.Kioszk.Communication.ImportedServiceContracts
HAX.Kioszk.Data

The solution folder is "D:\Entegro\TFS\Volánbusz TVM\Bugfixes 2018-06".

The build failure error is the following:

Error CS0006 Metadata file 'D:\Entegro\TFS\Volánbusz TVM\Bugfixes 2018-06\HAX.Kioszk.Data\bin\Debug\WpfControlLibrary.dll' could not be found (in project WpfControlLibrary; file CSC)

So when I build the WpfControlLibrary project, CSC is looking for a dll that is named after the project in the bin\debug folder of one of the projects the WpfControlLibrary project is referencing.

The build log shows that this is the case with all four project references above. The build is looking for a dll called WpfControlLibrary.dll in their bin\debug folder after building those projects seemingly correctly (the projects are built with the correctly named dll and pdb files). The build error probably only contains one missing dll because it fails at the first error.

However, if I build the whole solution, and I watch the bin\debug folder of the Helpers project, I see that the project is built correctly, and then later during the build, the Helpers.dll file disappears, and there appears a file named WpfControlLibrary.dll (and pdb). The build fails in the end because it does not find Helpers.dll.

The projects the WpfControlLibrary project references have no reference to WpfControlLibrary.

The above result is produced by VS Enterprise 2017 version 15.8.2 with msbuild version 15.8.168.64424. I've recently updated VS, and returned to this solution after working on others for a while, so I can't tell if the update broke it.

I tried building the problematic projects on the same computer using VS Community 2017 version 15.2 with msbuild version 15.1.1012.6693, which builds them fine.

The project also builds fine on other computers with various other versions of VS and msbuild.

I don't know much about msbuild but I compared the Enterprise and the Community build logs, and it seems that the dll names turn sour in the former after the following lines:

Task Parameter:
1>      Properties=
1>          Configuration=Debug
1>          Platform=AnyCPU (TaskId:12)
1>  Global Properties: (TaskId:12)
1>    Configuration=Debug (TaskId:12)
1>    Platform=AnyCPU (TaskId:12)
1>  Removing Properties for project "..\Helpers\Helpers.csproj": (TaskId:12)
1>    TargetFramework (TaskId:12)
1>  Removing Properties for project "..\HAX.Kioszk.Common\HAX.Kioszk.Common.csproj": (TaskId:12)
1>    TargetFramework (TaskId:12)
1>  Removing Properties for project "..\HAX.Kioszk.Communication.ImportedServiceContracts\HAX.Kioszk.Communication.ImportedServiceContracts.csproj": (TaskId:12)
1>    TargetFramework (TaskId:12)
1>  Removing Properties for project "..\HAX.Kioszk.Data\HAX.Kioszk.Data.csproj": (TaskId:12)
1>    TargetFramework (TaskId:12)

These are missing in the Community build log but then there may be lots of other differences.

I'm not sure it is VS' fault. It may be that a global msbuild file the build process takes build targets and whatnot from went corrupt. I tried repairing VS but it made no difference.

I am linking the build log below.

I have a hunch that I'll be able to resolve this only by removing and reinstalling, or repairing the installed .NET Framework versions. Can I do that simply from Control Panel / Programs and Features / <.NET Framework version> / Repair in the context menu?

Download the zipped diagnostic-level build log at https://drive.google.com/file/d/1NqLrzkmQhYSoYQxoja76NQp484gWNTHL/view?usp=sharing

UPDATE

The build log references the Microsoft.Common.CurrentVersion.targets file for each project. For instance:

1>Target "GetTargetPathWithTargetPlatformMoniker: (TargetId:19)" in file "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets" from project "D:\Entegro\TFS\Volánbusz TVM\Bugfixes 2018-06\Helpers\Helpers.csproj" (target "GetTargetPath" depends on it):
1>Added Item(s): 
1>    TargetPathWithTargetPlatformMoniker=
1>        D:\Entegro\TFS\Volánbusz TVM\Bugfixes 2018-06\Helpers\bin\Debug\Helpers.dll
1>                CopyUpToDateMarker=D:\Entegro\TFS\Volánbusz TVM\Bugfixes 2018-06\Helpers\obj\Debug\Helpers.csproj.CopyComplete
1>                TargetFrameworkIdentifier=.NETFramework
1>                TargetFrameworkVersion=4.0
1>                TargetPlatformIdentifier=Windows
1>                TargetPlatformMoniker=Windows,Version=7.0
1>Done building target "GetTargetPathWithTargetPlatformMoniker" in project "Helpers.csproj".: (TargetId:19)

If I replace that file with the file that the VS Community build log references, and build the project, it still gives the same error. However, if I clean the project, and then build, it builds it without errors. For VS and msbuild versions, see above. I link the two files below. The file name shows which version they are.

Enterprise: https://drive.google.com/open?id=1MPnAVQxMtjTcuy39pzmLJolIroBaDgMt

Community: https://drive.google.com/open?id=19Jn8UkRaJ5oAFXreXI4IpRKmGrnjtGQ8

I'll try to compile a small solution with projects that shows this behavior.

UPDATE 2

I've created a solution with only the WpfControlLibrary project and its dependencies. You can download it at the following link:

use the updated link under UPDATE 4 below

I simply copied the project folders into a new folder, and added them to the new solution. I had to doctor the csproj files a little because the following section in each of them resulted in the standard .NET 4 project references (System., etc.) showing up in Solution Explorer as not found:

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
  </Target>

My solution was to comment out this part, which resulted in the projects opening correctly. I also removed and re-added the referenced Nuget packages, just in case.

After restoring the NuGet packages, building the solution results in the following error:

Metadata file 'D:\Entegro\VB TVM\Playground\2018-09-04 VS Build Error\BuildFailure\HAX.Kioszk.Data\bin\Debug\WpfControlLibrary.dll could not be found (Project: WpfControlLibrary; File: CSC)

So the error described above is now reproduced.

I managed to get rid of the build error by excluding all xaml files from the WpfControlLibrary project. I built the solution after re-adding them one by one but could not reach a conclusion as to which caused the error because it varied. Once, the build error went away by removing the reference to the HAX.Kioszk.Communication.ImportedServiceContracts project.

I don't think replacing the Microsoft.Common.CurrentVersion.targets file is a decent solution, so I hope someone can come up with a better one. Thanks in advance.

UPDATE 3

VS 15.8.3 just came out. After updating to it, the process of building the sample solution changes a little. The build error is the following:

The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://www.postsharp.net/links/nuget-restore.  WpfControlLibrary   D:\Entegro\VB TVM\Playground\2018-09-04 VS Build Error\BuildFailure\WpfControlLibrary\WpfControlLibrary.csproj

The error remains in subsequent builds and never goes away, except if I build only the WpfControlLibrary project. Then we're back to the original build error.

Updating to VS 15.8.4 did not fix it, either.

Because these projects have seen many versions of VS come and go since VS 2010, I have a hunch the csproj files may be in an inconsistent state. I'll try creating new projects and add the code files to them.

UPDATE 4

I just created a new solution with new projects, and added the code files to them. Unluckily, the build finishes with the same error. You can download the solution here:

https://drive.google.com/open?id=1oMtwc8aD0kxE6jmtoF05Z5ROZ7diQzUQ

UPDATE 5

This issue probably has nothing to do with the Microsoft.Common.CurrentVersion.targets file because I compared it with another VS Enterprise installation of the same version, and they match.

I'll try uninstalling and reinstalling VS.

UPDATE 6

Some of the projects in the solution used the PostSharp NuGet package version 4.1.23. Updating them to 6.0.27, the most recent version, solved the issue. The solution builds fine again.


Solution

  • Update - it turns out that there are breaking changes in the Visual Studio update, approx 15.8.3, which caused issues using early versions of PostSharp - I think Drew and myself were on 4.x. Updating PostSharp to the latest version, 6.0.27, solved the problem, after putting in the effort to deal with a few breaking changes that introduced in my aspects.

    Original post below.

    Same problem this week with a project I haven't touched for months. I cloned the repo on Tuesday and NCrunch builds it fine. Yesterday, I updated VS2017 to 15.8.4 and NCrunch still builds it. But... now 4 projects out of the 32 give me a CS0006 when I hit F5. Looking at the verbose build log I see this:

    1>Target "ResolveAssemblyReferences" in file "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets" from project "D:\Repos\g\Game.Client.Wpf\Game.Client.Wpf_ftfxk2qp_wpftmp.csproj" (target "PostSharp30InspectReferences" depends on it):  
    1>Using "ResolveAssemblyReference" task from assembly "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".  
    1>Task "ResolveAssemblyReference"  
    1>  TargetFrameworkMoniker:  
    1>      .NETFramework,Version=v4.7.2  
    1>  TargetFrameworkMonikerDisplayName:  
    1>      .NET Framework 4.7.2  
    1>  TargetedRuntimeVersion:  
    1>      v4.0.30319  
    1>  Assemblies:  
    1>      System.Core  
    1>  AssemblyFiles:  
    1>      D:\Repos\g\EntityComponentSystem\bin\Debug\Lafs2.dll  
    1>      D:\Repos\g\Framework.Unity\bin\Debug\Lafs2.dll  
    1>      D:\Repos\g\Framework\bin\Debug\Lafs2.dll  
    1>      D:\Repos\g\Game.Controls.Wpf\bin\Debug\Lafs2.dll  
    1>      D:\Repos\g\Game.Messages\bin\Debug\Lafs2.dll  
    1>      D:\Repos\g\Logging.Metrics\bin\Debug\Lafs2.dll  
    1>      D:\Repos\g\Logging\bin\Debug\Lafs2.dll  
    1>      D:\Repos\g\NetCode\bin\Debug\Lafs2.dll  
    1>      D:\Repos\g\Universe.Client\bin\Debug\Lafs2.dll  
    1>      D:\Repos\g\Universe.Physics\bin\Debug\Lafs2.dll  
    1>      D:\Repos\g\Universe\bin\Debug\Lafs2.dll  
    1>      C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7.2\mscorlib.dll  
    

    The assembly name of the currently building project has replaced the filename of ALL of the items in AssemblyFiles. The CS0006 says it cannot find D:\Repos\g\Universe\bin\Debug\Lafs2.dll when it should be looking for D:\Repos\g\Universe\bin\Debug\Universe.dll.