RecordGenerator icon indicating copy to clipboard operation
RecordGenerator copied to clipboard

Compiler warning CS8669 when using nullable reference types

Open bddckr opened this issue 6 years ago • 3 comments

Looks like the dotnet team decided auto-generated code needs to explicitly opt-in into nullable reference types:

public partial class TestClass
{
    public string? SomeTestProperty { get; }
}

In a project with

<LangVersion>8.0</LangVersion>
<Nullable>enable</Nullable>

this generates without issues and is actually usable, but the following compiler warning is listed for the generated file. One warning per line that uses ? on a reference type.

Warning CS8669 The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.

I'm not sure how to check whether one can/should enable the nullable context in the generated source for all users of this awesome generator, but to resolve it all we need is to add #nullable enable somewhere at the top of the file!

bddckr avatar Dec 05 '19 19:12 bddckr

Hmm. Interesting that they know which files are generated. O.o

Also, good to know that the NRT propagates even though it's expected to support C#7.3 at most.

As per the issue at hand: well, I think the actual support will come only when my upstream (CodeGeneration.Roslyn) will be able to pass the generator a variable saying whether the Nullable feature is on or off in the project.

Maybe it can be done upstream in full, so that the directive, if you have it in your source file, is preserved. That way you can "workaround" by duplicating the project setting in source files as well.

Thanks for bringing attention to this.

amis92 avatar Dec 05 '19 20:12 amis92

Thanks for the quick reply, and the detailed information!

Interesting that they know which files are generated.

I believe Roslyn nowadays checks for GeneratedCodeAttribute, maybe even the <auto-generated> comment tag.

Also, good to know that the NRT propagates even though it's expected to support C#7.3 at most.

For sure! To me, this means this issue is thankfully not a blocker: I just disabled the warning in our project and it's working fine. Thankfully the compiler team added this warning as a specific one, i.e. disabling it won't toggle off anything else as far as I can see.

bddckr avatar Dec 06 '19 11:12 bddckr

I believe Roslyn nowadays checks for GeneratedCodeAttribute, maybe even the comment tag.

the explanation can found be here https://github.com/dotnet/roslyn/blob/70e158ba6c2c99bd3c3fc0754af0dbf82a6d353d/docs/features/nullable-reference-types.md#generated-code

manne avatar Jan 28 '20 21:01 manne