cs-script icon indicating copy to clipboard operation
cs-script copied to clipboard

How to enable the non-nullable reference feature?

Open sken130 opened this issue 2 years ago • 5 comments

I tried

//css_co -nullable:enable

but it gives no error or warning for string abc = null;

OS: Windows Server 2012 R2 Standard .NET version: 6.0.x cs-script version: 4.4.7 Compiler engine: csc

Output from css -ver:

C# Script execution engine (.NET Core). Version 4.4.7.0.
Copyright (C) 2004-2020 Oleg Shilo. www.csscript.net (github.com/oleg-shilo/cs-script)

   CLR:             6.0.10
   System:          Microsoft Windows NT 6.3.9600.0
   Architecture:    x64
   Install dir:     <not integrated>
   Script engine:   C:\cs-script\netcore\cs-script.win.v4.4.7.0\cscs.dll
   Config file:     C:\cs-script\netcore\cs-script.win.v4.4.7.0\css_config.xml
   Compiler engine: csc (C:\Program Files\dotnet\sdk\6.0.402\Roslyn\bincore\csc.dll)
                  : dotnet (C:\Program Files\dotnet\dotnet.exe)
   NuGet manager:   dotnet
   NuGet cache:     C:\Users\kenlam\.nuget\packages
   Custom commands: C:\ProgramData\cs-script\commands
   Global includes: C:\ProgramData\cs-script\inc

Also, how do I know the C# version being used?

sken130 avatar Oct 26 '22 08:10 sken130

It is not an easy one. You are using dotenet.exe as a compilation engine and it does not accept -nullable:enable switch. Thus you will need to switch to csc.exe (it accepts this switch):

//css_ng csc
//css_co -nullable:enable

Or modify nullable project property in the dotnet project template (C:\Users\oleg.shilo\AppData\Local\Temp\csscript.core\build\build.6.0.10..csproj) manually:

  <PropertyGroup>
    <DefineConstants>TRACE;NETCORE;CS_SCRIPT</DefineConstants>
    <Nullable>enable</Nullable>
  </PropertyGroup>

After that, a warning will be generated. Though... Since it is scripting, all compiler output is suppressed if the compilation succeeded so compiler output does not interfere with the script output. Thus you cannot see the warning. The only way to see it is to pass -verbose switch and then indeed compiler output will be available, alongside with the rest of the script execution context: image

As for the version of C#, it is impossible for the script engine to know as this information is not available at runtime under CLR context. But even at compile time the script engine cannot possibly know as C# version is determined by the SDK version installed and the actual csc.exe/dotnet.exe being executed to build the script assembly.

.

oleg-shilo avatar Oct 26 '22 13:10 oleg-shilo

Turns out that I was only missing the -verbose switch

sken130 avatar Oct 27 '22 04:10 sken130

After some studying, I have found another solution: //css_co -nullable:enable -warnaserror:nullable assuming we use a compiler engine that supports it.

(I do run "C:\cs-script\netcore\cs-script.win.v4.4.7.0\css.exe" -config:set:DefaultCompilerEngine=csc before running my C# script)

The -verbose switch is not needed any more.

Reference: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/errors-warnings

sken130 avatar Oct 27 '22 04:10 sken130

Makes sense. I think it is a good example for //css_co directive. Will add it to the documentation

oleg-shilo avatar Oct 27 '22 06:10 oleg-shilo

cscs.exe -engine:csc -co:/nullable:enable -co:/warnaserror:nullable "SomeScript.cs" also works as intended.

sken130 avatar Oct 27 '22 08:10 sken130

Thanks for the update. Also, I suggest putting the same thing to this wiki: https://github.com/oleg-shilo/cs-script/wiki/Script-Syntax

sken130 avatar Nov 04 '22 03:11 sken130