5

I lean quite heavily on Resharper context actions to generate boilerplate code. The Check parameter for null context action had, up until recently, generated code that performs the null check and throws accordingly. However, now it is adding a Resharper specific [NotNull] annotation to the parameter as part of the method signature.

Looking at this related question Stop ReSharper from Adding Annotations, I've checked across my solution codebase for jetbrains.annotations.dll references but not found any.

However, searching across all files I've found references to jetbrains.annotations.dll in the xml documentation file for NodaTime (NodaTime.xml).

Specifically, entries like this: <member name="T:JetBrains.Annotations.NotNullAttribute">.

Im not categorically stating that this is the root cause. However, NodaTime is the most recent nuget package I have added since the issue began, and a grep across the solution shows this to be the single point of reference.

Should xml documentation files be including these references, and if so, is there a way for me to configure Resharper to ignore them?

UPDATE I've decompiled the NodaTime assembly and although it doesn't have any binary references to jetbrains.annotations.dll it does appear to (re)declare the offending JetBrains.Annotations namespaces and annotations within itself.

As per citizenmatts suggestion, Ive used the Resharper take me to definition tool for the [NotNull] attribute and this goes to the NodaTime.dll

I've also just noticed that the NotNull attributes Resharper is including do not compile and complain of Cannot reference internal class NotNullAttribute. Hence I obviously don't have the jetbrains.annotations.dll included anyway.

UPDATE 2 Just done the obvious thing of creating a new blank project in VS2013 and checking the null check context action in Resharper works. Then added NodaTime nuget package and rechecked - and indeed it adds the [NotNull] attribute and the project wont compile.

UPDATE 3 As suggested by Jon Skeet I've tried "turning off" data annotations using Resharper-->Code Inspection-->Code Annotations-->Unticking JetBrains.Annotations. However, this has no affect and the NotNull attribute continues to be included.

Looks like I may just have to return to System.DateTime.

Community
  • 1
  • 1
Matt Caton
  • 3,473
  • 23
  • 25

2 Answers2

7

Noda Time deliberately ships with an internal copy of the R# annotations, so that those who do want them can benefit from them - it will know which methods are pure, etc.

It sounds like you don't actually want to use annotations at all, so it's just worth disabling them. That's easy from the R# options - under Code Inspection / Code Annotations, uncheck the JetBrains.Annotations option.

We can certainly consider moving the annotations into a specific namespace for Noda Time in the future, so that it's more opt-in than opt-out, but this is probably a good solution if you don't want to use annotations, as it makes it absolutely clear to R# that you really don't want to use them, even if other libraries include annotations.

EDIT: Just to promote Matt's comment into this answer:

Sadly, this is a bug in ReSharper - it doesn't take visibility of the NotNullAttribute into account before applying it. Here's the ticket to track and vote on: http://youtrack.jetbrains.com/issue/RSRP-413425

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Unchecking this option has no affect and the context action continues to add the [NotNull] attribute. Which is very odd as Im not sure what affect this option actually has! Re shipping to allow those that want them to benefit - using the annotations is just a matter of including the jetbrains.annotations.dll in the project that references NodaTime. Not sure why NodaTime would need to include them for this to work? – Matt Caton Jul 19 '14 at 07:41
  • @LongboatHarry: I don't want to add an extra dependency to Noda Time for this. I'm amazed that unchecking the option wouldn't help - I'll have to experiment with it. Thanks for the detail of the context action though - I've been trying to reproduce this from another issue report, but that didn't have details. Will try to look over the weekend. – Jon Skeet Jul 19 '14 at 08:13
  • Sorry I meant I could add the jetbrains.annotations.dll to *my* project - didn't mean you needed to add it to NodaTime – Matt Caton Jul 19 '14 at 08:28
  • @LongboatHarry: But if Noda Time refers to the annotations as well, it could cause problems if it's not present at execution time. – Jon Skeet Jul 19 '14 at 08:49
  • @LongboatHarry: I've now reproduced the issue, and failed to turn that part off. Hmm. Think we might need some input from JetBrains. I'll see who I can find... – Jon Skeet Jul 19 '14 at 08:55
  • @LongboatHarry: Have now raised https://code.google.com/p/noda-time/issues/detail?id=300 and mailed folks at JetBrains. – Jon Skeet Jul 19 '14 at 09:12
  • Many thanks Jon. In the meantime I've refactored all our NodaTime usage to a single assembly. Resharper only seems to exhibit this behaviour for projects referencing NodaTime - it doesn't affect the other projects in the solution. – Matt Caton Jul 19 '14 at 10:56
  • 3
    Sadly, this is a bug in ReSharper - it doesn't take visibility of the `NotNullAttribute` into account before applying it. Here's the ticket to track and vote on: http://youtrack.jetbrains.com/issue/RSRP-413425 – citizenmatt Jul 23 '14 at 09:19
4

NodaTime.dll ships with the JetBrains annotations compiled in as internal symbols, which means they're used internally to NodaTime and not visible for anyone else to use. However, ReSharper can see these annotations that have been applied to the NodaTime classes and methods.

NodaTime doesn't have any binary references to JetBrains.Annotations.dll, and neither ReSharper nor Visual Studio should be adding a reference to a dll based on xml documentation. If ReSharper is able to add a [NotNull] annotation to your code, the dll must be being referenced from somewhere. I'd suggest invoking ReSharper's "go to definition" on the [NotNull] and see where it takes you - it should show you where the annotations dll is being referenced from, and you should be able to remove it easily from there.

Update: As noted above, and in the other answers, the issue is a bug in ReSharper that doesn't check the accessibility of the attribute before applying it. Here's the YouTrack ticket to track and vote on: http://youtrack.jetbrains.com/issue/RSRP-413425

citizenmatt
  • 18,085
  • 5
  • 55
  • 60
  • Go to definition takes me to... NodaTime.dll ;) – Matt Caton Jul 17 '14 at 15:20
  • Wasn't expecting that! The definitions in NodaTime.dll are internal, so it shouldn't compile. Does your code compile with the `[NotNull]` added? – citizenmatt Jul 18 '14 at 08:16
  • 1
    Thanks for the continued interest in helping me out. Nope - with the NotNull's added there are red squiggles under them and `Cannot reference internal class NotNullAttribute` errors on compilation. Loving NodaTime - but for whatever reason its left Resharper unusable for our team (as not everyone is using it) so has been removed for now. With it removed, Resharper is behaving properly again. – Matt Caton Jul 18 '14 at 08:35