Docs: Missing guidance for nullable context in auto-generated files

Created on 14 Oct 2020  Â·  5Comments  Â·  Source: dotnet/docs

When using auto-generated code, the #nullable directive is imperative in the code file; setting the nullable context via the project file is not enough (as per CS8669). I didn't find anything in the docs pointing this out.


Document details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Area - C# Guide Technology - C# Null safety P1 csharp-null-safettech doc-bug dotnet-csharprod

All 5 comments

ping @jcouv @333fred

Can you clarify or point me to the roslyn issue / PR for this? Does this only apply for .generated.cs code files? Or is there another trigger?

I'm not sure if the question was directed at me or at the pinged people.

The file in question is a .cs file, without a .generated in the file name. It does have the following block at the beginning, however; removing this block removes CS8669 and picks up the nullable context from the .csproj file.

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

Can I supply any more information?

Thanks @Thaoden That helps.

I was pinging the C# compiler team, as I can't find this in the compiler specs. We'll address this, but I want the spec details to make sure I get the nuances correct.

@BillWagner if the file is marked as generated, we flip the default and turn off nullable warnings, regardless of the global context. Generators can opt-in by explicitly enabling nullable. There are a few ways to say that a file is generated:

  1. In the .editorconfig, specify generated_code = true in a section that applies to that file.
  2. Put <auto-generated (No, I did not forget the closing tag, that way it works with either <auto-generated> or <auto-generated />) in a comment at the top of the file (can be on any line in that comment, but the comment block must be the first thing in the file).
  3. Start the file name with TemporaryGeneratedFile_
  4. End the file name with .designer.cs, .generated.cs, .g.cs, or .g.i.cs.

http://sourceroslyn.io/#Microsoft.CodeAnalysis/InternalUtilities/GeneratedCodeUtilities.cs,1c5dbec99946c19e,references is the code that actually checks those last 3 rules.

As part of this update, the pages that describe the <Nullable>enable</Nullable> option and the #nullable pragma should also reference this behavior.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Manoj-Prabhakaran picture Manoj-Prabhakaran  Â·  3Comments

sdmaclea picture sdmaclea  Â·  3Comments

sime3000 picture sime3000  Â·  3Comments

garfbradaz picture garfbradaz  Â·  3Comments

ite-klass picture ite-klass  Â·  3Comments