sdk: "A self-contained executable cannot be referenced by a non self-contained executable" error on unit tests

I’m getting the error:

The referenced project ‘{project}’ is a self-contained executable. A self-contained executable cannot be referenced by a non self-contained executable.

In my scenario, there’s a project which is indeed self-contained, and a unit test project which depends on that project and tests it.

This error seems to imply that self-contained projects should not have unit tests, which can’t be the case.

An easy workaround is to set <ValidateExecutableReferencesMatchSelfContained>false</ValidateExecutableReferencesMatchSelfContained>, but it seems like the validation is incorrect in this scenario.

For reference, the self-contained project looks something like:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <DeployOnBuild>true</DeployOnBuild>
    <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
    <SelfContained>true</SelfContained>
    <PublishSingleFile>true</PublishSingleFile>
  </PropertyGroup>
</Project>

and the unit test project:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <RuntimeIdentifier>linux-x64</RuntimeIdentifier>
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\TheSelfContainedProject\TheSelfContainedProject.csproj" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.8.3" />
    <PackageReference Include="MSTest.TestAdapter" Version="2.1.2" />
    <PackageReference Include="MSTest.TestFramework" Version="2.1.2" />
  </ItemGroup>
</Project>

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (5 by maintainers)

Commits related to this issue

Most upvoted comments

This should be fixed with #17594, but until then you can work around it by setting ValidateExecutableReferencesMatchSelfContained to false in your unit test projects.

@dsplaisted after putting in the recommended workaround we get this error:

C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(4969,5): error MSB3030: Could not copy the file "C:\Repos\eagle\Features\ModelDesigner\ModelDesignerGenerator\obj\Debug\apphost.exe" because it was not found. [C:\Repos\eagle\Features\ModelDesigner\ModelDesignerGenerator\ModelDesignerGenerator.csproj]

Full output logs

PS C:\Repos\eagle> dotnet publish ".\Features\Sample\Sample.Server\Sample.Server.csproj" -r win-x86 -bl
Microsoft (R) Build Engine version 17.0.0-preview-21329-01+1b7661f36 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\MSBuild.dll -bl -distributedlogger:Microsoft.DotNet.Tools.MSBuild.MSBuildLogger,C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\dotnet.dll*Microsoft.DotNet.Tools.MSBuild.MSBuildForwardingLogger,C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\dotnet.dll -maxcpucount -property:RuntimeIdentifier=win-x86 -restore -target:Publish -verbosity:m .\Features\Sample\Sample.Server\Sample.Server.csproj
  Determining projects to restore...
  All projects are up-to-date for restore.
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
  Eagle.Core -> C:\Repos\eagle\Core\Eagle.Core\bin\Debug\Eagle.Core.dll
  Eagle.Framework.Server.DataUpgrade -> C:\Repos\eagle\Framework\Server\Persistence\DatabaseUpgrade\Eagle.Framework.Server.DataUpgrade\bin\Debug\Eagle.Framework.Server.DataUpgrade.dll
  Eagle.Framework.Server.DataCleanup -> C:\Repos\eagle\Framework\Server\Persistence\DatabaseUpgrade\Eagle.Framework.Server.DataCleanup\bin\Debug\Eagle.Framework.Server.DataCleanup.dll
  Eagle.Framework.Common -> C:\Repos\eagle\Framework\Common\Eagle.Framework.Common\bin\Debug\Eagle.Framework.Common.dll
  TEST.FlaggedFeature -> C:\Repos\eagle\Output\Debug\Sample\Sample\TEST.FlaggedFeature.dll
  Eagle.Framework.Common.Diagnostics -> C:\Repos\eagle\Framework\Common\Eagle.Framework.Common.Diagnostics\bin\Debug\Eagle.Framework.Common.Diagnostics.dll
  EagleServer.Common -> C:\Repos\eagle\Framework\Server\EagleServer.Common\bin\Debug\EagleServer.Common.dll
  Eagle.Framework.Server.Core -> C:\Repos\eagle\Framework\Server\Eagle.Framework.Server.Common\bin\Debug\Eagle.Framework.Server.Core.dll
  ModelDesignerDataExchanger -> C:\Repos\eagle\Features\ModelDesigner\ModelDesignerDataExchanger\bin\Debug\ModelDesignerDataExchanger.dll
  Eagle.Framework.Server.ConflictResolver -> C:\Repos\eagle\Framework\Server\Eagle.Framework.Server.ConflictResolver\bin\Debug\net5\Eagle.Framework.Server.ConflictResolver.dll
  PropertyMetaDataCache -> C:\Repos\eagle\Framework\Server\Persistence\PropertyMetaDataCache\bin\Debug\net5.0\PropertyMetaDataCache.dll
  Eagle.Framework.Server.SelfUpdate -> C:\Repos\eagle\Framework\Server\Eagle.Framework.Server.SelfUpdate\bin\Debug\Eagle.Framework.Server.SelfUpdate.dll
  ModelDesignerHelper -> C:\Repos\eagle\Features\ModelDesigner\ModelDesignerHelper\bin\Debug\ModelDesignerHelper.dll
C:\Program Files\dotnet\sdk\6.0.100-preview.6.21355.2\Microsoft.Common.CurrentVersion.targets(4969,5): error MSB3030: Could not copy the file "C:\Repos\eagle\Features\ModelDesigner\ModelDesignerGenerator\obj\Debug\apphost.exe" because it was not found. [C:\Repos\eagle\Features\ModelDesigner\ModelDesignerGenerator\ModelDesignerGenerator.csproj]
PS C:\Repos\eagle>

Binlog: binlog.zip

Note that ModelDesignerGenerator is a exe type.

@dsplaisted We also have this scenario by design. We have our unit tests alongside the code that they test and the files get excluded from release builds. This means that all of our Projects (in Debug mode anyway) are Test Projects

This is also happening dotnet 6 Preview 6! This is a big stopper for us and we can’t even upgrade to the latest version of dotnet 5 because that also has the problem. @dsplaisted when will this be fixed in dotnet 5 and dotnet 6?

Happening here on SDK 6.0.100-preview.5.21302.13 as well, when is this going to be released?