Skip to content
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

Compiler warning CS8669 when using nullable reference types #110

Open
bddckr opened this issue Dec 5, 2019 · 3 comments
Open

Compiler warning CS8669 when using nullable reference types #110

bddckr opened this issue Dec 5, 2019 · 3 comments
Labels
API design Generated API design discussions help wanted
Milestone

Comments

@bddckr
Copy link

bddckr commented Dec 5, 2019

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!

@amis92
Copy link
Owner

amis92 commented Dec 5, 2019

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 amis92 added this to the Future milestone Dec 5, 2019
@amis92 amis92 added API design Generated API design discussions help wanted labels Dec 5, 2019
@bddckr
Copy link
Author

bddckr commented Dec 6, 2019

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.

@manne
Copy link

manne commented Jan 28, 2020

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API design Generated API design discussions help wanted
Projects
None yet
Development

No branches or pull requests

3 participants