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.65k stars 1.06k forks source link

NoStdLib is not beign respected after net7 upgrade #29110

Open CheloXL opened 1 year ago

CheloXL commented 1 year ago

Version Used: .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: 6.0.201 [C:\Program Files\dotnet\sdk] 6.0.301 [C:\Program Files\dotnet\sdk] 7.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed: 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 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 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]

Steps to Reproduce: I have a project that it's a custom mscorlib library. This is the csproj that I'm using to build it:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net20</TargetFramework>
        <NoStandardLibraries>true</NoStandardLibraries>
        <DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
        <NoStdLib>true</NoStdLib>
        <GenerateAssemblyInfo>false</GenerateAssemblyInfo>
        <DebugType>none</DebugType>
        <RuntimeMetadataVersion>2.0.0.0</RuntimeMetadataVersion>
    </PropertyGroup>

    <PropertyGroup>
        <RootNamespace>System</RootNamespace>
        <AutoGenerateBindingRedirects>False</AutoGenerateBindingRedirects>
    </PropertyGroup>
</Project>

The code was compiling fine under netcore6. After upgrading to netcore7, I'm getting hundred of errors, like ...\Attributes\Reflection\AssemblyTitleAttribute.cs(4,47): warning CS0436: The type 'Attribute' in '...\System\Attribute.cs' conflicts with the imported type 'Attribute' in 'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. Using the type defined in '...\System\Attribute.cs'. [...\jscorelib.csproj] or also ...\System\Byte.cs(9,35): error CS0556: User-defined conversion must convert to or from the enclosing type [...\jscorelib.csproj] Comparing the old project.assets.json generated under obj with the new one, I see that now the compiler is importing the built-in references assemblies.

Old assets (working... I trimmed some entries not relevant to the issue)

{
  "version": 3,
  "targets": {
    ".NETFramework,Version=v2.0": {}
  },
  "libraries": {},
  "projectFileDependencyGroups": {
    ".NETFramework,Version=v2.0": []
  },
  "project": {
    "version": "21.12.23.1648",
    "restore": {
....
      "originalTargetFrameworks": [
        "net20"
      ],
      "frameworks": {
        "net20": {
          "targetAlias": "net20",
          "projectReferences": {}
        }
      },
      "warningProperties": {
        "warnAsError": [
          "NU1605"
        ]
      }
    },
    "frameworks": {
      "net20": {
        "targetAlias": "net20",
        "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\6.0.101\\RuntimeIdentifierGraph.json"
      }
    }
  }
}

New assets:

{
  "version": 3,
  "targets": {
    ".NETFramework,Version=v2.0": {
      "Microsoft.NETFramework.ReferenceAssemblies/1.0.2": {
        "type": "package",
        "dependencies": {
          "Microsoft.NETFramework.ReferenceAssemblies.net20": "1.0.2"
        }
      },
      "Microsoft.NETFramework.ReferenceAssemblies.net20/1.0.2": {
        "type": "package",
        "build": {
          "build/Microsoft.NETFramework.ReferenceAssemblies.net20.targets": {}
        }
      }
    }
  },
  "libraries": {
    "Microsoft.NETFramework.ReferenceAssemblies/1.0.2": {
      "sha512": "5/cSEVld+px/CuRrbohO/djfg6++eR6zGpy88MgqloXvkj//WXWpFZyu/OpkXPN0u5m+dN/EVwLNYFUxD4h2+A==",
      "type": "package",
      "path": "microsoft.netframework.referenceassemblies/1.0.2",
      "files": [
        ".nupkg.metadata",
        ".signature.p7s",
        "microsoft.netframework.referenceassemblies.1.0.2.nupkg.sha512",
        "microsoft.netframework.referenceassemblies.nuspec"
      ]
    },
    "Microsoft.NETFramework.ReferenceAssemblies.net20/1.0.2": {
      "sha512": "XWka/n9cKLI8sNZf8ROWzrcsZ7ucFvR7WfBIMwt+dKX4JFy2zB9Jm6hkpsv6yoyBMCvqHJl0laWKz47qhPOHxQ==",
      "type": "package",
      "path": "microsoft.netframework.referenceassemblies.net20/1.0.2",
      "files": [
        ".nupkg.metadata",
        ".signature.p7s",
        "build/.NETFramework/v2.0/Accessibility.dll",
        "build/.NETFramework/v2.0/AspNetMMCExt.dll",
        "build/.NETFramework/v2.0/CustomMarshalers.dll",
        "build/.NETFramework/v2.0/IEExecRemote.dll",
        "build/.NETFramework/v2.0/IEHost.dll",
        "build/.NETFramework/v2.0/IIEHost.dll",
        "build/.NETFramework/v2.0/ISymWrapper.dll",
        "build/.NETFramework/v2.0/Microsoft.Build.Engine.dll",
        "build/.NETFramework/v2.0/Microsoft.Build.Framework.dll",
        "build/.NETFramework/v2.0/Microsoft.Build.Tasks.dll",
        "build/.NETFramework/v2.0/Microsoft.Build.Utilities.dll",
        "build/.NETFramework/v2.0/Microsoft.JScript.dll",
        "build/.NETFramework/v2.0/Microsoft.VisualBasic.Compatibility.Data.dll",
        "build/.NETFramework/v2.0/Microsoft.VisualBasic.Compatibility.dll",
        "build/.NETFramework/v2.0/Microsoft.VisualBasic.Vsa.dll",
        "build/.NETFramework/v2.0/Microsoft.VisualBasic.dll",
        "build/.NETFramework/v2.0/Microsoft.VisualC.dll",
        "build/.NETFramework/v2.0/Microsoft.Vsa.Vb.CodeDOMProcessor.dll",
        "build/.NETFramework/v2.0/Microsoft.Vsa.dll",
        "build/.NETFramework/v2.0/Microsoft_VsaVb.dll",
        "build/.NETFramework/v2.0/RedistList/FrameworkList.xml",
        "build/.NETFramework/v2.0/SubsetList/Client.xml",
        "build/.NETFramework/v2.0/System.Configuration.Install.dll",
        "build/.NETFramework/v2.0/System.Configuration.dll",
        "build/.NETFramework/v2.0/System.Data.OracleClient.dll",
        "build/.NETFramework/v2.0/System.Data.SqlXml.dll",
        "build/.NETFramework/v2.0/System.Data.dll",
        "build/.NETFramework/v2.0/System.Deployment.dll",
        "build/.NETFramework/v2.0/System.Design.dll",
        "build/.NETFramework/v2.0/System.DirectoryServices.Protocols.dll",
        "build/.NETFramework/v2.0/System.DirectoryServices.dll",
        "build/.NETFramework/v2.0/System.Drawing.Design.dll",
        "build/.NETFramework/v2.0/System.Drawing.dll",
        "build/.NETFramework/v2.0/System.EnterpriseServices.Thunk.dll",
        "build/.NETFramework/v2.0/System.EnterpriseServices.Wrapper.dll",
        "build/.NETFramework/v2.0/System.EnterpriseServices.dll",
        "build/.NETFramework/v2.0/System.Management.dll",
        "build/.NETFramework/v2.0/System.Messaging.dll",
        "build/.NETFramework/v2.0/System.Runtime.Remoting.dll",
        "build/.NETFramework/v2.0/System.Runtime.Serialization.Formatters.Soap.dll",
        "build/.NETFramework/v2.0/System.Security.dll",
        "build/.NETFramework/v2.0/System.ServiceProcess.dll",
        "build/.NETFramework/v2.0/System.Transactions.dll",
        "build/.NETFramework/v2.0/System.Web.Mobile.dll",
        "build/.NETFramework/v2.0/System.Web.RegularExpressions.dll",
        "build/.NETFramework/v2.0/System.Web.Services.dll",
        "build/.NETFramework/v2.0/System.Web.dll",
        "build/.NETFramework/v2.0/System.Windows.Forms.dll",
        "build/.NETFramework/v2.0/System.Xml.dll",
        "build/.NETFramework/v2.0/System.dll",
        "build/.NETFramework/v2.0/cscompmgd.dll",
        "build/.NETFramework/v2.0/mscorlib.dll",
        "build/.NETFramework/v2.0/sysglobl.dll",
        "build/Microsoft.NETFramework.ReferenceAssemblies.net20.targets",
        "microsoft.netframework.referenceassemblies.net20.1.0.2.nupkg.sha512",
        "microsoft.netframework.referenceassemblies.net20.nuspec"
      ]
    }
  },
  "projectFileDependencyGroups": {
    ".NETFramework,Version=v2.0": [
      "Microsoft.NETFramework.ReferenceAssemblies >= 1.0.2"
    ]
  },
  "project": {
    "version": "22.11.17.1138",
    "restore": {
....
      "originalTargetFrameworks": [
        "net20"
      ],
      "sources": {
....
      },
      "frameworks": {
        "net20": {
          "targetAlias": "net20",
          "projectReferences": {}
        }
      },
      "warningProperties": {
        "warnAsError": [
          "NU1605"
        ]
      }
    },
    "frameworks": {
      "net20": {
        "targetAlias": "net20",
        "dependencies": {
          "Microsoft.NETFramework.ReferenceAssemblies": {
            "suppressParent": "All",
            "target": "Package",
            "version": "[1.0.2, )",
            "autoReferenced": true
          }
        },
        "runtimeIdentifierGraphPath": "C:\\Program Files\\dotnet\\sdk\\7.0.100\\RuntimeIdentifierGraph.json"
      }
    }
  }
}
jaredpar commented 1 year ago

Tried a quick repro and mscorlib is being provided to the compiler here which is the root of the errors:

image

Note: I do not know if the SDK intended for this to work or not. I will let them decide but we're definitely getting mscorlib passed here.

LonghronShen commented 6 months ago

Investigated and found this PR 10981 Just disable the newly added AutomaticallyUseReferenceAssemblyPackages option in your csproj to solve the problem:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <NoStdLib>true</NoStdLib>
        <DisableImplicitFrameworkReferences>true</DisableImplicitFrameworkReferences>
        <AutomaticallyUseReferenceAssemblyPackages>false</AutomaticallyUseReferenceAssemblyPackages>
    </PropertyGroup>
</Project>

This allows you to use more recent versions of the .NET Core SDKs to build projects that need the NoStdLib option.