Skip to content

Multi-targeting with AOT dependency results in duplicate conflicting builds #29395

Open
@dfields-msft

Description

@dfields-msft

Describe the bug

I have a solution which involves an AOT library, and a multi-targeted executable which depends on that AOT library. I can successfully build and run the executable and library in VS for all target frameworks, but when I build the solution with dotnet build, the AOT library is built twice in parallel, which besides being unnecessary, errors out due to file contention failures.

To Reproduce

Here's a min-repro solution which demonstrates the problem:
MultiTargetingAotConflictMinRepro.zip

The AOT library targets net7.0 exclusively (since it requires AOT support) and has a custom target which invokes ComputeFilesToPublish after Compile to produce the AOT binary as part of the normal build. It also overrides GetTargetPath to return the path to the native binary that was built instead of the intermediate managed assembly.

The executable multi-targets netstandard2.0 and net7.0, but explicitly specifies net7.0 in the ProjectReference for the AOT library using the SetTargetFramework property since that project by definition can't build for netstandard2.0 (and in fact isn't truly a managed project anyway since it encapsulates the target framework details in the native binary).

Exceptions (if any)

Here's example dotnet build error output that I see (though it varies depending on timing and exactly what kind of file contention is encountered):

MSBuild version 17.4.0+18d5aef85 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
  Generating native code
  Generating native code
LINK : fatal error LNK1104: cannot open file 'bin\Debug\net7.0\win-x64\native\AotNativeLibrary.dll' [C:\Users\dfields\source\repos\MultiTargetingAotConflictMinRepro\AotNativeLibrary\Aot
NativeLibrary.csproj]
     Creating library bin\Debug\net7.0\win-x64\native\AotNativeLibrary.lib and object bin\Debug\net7.0\win-x64\native\AotNativeLibrary.exp
C:\Program Files\dotnet\sdk\7.0.100\Sdks\Microsoft.DotNet.ILCompiler\build\Microsoft.NETCore.Native.targets(350,5): error MSB3073: The command ""C:\Program Files\Microsoft Visual Studio
\2022\Enterprise\VC\Tools\MSVC\14.34.31933\bin\Hostx64\x64\link.exe" @"obj\Debug\net7.0\win-x64\native\link.rsp"" exited with code 1104. [C:\Users\dfields\source\repos\MultiTargetingAot
ConflictMinRepro\AotNativeLibrary\AotNativeLibrary.csproj]
C:\Program Files\dotnet\sdk\7.0.100\Microsoft.Common.CurrentVersion.targets(5150,5): error MSB3030: Could not copy the file "C:\Users\dfields\source\repos\MultiTargetingAotConflictMinRe
pro\AotNativeLibrary\bin\Debug\net7.0\win-x64\native\AotNativeLibrary.dll" because it was not found. [C:\Users\dfields\source\repos\MultiTargetingAotConflictMinRepro\AotNativeLibrary\Ao
tNativeLibrary.csproj]

Build FAILED.

Further technical details

  • Include the output of dotnet --info
.NET SDK:
 Version:   7.0.100
 Commit:    e12b7af219

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22621
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.100\

Host:
  Version:      7.0.0
  Architecture: x64
  Commit:       d099f075e4

.NET SDKs installed:
  3.1.425 [C:\Program Files\dotnet\sdk]
  5.0.414 [C:\Program Files\dotnet\sdk]
  6.0.306 [C:\Program Files\dotnet\sdk]
  7.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.31 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.11 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and its version
Microsoft Visual Studio Enterprise 2022 (64-bit) - Current
Version 17.4.1

Metadata

Metadata

Assignees

Labels

Area-NativeAOTNative AOT compilationuntriagedRequest triage from a team member

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions