sdk icon indicating copy to clipboard operation
sdk copied to clipboard

`SkipLocalsInit` property to make the SDK generate `[module: SkipLocalsInit]`

Open alexrp opened this issue 2 years ago • 4 comments

Is your feature request related to a problem? Please describe.

Similar to many of the assembly attribute properties (Company, Copyright, etc) it would be useful to have a way to generate a module-level SkipLocalsInitAttribute just by setting an SDK property. Bonus points if the SDK generates the class definition for TFMs that are known not to have it (< net5.0).

I use this attribute in most of my projects and either have to carry around a Module.cs file that contains it, or do the equivalent of what this feature request is about in Directory.Build.targets (for larger solutions).

Describe the solution you'd like

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <SkipLocalsInit>true</SkipLocalsInit>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
</Project>

->

// <auto-generated />
[module: global::System.Runtime.CompilerServices.SkipLocalsInit]

and

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <SkipLocalsInit>true</SkipLocalsInit>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
</Project>

->

// <auto-generated />
[module: global::System.Runtime.CompilerServices.SkipLocalsInit]

namespace System.Runtime.CompilerServices
{
    [global::System.AttributeUsage(
        global::System.AttributeTargets.Module |
        global::System.AttributeTargets.Class |
        global::System.AttributeTargets.Struct |
        global::System.AttributeTargets.Interface |
        global::System.AttributeTargets.Constructor |
        global::System.AttributeTargets.Method |
        global::System.AttributeTargets.Property |
        global::System.AttributeTargets.Event,
        Inherited = false)]
    internal sealed class SkipLocalsInitAttribute : global::System.Attribute
    {
    }
}

alexrp avatar Apr 16 '22 23:04 alexrp

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.

This would be a great idea - the hard part as I see it is the codegen of the shim class. WriteCodeFragment

a) only supports attributes, so cannot generate the shim classes, and b) doesn't currently support module-level attributes, only assembly-level attributes

so wouldn't be fitting for this use case.

Another way to approach this might be a shim NuGet package - it would target netstandard2.0 and net5.0, and for net5.0 contain nothing, but for netstandard2.0 it would contain the shim class you've defined above. Then, it would be easy to add a PackageReference to this NuGet package purely based on a new SkipLocalsInit property.

baronfel avatar Jun 03 '22 17:06 baronfel

WriteCodeFragment sets CodeCompileUnit.AssemblyCustomAttributes but System.CodeDom doesn't seem to have anything similar for custom attributes of a module.

If I understand correctly, SkipLocalsInit is a Roslyn feature and only implemented for C#, not Visual Basic. Can you include the two C# code snippets above as files in the SDK installation tree and generate Compile items that point to them?

KalleOlaviNiemitalo avatar Jun 03 '22 18:06 KalleOlaviNiemitalo

So far I've created a simple NuGet package that implements this behavior: https://github.com/HavenDV/SkipLocalsInit

HavenDV avatar Aug 10 '22 15:08 HavenDV