-
Notifications
You must be signed in to change notification settings - Fork 4k
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
Dev nuget Package Microsoft.Net.Compilers.Toolset v 3.8.0-1.20325.3 when using init
feature in c# 9 raise error: Predefined type 'System.Runtime.CompilerServices.IsExternalInit' is not defined or imported
#45510
Comments
Ahhh, I just posted a similar question here: (I am using preview6 sdk though) |
I've encountered the same problem, and even the latest master branch (June 27) in Sharplab.io generates the same error. I've used the same fix. |
Thanks for reporting the issue. This is not a bug in the compiler. The |
(SDK Preview 8 + VS Pro 16.8 Preview 2.0) This works fine now for If dummy class is added, the other tfms works fine. |
Trivial workaround: just declare the missing type in your project: // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel;
namespace System.Runtime.CompilerServices
{
/// <summary>
/// Reserved to be used by the compiler for tracking metadata.
/// This class should not be used by developers in source code.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class IsExternalInit
{
}
} If you're multitargeting net5.0 and other TFMs, just add this bit of MSBuild to remove it for net5.0 since it's built-in: <ItemGroup>
<Compile Remove="IsExternalInit.cs" Condition="'$(TargetFramework)' == 'net5.0'" />
</ItemGroup> |
I stil have this within net standard 2.1 projects with the release from today. an anyone confirm? |
@BoasE I can confirm that. More about target framework https://docs.microsoft.com/en-us/dotnet/standard/net-standard#when-to-target-net50-vs-netstandard |
I recommend adding this file to your project, based on the example above from @kzu. It will handle multi-targeting and marks the attribute as internal to avoid conflicts with other packages. // Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#if NETSTANDARD2_0 || NETSTANDARD2_1 || NETCOREAPP2_0 || NETCOREAPP2_1 || NETCOREAPP2_2 || NETCOREAPP3_0 || NETCOREAPP3_1 || NET45 || NET451 || NET452 || NET46 || NET461 || NET462 || NET47 || NET471 || NET472 || NET48
using System.ComponentModel;
// ReSharper disable once CheckNamespace
namespace System.Runtime.CompilerServices
{
/// <summary>
/// Reserved to be used by the compiler for tracking metadata.
/// This class should not be used by developers in source code.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class IsExternalInit
{
}
}
#endif |
Thanks for the workaround. Still feels like a hack though. I doubt that it should be the responsibility of the developer to provide internal system classes to make the compiler work properly... |
I generally agree, but this isn't the first feature like this. A similar (and larger) workaround file is necessary for nullable reference types on older frameworks. I know some language features aren't really usable on older versions of the framework, but this one is. Feels like Microsoft could publish a NuGet package that gives us what we need for older frameworks, much like was done with IAsyncEnumerable in Microsoft.Bcl.AsyncInterfaces. It could probably even be a source code package instead of a binary package. I see a couple of packages from 3rd parties that may do this, but I'd prefer something official and maintained. |
This is a C# 9 feature and hence only officially supported when targeting This is how C# has functioned for many features over the years. Extension methods are a very analogous example here. Unless you target a target framework with the suporting |
What I added to my approach is bit of msbuild: <ItemGroup>
<Compile Remove="IsExternalInit.cs" Condition="'$(TargetFramework)' == 'net5.0'" />
</ItemGroup> |
But we have to use the class with |
That stray |
@tullo-x86 Thanks for pointing that out, fixed. |
@brantburnett Wouldn't it be enough to just do |
@Jorenkv Yes, that would also work. Personally, I like this more declarative approach a bit better, it makes it a bit more clear where it's used. But that's just a preference. |
Version Used:
Dev nuget package: Microsoft.Net.Compilers.Toolset v 3.8.0-1.20325.3
Steps to Reproduce:
1- Install Net5 Preview 5 SDK.
2- Create a console project and add the next class to use c# 9
init
feature3- Add a reference to
Microsoft.Net.Compilers.Toolset
to the project..NET Core SDK Net5 Preview 5 is not supporting
init
of C# 9 feature yet.4- build
Expected Behavior:
Build success.
Actual Behavior:
The compiler raise error
5- Add this dummy class to the project:
6- build again
Build success and application run sucessfully.
Is
IsExternalInit
a missing class?If
IsExternalInit
class has no role remove any calling to it.The text was updated successfully, but these errors were encountered: