Skip to content

In some cases dotnet build fails when there is a direct DLL reference while building via VS is okay  #6730

@aalonzov

Description

@aalonzov

We are trying to convert an old project to .NET standard. First step was to convert .csproj file to an SDK style. There is a directly referenced legacy DLL with a target runtime v2.0.50727. Project itself has net40 target framework.
When I try to compile a project after conversion with a Visual Studio everything goes well. But when I use dotnet build command it fails with the following error:

C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018: The "ResolveAssemblyReference" task failed unexpectedly. [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018: System.InvalidCastException: Specified cast is not valid. [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at System.Reflection.Throw.InvalidCast() [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at System.Reflection.Metadata.TypeReferenceHandle.op_Explicit(EntityHandle handle) [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.Tasks.AssemblyInformation.CorePopulateMetadata() [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.Tasks.AssemblyInformation.get_Dependencies() [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.Tasks.AssemblyDependency.AssemblyMetadata..ctor(String path) [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.Tasks.AssemblyInformation.<>c.<GetAssemblyMetadata>b__12_0(String p) [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.Tasks.AssemblyInformation.GetAssemblyMetadata(String path, ConcurrentDictionary`2 assemblyMetadataCache, AssemblyNameExtension[]& dependencies, String[]& scatterFiles, FrameworkName& frameworkName) [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.Tasks.SystemState.GetAssemblyMetadata(String path, ConcurrentDictionary`2 assemblyMetadataCache, AssemblyNameExtension[]& dependencies, String[]& scatterFiles, FrameworkName& frameworkName) [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.Tasks.ReferenceTable.GetUnifiedAssemblyMetadata(Reference reference, IEnumerable`1& unifiedDependencies, String[]& scatterFiles) [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.Tasks.ReferenceTable.FindDependenciesAndScatterFiles(Reference reference, List`1 newEntries) [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.Tasks.ReferenceTable.FindAssociatedFiles() [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.Tasks.ReferenceTable.ComputeClosure() [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.Tasks.ReferenceTable.ComputeClosure(IEnumerable`1 remappedAssembliesValue, ITaskItem[] referenceAssemblyFiles, ITaskItem[] referenceAssemblyNames, List`1 exceptions) [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.Tasks.ResolveAssemblyReference.Execute(FileExists fileExists, DirectoryExists directoryExists, GetDirectories getDirectories, GetAssemblyName getAssemblyName, GetAssemblyMetadata getAssemblyMetadata, GetLastWriteTime getLastWriteTime, GetAssemblyRuntimeVersion getRuntimeVersion, GetAssemblyPathInGac getAssemblyPathInGac, IsWinMDFile isWinMDFile, ReadMachineTypeFromPEHeader readMachineTypeFromPEHeader) [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.Tasks.ResolveAssemblyReference.Execute() [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]
C:\Program Files\dotnet\sdk\5.0.203\Microsoft.Common.CurrentVersion.targets(2182,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [C:\Git\surpass.common.documentreport\DocumentReport\DocumentReport.csproj]

I've tried to compare detailed build logs for this step from VS and from CLI and there is only 1 difference:
In VS there is a following text:

TargetedRuntimeVersion:
        v4.0.30319

But when I execute dotnet build I get

TargetedRuntimeVersion:
             v

When I remove a reference for this third-party DLL and comment out all the code that uses it project builds well even via CLI.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions