Skip to content

Add analyzer and codefixer for WebHost.CreateDefaultBuilder to Host.CreateDefaultBuilder.ConfigureWebHostDefaults #63246

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 send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

BrennanConroy
Copy link
Member

No description provided.

@github-actions github-actions bot added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Aug 13, 2025
}
";

var fixedSource = @"
Copy link
Member Author

Choose a reason for hiding this comment

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

@CyrusNajmabadi wonder if you have any insight into making the formatting a bit nicer here? See fixed code example below not having 'nice' indentation like the original code above.

}

private static readonly HashSet<string> WebHostBuilderMethods = new HashSet<string>
{
Copy link
Member Author

@BrennanConroy BrennanConroy Aug 13, 2025

Choose a reason for hiding this comment

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

Hardcoded list because we don't have an easy way to look at chained methods and see if they implement the pattern IWebHostBuilder MethodName(this IWebHostBuilder, ...)

Should we include all of the methods from
https://github.com/search?q=repo%3Adotnet%2Faspnetcore%20%22this%20iwebhostbuilder%22&type=code
?

@BrennanConroy BrennanConroy requested a review from joperezr August 13, 2025 22:35

private static bool IsWebHostBuilderType(TypeSyntax typeSyntax)
{
return typeSyntax.ToString().Contains("IWebHostBuilder");
Copy link
Member

Choose a reason for hiding this comment

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

Equals instead of Contains to avoid detecting non-related types. We do a check in the analyzer for the exact return type but it might help to harden it here too.

{
// Create a placeholder for the Host.CreateDefaultBuilder().ConfigureWebHostDefaults(...) call
// This will be replaced later, but we need something to chain the remaining methods to
var placeholder = SyntaxFactory.IdentifierName("HOST_PLACEHOLDER");
Copy link
Member

Choose a reason for hiding this comment

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

This pattern feels slightly fragile (maybe less so if we null check above) but I dunno if there is a better way to do the swarp here... 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants