Skip to content

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

Draft
nkolev92 wants to merge 3 commits into
devfrom
dev-nkolev92-nullableProtocolPluginMessages
Draft

Enable nullable for NuGet.Protocol plugin messages, logging, and contracts#7434
nkolev92 wants to merge 3 commits into
devfrom
dev-nkolev92-nullableProtocolPluginMessages

Conversation

@nkolev92
Copy link
Copy Markdown
Member

@nkolev92 nkolev92 commented May 30, 2026

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
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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 <223556219+Copilot@users.noreply.github.com>
@nkolev92 nkolev92 force-pushed the dev-nkolev92-nullableProtocolPluginMessages branch from d9001ac to f3fa223 Compare June 3, 2026 00:54
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.

1 participant