Skip to content

Enable nullable for NuGet.Protocol plugin messages, logging, and contracts - #7434

Merged
nkolev92 merged 4 commits into
devfrom
dev-nkolev92-nullableProtocolPluginMessages
Jun 8, 2026
Merged

nkolev92 merged 4 commits into
devfrom
dev-nkolev92-nullableProtocolPluginMessages

Conversation

@nkolev92

@nkolev92 nkolev92 commented May 30, 2026 •

Copy link
Copy Markdown
Member

Bug

Progress: NuGet/Home#14851

Description

Completes the nullable enablement of the NuGet.Protocol plugin layer: every type under NuGet.Protocol.Plugins/Messages/ and NuGet.Protocol.Plugins/Logging/, plus MessageUtilities and the surrounding plugin contracts (interfaces, exceptions, event args, constants, and small value objects), is now annotated. The plugin runtime (Connection, Sender, Receiver, MessageDispatcher, request handlers, lifecycle, discovery, package operations) is intentionally left for a follow-up PR.

Non-mechanical changes worth calling out:

  • PluginLogger.CreateStreamWriter now throws an explicit InvalidOperationException if _logDirectoryPath is missing or if Process.MainModule?.FileName is null, instead of relying on an implicit NullReferenceException.
  • The obsolete public Message(string, MessageType, MessageMethod, JObject) constructor previously had an empty body, leaving RequestId, Type, Method, and PayloadObject uninitialized. It now delegates to the internal constructor so the documented validation actually runs.
  • PluginCreationResult had five reference-type properties that are mutually exclusive depending on whether the creation succeeded. Added an internal bool IsSuccess discriminator with [MemberNotNullWhen] attributes so the compiler enforces the documented contract: when Message is non-null Plugin/PluginMulticlientUtilities/Claims are null, and vice versa.
  • LineReadEventArgs.Line and ProtocolErrorEventArgs.Message are now nullable to match the call sites that already produced null values (StandardOutputReceiver null-guards lines; the single-argument ProtocolErrorEventArgs constructor never sets Message).
  • IConnection.ProtocolVersion and IMessageDispatcher.SetConnection's parameter are now nullable, matching what the XML docs already said.
  • IRequestHandlers.TryGet is annotated with [NotNullWhen(true)] on the out parameter so callers don't need a redundant null check after a successful lookup.
  • Plugin exception constructors accept nullable message/innerException parameters to match the System.Exception base contract.

PR Checklist

  • Meaningful title, helpful description and a linked NuGet/Home issue
  • Added tests
  • Link to an issue or pull request to update docs if this PR changes settings, environment variables, new feature, etc.

@nkolev92 nkolev92 changed the title Enable nullable for NuGet.Protocol plugin messages and logging Enable nullable for NuGet.Protocol plugin messages, logging, and contracts Jun 1, 2026
@nkolev92
nkolev92 force-pushed the dev-nkolev92-nullableProtocolPluginMessages branch from 38170f0 to b7f05bb Compare June 2, 2026 21:18
nkolev92 and others added 3 commits June 2, 2026 17:52
Removes `#nullable disable` from the plugin constants, exceptions,
event args, value objects, and interfaces in `NuGet.Protocol`.

Key behavioral annotations:
- `LineReadEventArgs.Line` becomes nullable; `StandardOutputReceiver`
  already null-checks it.
- `ProtocolErrorEventArgs.Message` becomes nullable because the 1-arg
  ctor never sets it.
- `PluginDiscoveryResult.Message` becomes nullable; returns null when
  the plugin file is `Valid`.
- `PluginCreationResult.{Plugin, PluginMulticlientUtilities, Claims,
  Message, Exception}` become nullable because the three ctors set
  mutually exclusive subsets. An `internal bool IsSuccess` helper is
  added with `[MemberNotNullWhen]` attributes so consumers can have
  the compiler enforce the invariant.
- `IConnection.ProtocolVersion` becomes nullable per the doc comment.
- `IMessageDispatcher.SetConnection(IConnection?)` accepts null per
  the doc comment.
- `IRequestHandlers.TryGet` out parameter uses
  `[NotNullWhen(true)] out IRequestHandler?`.
- `PluginException`/`ProtocolException` ctors accept nullable
  `message` and `innerException` to match `System.Exception` base.

Progress: NuGet/Home#14851

Co-authored-by: Copilot <[email protected]>
@nkolev92
nkolev92 force-pushed the dev-nkolev92-nullableProtocolPluginMessages branch from d9001ac to f3fa223 Compare June 3, 2026 00:54
After plugin contracts and message DTOs were annotated, `MessageConverter`
(added in a parallel STJ-migration PR) failed to build on net472:

- `MessageConverter.Read` pulls `requestId` from `JsonElement.GetString()`
  (returns `string?`) and validates it with `string.IsNullOrEmpty`. The
  net472 BCL reference assemblies do not have `[NotNullWhen(false)]` on
  `string.IsNullOrEmpty`, so the compiler still sees `string?` when the
  value is passed to `new Message(string requestId, ...)`. Added `!`
  suppression on the constructor call; the explicit guard above already
  throws on null/empty.
- `MessageConverterTests.GetPayload<T>` returned
  `MessageUtilities.DeserializePayload<T>(message)` which is now `T?`.
  Added `!` so the helper keeps its non-null contract and callers don't
  need per-line suppressions.

Co-authored-by: Copilot <[email protected]>
@nkolev92
nkolev92 marked this pull request as ready for review June 4, 2026 22:16
@nkolev92
nkolev92 requested a review from a team as a code owner June 4, 2026 22:16

@Nigusu-Allehu Nigusu-Allehu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM bar a few questions I had

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants