8000 [PM-22380] Enable NRT for some Core project files by benbryant0 · Pull Request #5912 · bitwarden/server · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[PM-22380] Enable NRT for some Core project files #5912

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally se 8000 nd you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 6, 2025

Conversation

benbryant0
Copy link
Contributor

🎟️ Tracking

Related to this ADR, but until that's accepted I'm working in the other direction and enabling per-file instead (as discussed here).

📔 Objective

This PR enables NRT (nullable reference types) for 25 files in the Core project and does some minimal work to fix the resulting errors.

I've intentionally targeted files that don't have any ownership according to the CODEOWNERS file, but a file in a test project that had to be changed does belong to the Key Management team.

Since the scope of these changes is pretty limited, it's not guaranteed that every field/variable/whatever that can be null has been marked as such. I've only focused on fixing errors, and been guided by callers of functions & constructors when those were also in nullable contexts and started erroring.

📸 Screenshots

⏰ Reminders before review

  • Contributor guidelines followed
  • All formatters and local linters executed and passed
  • Written new unit and / or integration tests where applicable
  • Protected functional changes with optionality (feature flags)
  • Used internationalization (i18n) for all UI strings
  • CI builds passed
  • Communicated to DevOps any deployment requirements
  • Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team

🦮 Reviewer guidelines

  • 👍 (:+1:) or similar for great changes
  • 📝 (:memo:) or ℹ️ (:information_source:) for notes or general info
  • ❓ (:question:) for questions
  • 🤔 (:thinking:) or 💭 (:thought_balloon:) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion
  • 🎨 (:art:) for suggestions / improvements
  • ❌ (:x:) or ⚠️ (:warning:) for more significant problems or concerns needing attention
  • 🌱 (:seedling:) or ♻️ (:recycle:) for future improvements or indications of technical debt
  • ⛏ (:pick:) for minor or nitpick changes

Comment on lines +161 to +164
[MemberNotNull(nameof(_hubClient))]
private NotificationHubConnection Init()
{
HubClient = NotificationHubClient.CreateClientFromConnectionString(ConnectionString, HubName, EnableSendTracing);
_hubClient = NotificationHubClient.CreateClientFromConnectionString(ConnectionString, HubName, EnableSendTracing);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This avoids needing to use a null-forgiving operator in the HubClient getter. The null analysis doesn't seem smart enough to work out that assigning to HubClient would just directly assign to _hubClient, so it has to be assigned to directly here. This makes the HubClient setter unused now, so that could be removed.

@@ -16,7 +18,7 @@ public JobFactory(IServiceProvider container)
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
var scope = _container.CreateScope();
return scope.ServiceProvider.GetService(bundle.JobDetail.JobType) as IJob;
return (scope.ServiceProvider.GetService(bundle.JobDetail.JobType) as IJob)!;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

As I understand it this should only be null if a job type hasn't been registered in DI before being executed by Quartz.NET, which shouldn't happen and that'll just lead to Quartz throwing a bit after calling this anyway.

I haven't used Quartz before, but from looking into it a bit I think as of ~4 years ago this isn't necessary, as Quartz can handle all of this itself. It looks like there could be a pretty big cleanup of most of the use of Quartz, which would get rid of this, but that's a whole other thing.

Copy link
Member

Choose a reason for hiding this comment

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

This could definitely use some cleanup it just never gets prioritized since it keeps working 🤷.

@@ -142,7 +145,10 @@ public virtual async Task StartAsync(CancellationToken cancellationToken)

public virtual async Task StopAsync(CancellationToken cancellationToken)
{
await _scheduler?.Shutdown(cancellationToken);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If _scheduler is null, this just throws a nullref exception as you can't await null.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The changes here are mostly around Jobs potentially being null. It's a bit odd that there's some logic in StartAsync that only executes if Jobs is not null, but then right after that Jobs would be used assuming that it wasn't null. I just shuffled stuff around to make that not an issue.

@benbryant0 benbryant0 marked this pull request as ready for review June 3, 2025 21:03
@benbryant0 benbryant0 requested a review from a team as a code owner June 3, 2025 21:03
@bitwarden-bot
Copy link

Thank you for your contribution! We've added this to our internal Community PR board for review.
ID: PM-22380
Link: https://bitwarden.atlassian.net/browse/PM-22380

Details on our contribution process can be found here: https://contributing.bitwarden.com/contributing/pull-requests/community-pr-process.

@bitwarden-bot bitwarden-bot changed the title Initial NRT enabling for some Core project files [PM-22380] Initial NRT enabling for some Core project files Jun 4, 2025
@benbryant0 benbryant0 changed the title [PM-22380] Initial NRT enabling for some Core project files [PM-22380] Enable NRT for some Core project files Jun 4, 2025
@quexten quexten self-assigned this Jun 5, 2025
@quexten quexten self-requested a review June 5, 2025 10:34
@quexten
Copy link
Contributor
quexten commented Jun 5, 2025

@benbryant0 I synced up the branch to main, to run the CI pipeline, please watch out to pull the changes if making further changes.

Copy link
Contributor
github-actions bot commented Jun 5, 2025

Logo
Checkmarx One – Scan Summary & Details31d3bc63-04f4-4c30-8757-3c3e7e17f202

Great job, no security vulnerabilities found in this Pull Request

@github-advanced-security
Copy link

This pull request sets up GitHub code scanning for this repository. Once the scans have completed and the checks have passed, the analysis results for this pull request branch will appear on this overview. Once you merge this pull request, the 'Security' tab will show more code scanning analysis results (for example, for the default branch). Depending on your configuration and choice of analysis tool, future pull requests will be annotated with code scanning analysis results. For more information about GitHub code scanning, check out the documentation.

Copy link
codecov bot commented Jun 5, 2025

Codecov Report

Attention: Patch coverage is 25.00000% with 18 lines in your changes missing coverage. Please review.

Project coverage is 47.73%. Comparing base (2435063) to head (c9264fd).
Report is 4 commits behind head on main.

Files with missing lines Patch % Lines
src/Core/Jobs/BaseJobsHostedService.cs 0.00% 12 Missing ⚠️
...re/HostedServices/ApplicationCacheHostedService.cs 0.00% 5 Missing ⚠️
src/Core/Jobs/JobFactory.cs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5912      +/-   ##
==========================================
- Coverage   51.06%   47.73%   -3.33%     
==========================================
  Files        1665     1665              
  Lines       75008    75013       +5     
  Branches     6755     6757       +2     
==========================================
- Hits        38300    35811    -2489     
- Misses      35188    37742    +2554     
+ Partials     1520     1460      -60     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Member
@justindbaur justindbaur left a comment

Choose a reason for hiding this comment

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

Some minor nits but they are in the KM files so if they want to override me there then that is fine. Thank you so much for getting this up so quickly!

@@ -175,7 +175,7 @@ public async Task RotateUserKeyWrongData_Throws(SutProvider<AccountsKeyManagemen
}
catch (BadRequestException ex)
{
Assert.NotEmpty(ex.ModelState.Values);
Assert.NotEmpty(ex.ModelState!.Values);
Copy link
Member

Choose a reason for hiding this comment

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

In my opinion I would use an assertion that this thing is non null instead of the forgiving operator in tests.

Suggested change
Assert.NotEmpty(ex.ModelState!.Values);
Assert.NotNull(ex.ModelState);
Assert.NotEmpty(ex.ModelState.Values);

@@ -210,7 +210,7 @@ public async Task PostSetKeyConnectorKeyAsync_SetKeyConnectorKeyFails_ThrowsBadR
var badRequestException =
await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.PostSetKeyConnectorKeyAsync(data));

Assert.Equal(1, badRequestException.ModelState.ErrorCount);
Assert.Equal(1, badRequestException.ModelState!.ErrorCount);
Copy link
Member

Choose a reason for hiding this comment

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

Same here

@@ -284,7 +284,7 @@ public async Task PostConvertToKeyConnectorAsync_ConvertToKeyConnectorFails_Thro
var badRequestException =
await Assert.ThrowsAsync<BadRequestException>(() => sutProvider.Sut.PostConvertToKeyConnectorAsync());

Assert.Equal(1, badRequestException.ModelState.ErrorCount);
Assert.Equal(1, badRequestException.ModelState!.ErrorCount);
Copy link
Member

Choose a reason for hiding this comment

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

And here

@@ -16,7 +18,7 @@ public JobFactory(IServiceProvider container)
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
var scope = _container.CreateScope();
return scope.ServiceProvider.GetService(bundle.JobDetail.JobType) as IJob;
return (scope.ServiceProvider.GetService(bundle.JobDetail.JobType) as IJob)!;
Copy link
Member

Choose a reason for hiding this comment

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

This could definitely use some cleanup it just never gets prioritized since it keeps working 🤷.

Copy link
Contributor
@quexten quexten left a comment

Choose a reason for hiding this comment

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

Approving for KM. Please let me know whether you want to address the nits Justin brought up, if not I'm happy to merge without them too.

Docker build errors can be ignored, they are missing authentication due to this being a community PR.

@benbryant0
Copy link
Contributor Author
benbryant0 commented Jun 5, 2025

Thanks!

@quexten I'll leave the nits for now since you're fine with them and they're in test files with no change to existing behaviour. I do agree with them though, so I'll make a note to fix them when I next have a PR that touches KM ownership.

I just rebased on main, so should be good to go.

Copy link
sonarqubecloud bot commented Jun 6, 2025

@quexten quexten merged commit 20d3911 into bitwarden:main Jun 6, 2025
45 of 60 checks passed
@quexten
Copy link
Contributor
quexten commented Jun 6, 2025

Merged! Thank you for the contribution.

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

Successfully merging this pull request may close these issues.

5 participants
0