dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.74k stars 1.07k forks source link

.NET 8: dotnet new creates a .csproj with <TargetFramework>net6.0</TargetFramework> on Linux / same template works on Windows #40017

Open thomiel opened 7 months ago

thomiel commented 7 months ago

Describe the bug

I have got a project template:

{
  "$schema": "http://json.schemastore.org/template",
  "author": "GtkSharp Contributors",
  "classifications": [
    "Gtk",
    "GUI App"
  ],
  "name": "Gtk Application",
  "identity": "GtkSharp.Application.CSharp",
  "groupIdentity": "GtkSharp.Application",
  "shortName": "gtkapp",
  "tags": {
    "language": "C#",
    "type": "project"
  },
  "sourceName": "GtkNamespace",
  "preferNameDirectory": true,
  "symbols": {
    "targetframework": {
      "type": "parameter",
      "description": "The target framework for the project.",
      "defaultValue": "net8.0",
      "replaces": "$(FrameworkParameter)"
    }
  },
  "primaryOutputs": [
    { "path": "GtkNamespace.csproj" }
  ]
}

dotnet new on Linux results in:

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="**\*.glade" />
    <EmbeddedResource Include="**\*.glade">
      <LogicalName>%(Filename)%(Extension)</LogicalName>
    </EmbeddedResource>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="GtkSharp" Version="3.24.24.*" />
  </ItemGroup>

</Project>

On Windows:

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

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net8.0-gtk</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <None Remove="**\*.glade" />
    <EmbeddedResource Include="**\*.glade">
      <LogicalName>%(Filename)%(Extension)</LogicalName>
    </EmbeddedResource>
  </ItemGroup>

</Project>

SDK installed on both OSes is 8.0.203.

I would expect that both dotnet news lead to the same .csproj file.

To Reproduce

dotnet new --install GtkSharp.Template.CSharp
dotnet new gtkapp

Find the complete template here.

Exceptions (if any)

On windows the correct <TargetFramework>net8.0-gtk</TargetFramework> is inserted in the .csproj file.

Further technical details

Include the output of dotnet --info:


dotnet --info
.NET SDK:
 Version:           8.0.203
 Commit:            5e1ceea679
 Workload version:  8.0.200-manifests.dfa7f6b2

Runtime Environment:
 OS Name:     debian
 OS Version:  12
 OS Platform: Linux
 RID:         linux-x64
 Base Path:   /home/thomas/.dotnet/sdk/8.0.203/

.NET workloads installed:
 [gtk]
   Installation Source: SDK 8.0.200
   Manifest Version:    3.24.24-rev.1/8.0.200
   Manifest Path:       /home/thomas/.dotnet/sdk-manifests/8.0.200/gtksharp.net.sdk.gtk/WorkloadManifest.json
   Install Type:        FileBased

Host:
  Version:      8.0.3
  Architecture: x64
  Commit:       9f4b1f5d66

.NET SDKs installed:
  8.0.203 [/home/thomas/.dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 8.0.3 [/home/thomas/.dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 8.0.3 [/home/thomas/.dotnet/shared/Microsoft.NETCore.App]

Other architectures found:
  None

Environment variables:
  DOTNET_ROOT       [/home/thomas/.dotnet]

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

The IDE (VS / VS Code/ VS4Mac) you're running on, and its version: only bash

verbosity diag output:

dotnet new gtkapp --verbosity diag
[2024-04-05 01:45:29.721] [Debug] [Template Engine] => [Execute]: Execute started
[2024-04-05 01:45:29.790] [Debug] [Microsoft.TemplateEngine.Edge.TemplateConstraintManager] => [Execute]: Found 5 constraints factories, initializing.
[2024-04-05 01:45:29.790] [Debug] [Microsoft.TemplateEngine.Edge.TemplateConstraintManager] => [Execute]: Found 5 constraints factories, initializing.
[2024-04-05 01:45:29.793] [Debug] [MSBuildEvaluator] => [Execute]: Output directory is: /home/thomas/GtkSharp/xxx.
[2024-04-05 01:45:29.793] [Debug] [MSBuildEvaluator] => [Execute]: Project full path is: <null>.
[2024-04-05 01:45:29.796] [Debug] [MSBuildEvaluator] => [Execute]: Found project files: .
[2024-04-05 01:45:29.796] [Debug] [MSBuildEvaluator] => [Execute]: No project found.
[2024-04-05 01:45:29.807] [Debug] [Template Engine] => [Execute] => [Template from config /home/thomas/.templateengine/packages/GtkSharp.Template.CSharp.3.24.24.95.nupkg/content/content/GtkSharp.Application.CSharp/.template.config/template.json]: Template from config /home/thomas/.templateengine/packages/GtkSharp.Template.CSharp.3.24.24.95.nupkg/content/content/GtkSharp.Application.CSharp/.template.config/template.json started
[2024-04-05 01:45:29.822] [Debug] [Template Engine] => [Execute] => [Template from config /home/thomas/.templateengine/packages/GtkSharp.Template.CSharp.3.24.24.95.nupkg/content/content/GtkSharp.Application.CSharp/.template.config/template.json]: Template from config /home/thomas/.templateengine/packages/GtkSharp.Template.CSharp.3.24.24.95.nupkg/content/content/GtkSharp.Application.CSharp/.template.config/template.json finished, took 14 ms
[2024-04-05 01:45:29.823] [Debug] [Template Engine] => [Execute]: Found template 'Gtk Application' (GtkSharp.Application.CSharp)
[2024-04-05 01:45:29.823] [Debug] [Template Engine] => [Execute]: The template 'Gtk Application' (GtkSharp.Application.CSharp) has the following validation messages:
   [Info][MV008] Missing 'generatorVersions'.
   [Info][MV009] Missing 'precedence'.

[2024-04-05 01:45:29.824] [Debug] [Microsoft.TemplateEngine.Edge.Template.TemplateCreator] => [Execute] => [Template content generation]: Template content generation started
[2024-04-05 01:45:29.839] [Debug] [NuGetLogger] => [Execute]: Custom source https://api.nuget.org/v3/index.json is already loaded from default configuration.
[2024-04-05 01:45:29.841] [Debug] [NuGetLogger] => [Execute]: Searching for GtkSharp.Template.CSharp in https://api.nuget.org/v3/index.json.
[2024-04-05 01:45:29.862] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'name{-VALUE-FORMS-}identity' was assigned to value 'xxx'.
[2024-04-05 01:45:29.862] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'name{-VALUE-FORMS-}safe_name' was assigned to value 'xxx'.
[2024-04-05 01:45:29.862] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'name{-VALUE-FORMS-}lower_safe_name' was assigned to value 'xxx'.
[2024-04-05 01:45:29.862] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'name{-VALUE-FORMS-}safe_namespace' was assigned to value 'xxx'.
[2024-04-05 01:45:29.862] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'name{-VALUE-FORMS-}lower_safe_namespace' was assigned to value 'xxx'.
[2024-04-05 01:45:29.862] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'language{-VALUE-FORMS-}identity' was assigned to value 'C#'.
[2024-04-05 01:45:29.862] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'type{-VALUE-FORMS-}identity' was assigned to value 'project'.
[2024-04-05 01:45:29.862] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'targetframework{-VALUE-FORMS-}identity' was assigned to value 'net6.0'.
[2024-04-05 01:45:29.874] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'name{-VALUE-FORMS-}identity' was assigned to value 'xxx'.
[2024-04-05 01:45:29.874] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'name{-VALUE-FORMS-}safe_name' was assigned to value 'xxx'.
[2024-04-05 01:45:29.874] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'name{-VALUE-FORMS-}lower_safe_name' was assigned to value 'xxx'.
[2024-04-05 01:45:29.874] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'name{-VALUE-FORMS-}safe_namespace' was assigned to value 'xxx'.
[2024-04-05 01:45:29.874] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'name{-VALUE-FORMS-}lower_safe_namespace' was assigned to value 'xxx'.
[2024-04-05 01:45:29.874] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'language{-VALUE-FORMS-}identity' was assigned to value 'C#'.
[2024-04-05 01:45:29.874] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'type{-VALUE-FORMS-}identity' was assigned to value 'project'.
[2024-04-05 01:45:29.874] [Debug] [Template Engine] => [Execute] => [Template content generation]: [JoinMacro]: Variable 'targetframework{-VALUE-FORMS-}identity' was assigned to value 'net6.0'.
[2024-04-05 01:45:29.890] [Debug] [Microsoft.TemplateEngine.Edge.Template.TemplateCreator] => [Execute] => [Template content generation]: Template content generation finished, took 66 ms
The template "Gtk Application" was created successfully.
[2024-04-05 01:45:29.996] [Debug] [NuGetLogger] => [Execute]:   GET https://api.nuget.org/v3/registration5-gz-semver2/gtksharp.template.csharp/index.json
[2024-04-05 01:45:30.116] [Debug] [NuGetLogger] => [Execute]:   OK https://api.nuget.org/v3/registration5-gz-semver2/gtksharp.template.csharp/index.json 120ms
[2024-04-05 01:45:30.170] [Debug] [NuGetLogger] => [Execute]: Found 37 versions for GtkSharp.Template.CSharp in NuGet feed https://api.nuget.org/v3/index.json.
[2024-04-05 01:45:30.172] [Debug] [NuGetLogger] => [Execute]: Found 2 search endpoints.
[2024-04-05 01:45:30.173] [Debug] [NuGetLogger] => [Execute]: Querying https://azuresearch-usnc.nuget.org/query?q=GtkSharp.Template.CSharp&skip=0&take=1&prerelease=true&semVerLevel=2.0.0

[2024-04-05 01:45:30.773] [Debug] [Template Engine] => [Execute]: Execute finished, took 1054 ms

Note: I was trying to understand how the gtk workload of GtkSharp works, when I stumbled upon the strange bug. I didn't find any reference to .NET in the solution (apart from a VersionGreaterThanOrEquals parameter in the WorkloadManifest.targets file), so I'd assume it's a problem in the SDKs CLI tool.

nagilson commented 5 months ago

The default value for the target framework does not seem to apply in other case, considering the default target framework is 8.0 but does not include '-gtk'. There may be some configuration they have based on the OS that changes the TFM but I do not see any logic for that in their repo. I'm not familiar with how to use these templates, where would that logic be @dotnet/templating-engine-maintainers ?

marcpopMSFT commented 5 months ago

Triage: It kind of sounds the template is not correctly installed and a different copy is being picked up. I might try fully uninstalling it. You can also run dotnet new gtkapp -d to get diagnostic information. @thomiel

thomiel commented 4 days ago

Sorry to keep you waiting for an answer so long, but finally I have prepared a dockerfile (FROM mcr.microsoft.com/dotnet/sdk:8.0) to allow you to easily and reproducably confirm the behavior. On a WSL2 or Linux machine:

git clone https://github.com/Thomas-Mielke-Software/GtkSharp-Docker
cd GtkSharp-Docker
docker build -t gtksharp-env .
docker run -i --rm -t gtksharp-env bash
grep "<TargetFramework>net6.0</TargetFramework>" *

I really wonder where the logic to set the target framework is on Linux...