I'm Manu. You can find more information about me on...
Legal: Impressum (Site Notice)
A source code only package which allows you to use .NET's nullable attributes in older target frameworks like .NET Standard 2.0 or the "old" .NET Framework.
Home Page: https://www.nuget.org/packages/Nullable
License: MIT License
I'm Manu. You can find more information about me on...
Legal: Impressum (Site Notice)
Just by glancing at the project I can't see any indication if this will work in a project that targets, for example, .NET Standard 1.1.
Does it? Are there technical limitations related to supporting lower versions?
I'm trying to create a NuGet package which contains 100% manual NRT attributes (with no C# 8.0 syntax) so that I can multitarget netstandard2.0
and netstandard2.1
, remain in an MS-supported configuration (employer requirement), and get correct nullability warnings in projects which use C# 8.0+.
When I try to build against netstandard2.0
, I get build errors on the generated .cs files in the obj
folder because they include the #nullable
preprocessor statement. If this preprocessor statement was not present (and the attributes had their own explicit annotations, if that's possible/needed), the files would otherwise compile.
Do you think you could make this change?
Particularly when setting:
<ProduceReferenceAssembly>true</ProduceReferenceAssembly>
We expect that when A references B and B changes in a way that does not impact its public API, then A does not need to be recompiled. But when A has a package reference to Nullable, it recompiles anyway for no good reason, making incremental builds slower.
Input file "C:\git\lib.template\obj\Library.Tests\Debug\netcoreapp3.1\NuGet\8ED094343F5007F7EC35D088A29F28BB2E4E7756\Nullable\1.3.0\Nullable/MemberNotNullAttribute.cs" is newer than output file "C:\git\lib.template\obj\Library.Tests\Debug\netcoreapp3.1\Library.Tests.xml".
Can you fix this by ensuring the timestamp is not updated on this file unnecessarily?
You can test this for yourself, and verify the effectiveness of your fix, using my template repo as follows:
git clone https://github.com/AArnott/Library.Template.git
cd Library.Template
.\init[.ps1]
dotnet build
Then make a change to Calculator.cs that does not impact the public API. Perhaps this:
- public static int Add(int a, int b) => a + b;
+ public static int Add(int a, int b) => a + b + 1;
Then run:
dotnet build -bl
Open up the msbuild.binlog (in the https://msbuildlog.com viewer) and search for $task csc
to see how many times the compiler actually ran. We want to see it just once. When Nullable
is not referenced, this happens. But when it is referenced, we see 4.
Every library that references Nullable and has XML documentation finds the latter "polluted" by the documentation for Nullable's attributes.
Although I understand that Intellisense is nice to have, I'd rather push F1 when I need an explanation on some attribute than have the attributes' documentation injected into every NuGet package I produce. Not to mention automatically-generated online docs!
I propose that XML documentation be removed completely from Nullable, as the inconveniences it brings to referencing libraries outweigh its usefulness.
When using the library in a WPF environment it´s not possible to add a reference to the included types.
I found that problem with my own library and was hoping, that you solved it somehow but I can recreate the issue with your project.
I created a sample project to recreate the issue.
The branch nullable shows the problem with your library.
Its a simple console application that also includes a WPF User Control
.
using System;
using System.Diagnostics.CodeAnalysis;
namespace TheWPFClient
{
class Program
{
[AllowNull] private readonly string OptionalValue;
static void Main(string[] args)
{
Console.WriteLine("Hello beatiful World!");
}
}
}
When compiling the code that build error accours:
Program.cs(8,10,8,19): error CS0246: The type or namespace name 'AllowNullAttribute' could not be found (are you missing a using directive or an assembly reference?)
Program.cs(8,10,8,19): error CS0246: The type or namespace name 'AllowNull' could not be found (are you missing a using directive or an assembly reference?)
If you delete the usercontrol the error is gone.
When I use ref readonly
and in parameter
in package for netstandard1.0
target, compiler return error Predefined type 'System.Runtime.InteropServices.InAttribute' is not defined or imported
.
Can you add new package like Nullable to support ref readonly
and in parameter
for netstandard1.0
.
Thanks.
I think asking most people to define NULLABLE_ATTRIBUTES_EXCLUDE_FROM_CODE_COVERAGE
to exclude from code analysis for all projects is troublesome. I found Exclude code from test coverage and code analysis declaring that [DebuggerNonUserCode]
and [DebuggerHidden]
can be used to exclude from code coverage. They all support .NET Standard 1.0.
Hi, I wanted to give this package a try but during installation VS gives met this error:
Could not install package 'Nullable 1.1.0'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.7.2', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.
New nullability attributes are currently being introduced (see https://github.com/dotnet/runtime/blob/master/src/libraries/System.Private.CoreLib/src/System/Diagnostics/CodeAnalysis/NullableAttributes.cs).
These should be added to Nullable as soon as:
internal
attributes (otherwise adding them to this package has no benefit).While this is most likely not going to cause problems, it is good practice to use #pragma warning restore
instead of #pragma warning enable
. In the nullable files, enable
is used at the moment:
This was an oversight and should be changed.
If there are no complaints/problems due to this in the meantime, I will delay the fix until the next release of the package.
I've added the project to my solution, but I can't use the attributes since they are marked as internal. When I change them to public it works brilliantly.
I have a relatively complex solution with lots of legacy stuff, but I did manage to modernize some of the assemblies to the Sdk style and PackageReference
instead of packages.config
. I can add your NuGet package and then write code like this, in .NET Framework 4.7.2:
public bool TryGetExistingFavorite(ContactRecord record, [NotNullWhen(true)] out Favorite? favorite)
{
favorite = _Favorites.FirstOrDefault(f => f.CID == record.ID);
return favorite != null;
}
This looks great during design-time builds. But when I actually do a full build, I get an error that the attributes can't be found:
1>…path…\Favorites\FavoritesRepository.cs(108,68,108,79): error CS0246: The type or namespace name 'NotNullWhenAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>…path…\Favorites\FavoritesRepository.cs(108,68,108,79): error CS0246: The type or namespace name 'NotNullWhen' could not be found (are you missing a using directive or an assembly reference?)
1>Done building project "MyProject_hh54oelh_wpftmp.csproj" -- FAILED.
I've tried both a regular reference like so:
<PackageReference Include="Nullable" Version="1.2.1" />
And the adjusted one from the README:
<PackageReference Include="Nullable" Version="1.2.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
I think the …**wpftmp**.csproj
portion may be a clue. However, with a test project that also contains WPF, I wasn't able to reproduce the issue.
When installing this package, its item is added to the compilation. If a style rule does not match with the code style in the code file, an analysis error might stop compilation.
To repro, add this to a project's .editorconfig:
# IDE0040: Add accessibility modifiers
dotnet_diagnostic.IDE0040.severity = error
(this setting will cause a build error if an accessibility modifier is added which is already the default for it, like adding internal
to a class).
After installing the package, now you'll get the expected build error:
Severity Code Description Project File Line Suppression State
Error IDE0040 Accessibility modifiers required [ProjectName] [ProjectPath]\obj\3.8.0\Debug\NuGet\3D28A4393902596C02B027108469F4ED40AA07C9\Nullable\1.0.0\Nullable\NullableAttributes.cs 46 Active
I have learned that this can be avoided by simply adding this to the code files somewhere (near the top?): // <auto-generated/>
That eliminates the (spurious) error shown above.
I'm trying to decorate a method parameter with [AllowNull]. Inside the IDE, visual studio recognizes it and works fine, but when I go to compile the project I can not - it says the attribute is internal not public
After installing the .NET 6.0.300 SDK, doing dotnet build --no-incremental
on a project using this library gives errors such as:
error CS0246: The type or namespace name 'NotNullWhen' could not be found
MyApp.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<LangVersion>8</LangVersion>
<Nullable>annotations</Nullable>
<TargetFrameworks>net6.0;net48</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Nullable" Version="1.3.0" PrivateAssets="all" />
</ItemGroup>
</Project>
Program.cs
using System;
using System.Diagnostics.CodeAnalysis;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
if (Test(out var value))
{
Console.WriteLine(value);
}
}
private static bool Test([NotNullWhen(true)] out string? value)
{
value = "Hello World!";
return true;
}
}
}
Compile with dotnet build --no-incremental
:
C:\Users\mattj\Code\MyApp>dotnet build --no-incremental
Microsoft (R) Build Engine version 17.2.0+41abc5629 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
C:\Users\mattj\Code\MyApp\Program.cs(16,35): error CS0246: The type or namespace name 'NotNullWhenAttribute' could not be found (are you m
issing a using directive or an assembly reference?) [C:\Users\mattj\Code\MyApp\MyApp.csproj]
C:\Users\mattj\Code\MyApp\Program.cs(16,35): error CS0246: The type or namespace name 'NotNullWhen' could not be found (are you missing a
using directive or an assembly reference?) [C:\Users\mattj\Code\MyApp\MyApp.csproj]
MyApp -> C:\Users\mattj\Code\MyApp\bin\Debug\net6.0\MyApp.dll
Build FAILED.
Install the previous 6.0.203 SDK, and force the project to use it by adding a global.json
file with the following:
{
"sdk": {
"version": "6.0.203"
}
}
Then the project builds just fine with dotnet build --no-incremental
.
This might be an upstream problem with .NET, but I haven't dug deep enough to figure out what it might be.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.