Code Monkey home page Code Monkey logo

nullable's Introduction

nullable's People

Contributors

dependabot[bot] avatar gtbuchanan avatar manuelroemer avatar mnivet avatar tyrrrz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

nullable's Issues

Support for netstd < 2.0

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?

Can't build on older targets without explicit LangVersion

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?

Incremental build is slowed by timestamps of generated source file

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".

image

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.

Nullable's XML documentation leaks into XML docs of referencing libraries

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.

Reference problem in WPF environment

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.
image

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.

Can you add package to support InAttribute for netstandard1.0 as target

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.

[Discussion] Installation Error when using NuGet's packages.config file

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.

Add support for [MemberNotNull] and [MemberNotNullWhen]

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:

  • They are available in a .NET release/are finalized and no longer prone to change.
  • It is verified that the compiler also supports them when they are declared as internal attributes (otherwise adding them to this package has no benefit).

Use #pragma warning restore instead of #pragma warning enable

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.

[Solution] Nullable attributes are not discovered in WPF projects

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.

Automatically exclude attributes from style/code analysis

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.

Internal attributes prevent build on .NET4.8

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

CS0246 build errors on .NET 6.0.300 SDK

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

Minimal Reproduction

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.

Workaround

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.