Skip to content

dotnet publish Targets Different Frameworks when Publishing Multiple Projects #16450

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

Closed
ilharp opened this issue Mar 20, 2021 · 6 comments
Closed
Milestone

Comments

@ilharp
Copy link

ilharp commented Mar 20, 2021

Summary

Run dotnet publish for two different projects in one solution, and published artifacts targeted different frameworks (5.0.0 and 5.0.4). This causes the program to fail to start.

Detail

Repo: Ruminoid/Toolbox / Tag: v0.2.0

My build process (Build.cs) triggers dotnet publish for each project in the solution, and sets self-contained publishing (L125) for the two executables rmbox and rmbox-shell. The CI log shows that rmbox (log) and the rmbox-shell (log) calls dotnet publish using the same parameters, but the result executables targeted different frameworks (5.0.0 and 5.0.4) in runtimeconfig.json.

In published artifacts in the CI Build for v0.2.0:

rmbox.runtimeconfig.json:

{
  "runtimeOptions": {
    "tfm": "net5.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "5.0.0"
    }
  }
}

rmbox-shell.runtimeconfig.json:

{
  "runtimeOptions": {
    "tfm": "net5.0",
    "includedFrameworks": [
      {
        "name": "Microsoft.NETCore.App",
        "version": "5.0.4"
      }
    ]
  }
}

As a result, the dependencies (e.g. coreclr.dll) (version 5.0.421.11614) of rmbox-shell replaces the dependencies for rmbox and cause it to rollback to find the framework installed on the computer, and finally fails to start.

It's weird that if I runs the nuke Publish command (which execute Build.cs) on my computer, the rmbox-shell.runtimeconfig.json is the same as rmbox.runtimeconfig.json, both are targeting version 5.0.0. So I think this may be a bug caused by dotnet/sdk or the GitHub Actions.

Here are a few reasons I think this is a failure caused by dotnet/sdk or GitHub Actions

  • I runs the publish command on my computer and found that the two executables both work properly.

  • This problem has been there since v0.1.6. The CI Build for v0.1.6 uses the .NET SDK version 5.0.201 (log) with the bug existing while the CI Build for v0.1.5 uses the .NET SDK version 5.0.103 (log) with the program working properly.

@ghost ghost added the untriaged Request triage from a team member label Mar 20, 2021
@ghost
Copy link

ghost commented Mar 20, 2021

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

ilharp added a commit to Ruminoid/Toolbox that referenced this issue Mar 20, 2021
@ilharp
Copy link
Author

ilharp commented Mar 21, 2021

Update: I add a actions/setup-dotnet step in my GitHub CI config to block the 5.0.201 SDK and force build to use 5.0.103. The two apps are both working properly (Build #68).

But I think the dotnet publish artifact using the same command line on one computer should target the same framework, whatever the SDK version is. As a temporary solution, I can now build the project by forcing to specify the SDK version, but I hope there will be a better way to solve this problem in the future.

@ilharp
Copy link
Author

ilharp commented Mar 23, 2021

Update: The solution above still have problems on MacOS computers so I found this issue to solve this. According to this issue, a better solution now is to add a global.json file using dotnet new globaljson --sdk-version "<sdk version>". I add the file and it now builds properly.

@ilharp
Copy link
Author

ilharp commented Apr 3, 2021

Update: Setting "version": "5.0.201" also caused this problem.

@ilharp ilharp changed the title "dotnet publish" Targets Different Frameworks when Publishing Multiple Projects dotnet publish Targets Different Frameworks when Publishing Multiple Projects Apr 17, 2021
@dsplaisted
Copy link
Member

I'm not sure what would be causing this. Is it possible to get a binlog of the build that seems to be using the wrong runtime version (5.0.0)?

@dsplaisted dsplaisted removed their assignment Jul 30, 2021
@dsplaisted dsplaisted added this to the Discussion milestone Jul 30, 2021
@dsplaisted dsplaisted removed the untriaged Request triage from a team member label Jul 30, 2021
@ilharp
Copy link
Author

ilharp commented Aug 31, 2021

Solved. I'll write down the cause of the problem and the solution here for latecomers.


Cause

NETSDK1150

Just use

dotnet publish src/rmbox-shell/ --configuration Release --runtime osx-x64

will get

/Users/runner/.dotnet/sdk/5.0.400/Sdks/Microsoft.NET.Sdk/targets/Microsoft.NET.Sdk.targets(993,5):
error NETSDK1150: The referenced project '../rmbox/rmbox.csproj' is a non self-contained executable.
A non self-contained executable cannot be referenced by a self-contained executable.
[/Users/runner/work/Toolbox/Toolbox/src/rmbox-shell/rmbox-shell.csproj]

See CI#171.

Use .NET SDK 5.0.101 won't get this error because this error was introduced in .NET SDK 5.0.300.

Let's take a look at the current project structure. rmbox-shell references rmbox which is also an executable. If rmbox is not published as a self-contained executable, it cannot be executed directly while it can be depended by rmbox-shell. This is exactly the situation I mentioned above, rmbox failed to run, but rmbox-shell runs normally. This is a by-design behavior, not a bug.

But such behavior does lack a specific description. And then, in .NET SDK 5.0.300, they added NETSDK1150 and NETSDK1151 to alert of this behavior.

And next are several solutions to this problem.

Solution 1: Add --self-contained true parameter

Status: Unavailable

This cause anoher bug of .NET SDK. The --self-contained parameter cannot be used with --runtime.

See #10566, #10902 and #18116.

Solution 2: Use --use-current-runtime parameter

Status: Requires .NET 6.0

See #14093 and #14293.

Solution 3: Modify project files manually using build script

Available and here's my code.

new[]
    {
        SourceDirectory / "rmbox/rmbox.csproj",
        SourceDirectory / "rmbox-shell/rmbox-shell.csproj"
    }
    .ForEach(x =>
        File.WriteAllText(
            x,
            File.ReadAllText(x)
                .Replace(
                    "<!-- RMBOX_BUILD_PROPS -->",
                    $"<RuntimeIdentifier>{Runtime}</RuntimeIdentifier><SelfContained>true</SelfContained>")));

See Build.cs and commit fd6a5a5.

CI of v0.5.5 uses this fix and now build successfully.

@ilharp ilharp closed this as completed Aug 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants