Should JetBrains.Annotations be declared as a private asset in my csproj files?

Viewed 813

I've recently installed ReSharper and I'm refactoring a number of my C# solutions. As part of this process, I've added references to JetBrains.Annotations in my projects. However, I'm not sure whether I should be marking this dependency as private (to prevent it being passed on as a dependency to higher-level projects) or whether doing this will potentially break things.

In other words, I'm not sure whether I should be doing this:

<ItemGroup>
  <PackageReference Include="JetBrains.Annotations" Version="11.0.0" PrivateAssets="All" />
</ItemGroup>

Or simply this:

<ItemGroup>
  <PackageReference Include="JetBrains.Annotations" Version="11.0.0" />
</ItemGroup>

From the NuGet package documentation:

All usages of ReSharper Annotations attributes are erased from metadata by default, which means no actual binary reference to 'JetBrains.Annotations.dll' assembly is produced.

If I'm understanding this correctly, it suggests that marking the package as private should be safe. However, I just want to confirm.

1 Answers

If I'm understanding this correctly, it suggests that marking the package as private should be safe. However, I just want to confirm.

That's correct. There's two things in play here; metadata references (in the assembly) and NuGet package references.

Because the JetBrains annotations are conditionally compiled (using [Conditional("JETBRAINS_ANNOTATIONS")]), as long as you don't define JETBRAINS_ANNOTATIONS, all traces of the annotations will be removed from the assembly itself. This also means that it's safe to remove the NuGet package reference.

However, if you do define JETBRAINS_ANNOTATIONS, you should also include the NuGet package reference.

See the docs for more information...

Related