Skip to content

Conversation

@danielmarbach
Copy link
Contributor

@danielmarbach danielmarbach commented Nov 14, 2025

This pull request introduces a new Roslyn analyzer and code fix to enforce best practices around enabling features. It detects and corrects the use of EnableFeature<TFeature>() within the Defaults method of a feature, prompting developers to use the protected Enable<TFeature>() method in the constructor instead.

Analyzer and code fix for feature enablement:

  • Added FeatureDefaultsEnableFeatureAnalyzer to report diagnostics when EnableFeature<TFeature>() is called inside a Defaults lambda, enforcing that dependent features should be enabled using Enable<TFeature>() in the constructor instead.
  • Introduced FeatureDefaultsEnableFeatureFixer to automatically move EnableFeature<TFeature>() calls from within Defaults lambdas to the feature constructor, optionally removing or updating the Defaults block as needed.
  • Registered a new diagnostic ID NSB0019 for this rule in DiagnosticIds.
  • Set the C# language version to 9 in the code fix project to support modern language features.

Scoped out:

  • Local functions or static methods used in defaults

@danielmarbach danielmarbach changed the title Error on Feature Defaults enabling a feature Analyzer: Error when Feature Defaults are used to enable other features Nov 14, 2025

This comment was marked as resolved.

@danielmarbach danielmarbach marked this pull request as ready for review November 14, 2025 21:45
Copy link
Member

@DavidBoike DavidBoike left a comment

Choose a reason for hiding this comment

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

Initial review done on an iPad on an airplane, no compiler support. 😉

@danielmarbach

This comment was marked as resolved.

This comment was marked as resolved.

* Initial plan

* Optimize analyzer performance and modernize test code blocks

Co-authored-by: danielmarbach <174258+danielmarbach@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: danielmarbach <174258+danielmarbach@users.noreply.github.com>
@danielmarbach

This comment was marked as resolved.

Copy link
Contributor

Copilot AI commented Nov 16, 2025

@danielmarbach I've opened a new pull request, #7476, to work on those changes. Once the pull request is ready, I'll request review from you.

Copy link
Member

@andreasohlund andreasohlund left a comment

Choose a reason for hiding this comment

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

LGTM (based on very limited analyzer knowledge so I would wait for Davids approval)

Copy link
Member

@DavidBoike DavidBoike left a comment

Choose a reason for hiding this comment

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

LGTM, I can't think of any other tests to add, let's go with it but be sure to smoke test it with the next alpha.

@danielmarbach danielmarbach merged commit c120b15 into master Nov 19, 2025
4 checks passed
@danielmarbach danielmarbach deleted the defaults-fixer branch November 19, 2025 07:05
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.

4 participants