Skip to content

Enable nullable reference types in DacpacTool - #1046

Merged
jmezach merged 3 commits into
rr-wfm:masterfrom
menes97:chore/nullable-dacpactool
Nov 24, 2026
Merged

jmezach merged 3 commits into
rr-wfm:masterfrom
menes97:chore/nullable-dacpactool

Conversation

@menes97

@menes97 menes97 commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Fixes #1030

DacpacTool used <Nullable>warnings</Nullable>. This switches it to enable and fixes the 66 warnings that gives, by annotating what can actually be null rather than suppressing:

  • Command line options: options that DotMake.CommandLine leaves unset when they aren't passed are nullable (Version, Output, Reference, PreDeploy, TargetUser, ...). Options marked Required = true (--name, --input, --targetServerName, --targetDatabaseName) keep non-nullable types with = null!, since the parser always sets them.
  • PackageBuilder.Model / Metadata are nullable until UsingVersion / SetMetadata (and Model again after Dispose). EnsureModelCreated, EnsureMetadataCreated and ValidateModel carry [MemberNotNull], so code after them needs no !.
  • Parameters already checked for null are annotated: externalParts, the pre/post scripts of AddOutputArtifacts and AddScript, suppressionList, rulesExpression, the password of UseSqlAuthentication (prompted when empty), the source of the batch Format extensions, the version of SetMetadata.
  • Return values that can be null: GetPreDeploymentScript / GetPostDeploymentScript / GetPrePlanScript, IConsole.ReadLine, SetDeployProperty, and the reflection lookups.

Behaviour stays the same, with two small exceptions in paths that shouldn't happen:

  • BuildDacpac calls ArgumentNullException.ThrowIfNull(options.Output) before SaveToDisk, which did exactly that as its first statement.
  • When CreateDacModelError returns no DacModelError, GetModelValidationErrors throws an InvalidOperationException with a message instead of the constructor's ArgumentNullException.
  • The deploy property log line formats the value with an interpolated string, so a value that parses to null logs empty instead of throwing a NullReferenceException.

The tests that read Model, Metadata and the scripts (PackageBuilderTests, ExtensionsTest, ReferencedDeploymentScriptTests) use ! there. They build without warnings both on master and with #1044 merged in. I checked both combinations locally, and they merge without conflicts.

Checks

  • dotnet build of the solution: no CS warnings. The remaining warnings are the existing EnableGenerateDocumentationFile and NuGet audit ones.
  • DacpacTool.Tests (net8.0, net9.0, net10.0): 239 of 240 pass. AddInputFile_FileExists_WithInvalidSyntax fails on master as well on my machine, because it compares a parser message under a Turkish locale.

The second bullet of the issue suggests a repository-wide <Nullable>enable</Nullable> in Directory.Build.props. I left that out to keep this change to DacpacTool. It can follow once the test projects are all enabled.

🤖 Generated with Claude Code

DacpacTool used <Nullable>warnings</Nullable>. It now uses enable, and
the 66 warnings this gives are fixed by annotating what can be null:

- options that DotMake.CommandLine leaves unset are nullable; the
  required ones (--name, --input, --targetServerName,
  --targetDatabaseName) keep non-nullable types;
- PackageBuilder.Model and Metadata are nullable until UsingVersion and
  SetMetadata, with MemberNotNull on the Ensure* checks and
  ValidateModel;
- reflection results, the Get*Script extensions and optional parameters
  that are already checked for null are annotated.

Program checks --output with ThrowIfNull before saving, which
SaveToDisk did first anyway. Tests that read Model, Metadata and the
scripts use ! so they build without warnings whether or not their
project enables nullable.

Fixes rr-wfm#1030

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ErikEJ

ErikEJ commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

@menes97 You are on fire - thanks so much for these targeted PRs

Comment thread src/DacpacTool/BuildOptions.cs Outdated
{
[CliOption(Description = "Name of the package", Required = true, Name = "--name", Alias = "-n")]
public string Name { get; set; }
public string Name { get; set; } = null!;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could it be "required" ??

Comment thread src/DacpacTool/DeployOptions.cs Outdated
{
[CliOption(Description = "Path to the .dacpac package to deploy", Required = true, Name = "--input", Alias = "-i")]
public FileInfo Input { get; set; }
public FileInfo Input { get; set; } = null!;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could it be the "required" keword iso null! ?

@ErikEJ ErikEJ left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM

Comment thread src/DacpacTool/BuildOptions.cs Outdated

[CliOption(Description = "Reference(s) to include", Required = false, Name = "--reference", Alias = "-r")]
public string[] Reference { get; set; }
public string[]? Reference { get; set; }

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.

Perhaps we should initialize these array arguments to empty arrays rather than making them nullable? Then wherever we're reading them we don't have to check for null

@ErikEJ

ErikEJ commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

@menes97 could you merge this with the latest master

…cpactool

# Conflicts:
#	src/DacpacTool/VersionChecker.cs
@jmezach

jmezach commented Nov 24, 2026

Copy link
Copy Markdown
Member

@menes97 Can you have a look at the open comments?

Required options use the `required` modifier instead of `= null!`, and the
array options start as empty arrays, so the null checks where they are read
are gone. SqlCmdVariables are still only added when some are given.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@menes97

menes97 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks! Done in 5751228: the required options use required now, and the array options start as empty arrays, so the null checks are gone. I kept a Length > 0 check before AddSqlCmdVariables, so a build without variables doesn't add an empty SqlCmdVariables entry to the model.

@jmezach
jmezach merged commit 9f3090a into rr-wfm:master Nov 24, 2026
12 checks passed
@jmezach

jmezach commented Nov 24, 2026

Copy link
Copy Markdown
Member

@menes97 Thank you for your contribution

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.

Enable nullable reference types consistently in production code

3 participants