Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dotnet publish won't publish self contained exe without a RuntimeIdentifier. I am passing -r in the command. #10566

Closed
ajmcateer opened this issue Oct 22, 2019 · 19 comments
Labels
Partner request requests from partners

Comments

@ajmcateer
Copy link

Steps to reproduce

Run the following command
dotnet publish --self-contained true -f netcoreapp3.0 -r linux-x64

Expected behavior

Should publish a single executable file for the given runtime

Actual behavior

C:\Program Files\dotnet\sdk\3.0.100\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.RuntimeIdentifierInference.targets(127,5): error NETSDK1031: It is not supported to build or publish a self-contained application without specifying a RuntimeIdentifier. Please either specify a RuntimeIdentifier or set SelfContained to false. [C:\Users\ardsafa\Documents\git\fdere\fdere\fdere\fdere.csproj]

Environment data

.NET Core SDK (reflecting any global.json): Version: 3.0.100
Commit: 04339c3

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

Host (useful for support):
Version: 3.0.0
Commit: 7d57652f33

.NET Core SDKs installed:
2.1.700 [C:\Program Files\dotnet\sdk]
2.2.300 [C:\Program Files\dotnet\sdk]
3.0.100-rc1-014190 [C:\Program Files\dotnet\sdk]
3.0.100 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0-rc1.19457.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.13 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0-rc1-19456-20 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0-rc1-19456-20 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

@ajmcateer
Copy link
Author

ajmcateer commented Oct 22, 2019

Alright so just figured something out. My project setup is
Command line app using .netcore 3.0
GUI app using .netcore 3.0 and Avalonia

The Gui app references the command line app to call some code directly I guess it is not passing -r to the command line app. If I manually change the csproj files for both and specify the same runtime it works. But I want to build this for all Windows, Linux, Mac its kind of a pain, to edit both each time.

@msftgits msftgits transferred this issue from dotnet/cli Jan 31, 2020
@neobenedict
Copy link

neobenedict commented Mar 21, 2020

Same issue.

Edit: Can be fixed by going to the linked projects .csproj file and adding a RuntimeIdentifiers tag with all the platforms you are releasing for:

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RuntimeIdentifiers>win10-x64;linux-x64</RuntimeIdentifiers>
  </PropertyGroup>

To be honest, I'm not sure if this even is a bug, but it's a useless error message that doesn't help me resolve the problem, and probably won't help others aside from finding this git issue.

@dasMulli
Copy link
Contributor

There are two ways to work around this:

  1. --self-contained true is actually the default when passing -r RID - there's no need to explicitly specify it and your dependencies should build fine. If set to false (platform specific, non-self-contained), that should also work.

  2. Adjust your ProjectReference to the referenced app to block the global property passed via the CLI to the referenced project:

<ItemGroup>
  <ProjectReference Include="..\TestCons\TestCons.csproj" GlobalPropertiesToRemove="SelfContained" />
</ItemGroup>

@codingyourlife
Copy link

The Gui app references the command line app

I think you ran into this issue: #10902 which is still open at the time of writing.

In my case I could implement a workaround as @neobenedict suggested which really not ideal.

@gandhis1
Copy link

This error is kind of silly. If you just drop the --self-contained then it works. I can't see a rational reason why having it - although it is redundant - should cause this to fail.

database64128 added a commit to database64128/shadowsocks-uri-generator that referenced this issue Feb 16, 2021

Verified

This commit was signed with the committer’s verified signature.
- Workaround for .NET bug dotnet/sdk#10566, dotnet/sdk#10902
@bricelam
Copy link
Contributor

bricelam commented Aug 13, 2021

Interestingly, passing --use-current-runtime instead of --runtime works without error. (Obviously, this isn't a valid workaround if you're targeting a different runtime.)

@bricelam
Copy link
Contributor

In addition to @dasMulli's workaround, I also needed to add /p:ValidateExecutableReferencesMatchSelfContained=false

@waltdestler
Copy link

Dropping the --self-contained argument is now a less-desirable option in .NET 6, because it now gives a warning:

warning NETSDK1179: One of '--self-contained' or '--no-self-contained' options are required when '--runtime' is used.

JoshuaWierenga added a commit to JoshuaWierenga/EfiSharp that referenced this issue Nov 11, 2021

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
not sure if --no-self-contained is the right flag but dotnet/sdk#10566 prevents using --self-contained true even if it were the right one.
@beatcracker
Copy link

beatcracker commented Nov 22, 2021

Hit the same issue. I've a .NET 6 project with tests that reference ASP.NET Core Web app. The tests refuse to build with NETSDK1031 when --runtime and --self-contained are specified.

@ptasev
Copy link

ptasev commented Nov 23, 2021

I'm also now struggling with this as I added --self-contained due to the new warning in .NET 6, and the test projects are giving the NETSDK1031 error. This is for a solution with 150+ projects so some of the work-arounds provided aren't ideal.

The error doesn't seem to help much either as one of the errors points to a project defined as the following which doesn't make sense since it's not an executable:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <IsPackable>false</IsPackable>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="Zio" Version="0.9.1" />
    </ItemGroup>

</Project>

@beatcracker
Copy link

beatcracker commented Nov 23, 2021

Specifying RuntimeIdentifier / SelfContained in project/props files works, but anything coming from the commandline (--runtime , -property:RuntimeIdentifier) - does not. This messes up our CI royally.

I dumped binlogs for affected project and the main difference is that RuntimeIdentifier property specified in the project file doesn't get removed by MSBuild, while --runtime coming from the commandline is obliterated.

  • --runtime / --self-contained from commandline
    fail-gh

  • RuntimeIdentifier / SelfContained in project file
    kludge-gh

@beatcracker
Copy link

beatcracker commented Nov 23, 2021

Here is the kludge for CI that I'm going to use for now 🦨...

  • Directory.Build.Props / *.csproj
<Project>
  <PropertyGroup Condition=" '$(TF_BUILD)' != '' AND '$(DOTNET_RUNTIME_IDENTIFIER)' != '' ">
    <RuntimeIdentifier>$(DOTNET_RUNTIME_IDENTIFIER)</RuntimeIdentifier>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(TF_BUILD)' != '' AND '$(DOTNET_SELF_CONTAINED)' != '' ">
    <SelfContained>$(DOTNET_SELF_CONTAINED)</SelfContained>
  </PropertyGroup>
</Project>
  • Azure DevOps
    In reality it's much more complex then this, but this is the gist:
  - task: DotNetCoreCLI@2
    displayName: '.NET | Build'
    inputs:
      command: 'build'
      projects: '**/*.csproj'
      arguments: >-
        --configuration Release
        --runtime debian-x64
        --self-contained
    env:
      DOTNET_RUNTIME_IDENTIFIER: 'debian-x64'
      DOTNET_SELF_CONTAINED: 'True'

@glihm
Copy link

glihm commented Mar 18, 2022

Hello there, I still issue this problem with .NET6 6.0.300-preview.22154.4 when -r an --self-contained are used. I am using the publish command.
Going through the code:

// in the file: ./src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Sdk.targets:
<NETSdkWarning Condition="'$(_SelfContainedWasSpecified)' != 'true' and
                          '$(_CommandLineDefinedRuntimeIdentifier)' == 'true' and
                          '$(TargetFrameworkIdentifier)' == '.NETCoreApp' and
                          '$([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '6.0'))"               
                           ResourceName="SelfContainedOptionShouldBeUsedWithRuntime" />

So it might be related to the fact that _SelfContainedWasSpecified is not correctly set.

// in the file: ./src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.RuntimeIdentifierInference.targets
// But this is only to avoid breaking changes from 1.0 behavior.
<_SelfContainedWasSpecified Condition="'$(SelfContained)' != ''">true</_SelfContainedWasSpecified>
<SelfContained Condition="'$(SelfContained)' == '' and '$(RuntimeIdentifier)' != ''">true</SelfContained>
<SelfContained Condition="'$(SelfContained)' == ''">false</SelfContained>

So, then into the option parser, I couldn't see where the issue is.
In the files:
src/Cli/dotnet/CommonOptions.cs
src/Cli/dotnet/ParseResultExtensions.cs
src/Cli/dotnet/ShellShim/ShellShimTemplateFinder.cs

Which looks good to me:

var selfContainedSpecified = parseResult.HasOption(SelfContainedOption) || parseResult.HasOption(NoSelfContainedOption);

So, I don't know where to search more.

@beatcracker
Copy link

TWIMC

Basically, I gave up and just patch Microsoft.Common.CurrentVersion.targets during our AzD builds :feelsgood:

# Patch Microsoft.Common.CurrentVersion.targets in all installed 6.x SDKs
& 'dotnet' @('--list-sdks') |
Select-String -Pattern '(6\.\d+\.\d+).+?\[(.+?)\]' -AllMatches |
ForEach-Object { Join-Path $_.Matches.Groups[2].Value $_.Matches.Groups[1].Value } |
Where-Object { Test-Path -LiteralPath $_ -PathType Container } |
Join-Path -ChildPath 'Microsoft.Common.CurrentVersion.targets' |
Where-Object { Test-Path -LiteralPath $_ -PathType Leaf } |
ForEach-Object {
    # Fail fast
    try {
        [xml]$xml = [System.IO.File]::ReadAllText($_)
        $nsm = [System.Xml.XmlNamespaceManager]::new($xml.NameTable)
        $nsm.AddNamespace('ns', $xml.Project.xmlns)
        $n = $xml.xSelectSingleNode(
            "/ns:Project/ns:Target[@Name='GetTargetFrameworks']/ns:ItemGroup/ns:_ThisProjectBuildMetadata/ns:IsRidAgnostic/@Condition",
            $nsm
        )

        # Add check for for _CommandLineDefinedRuntimeIdentifier
        # https://github.com/dotnet/msbuild/pull/6924#issuecomment-978128930
        $n.'#text' = ' {0} ' -f @'
'$(RuntimeIdentifier)' == '' and '$(RuntimeIdentifiers)' == '' and $(_CommandLineDefinedRuntimeIdentifier) == ''
'@
        $xml.Save($_)
    }
    finally {}
}

@thefringeninja
Copy link

thefringeninja commented May 18, 2022

I'm having the same problem. Running dotnet publish --configuration Release --runtime linux-x64 on the solution gives error NETSDK1031: It is not supported to build or publish a self-contained application without specifying a RuntimeIdentifier. You must either specify a RuntimeIdentifier or set SelfContained to false. <SelfContained>true</SelfContained> is in the project file itself.

I should note that the project in question gets successfully published anyway.

@GaTechThomas
Copy link

  1. Adjust your ProjectReference to the referenced app to block the global property passed via the CLI to the referenced project:
<ItemGroup>
  <ProjectReference Include="..\TestCons\TestCons.csproj" GlobalPropertiesToRemove="SelfContained" />
</ItemGroup>

This was the solution for me.

Just add GlobalPropertiesToRemove="SelfContained" to the end of the ProjectReference in the .csproj file.

@nagilson nagilson added the needs team triage Requires a full team discussion label Nov 30, 2022
@marcpopMSFT marcpopMSFT removed the needs team triage Requires a full team discussion label Dec 7, 2022
@marcpopMSFT
Copy link
Member

We believe this has been fixed at this point as the original issue was from 2019. If it's still broken in 7.0.100, please file a new issue.

@adschmu
Copy link

adschmu commented Jan 10, 2023

Confirmation:
For me, this is fixed with .NET 7 SDK. I can remove the GlobalPropertiesToRemove="SelfContained" which I needed for .NET 6 to build.

@grzegorz-wolszczak
Copy link

Is the fix backported to .NET 6 ? Not everyone can use .NET 7 yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Partner request requests from partners
Projects
None yet
Development

No branches or pull requests