-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
Originally submitted on dotnet/runtime#118052
Problem Statement
Currently, the .NET hot reload mechanism does not consistently trigger rebuilds when non-C# source files are modified, even when these files are critical inputs for source generators. This limitation particularly affects projects using source generators that depend on external files (e.g., GraphQL schemas, JSON configurations, protocol buffers) to generate C# code.
Current Behavior
When using dotnet watch
on a project that includes source-generated content from non-C# files:
- Changes to
.graphql
files do not trigger the Strawberry Shake source generator - The hot reload process ignores these file changes, resulting in outdated generated code
- Developers must manually restart the application to see changes reflected
Expected Behavior
The hot reload mechanism should:
- Detect changes to watched non-C# source files
- Trigger a full rebuild (rude edit) when these files are modified
- Re-execute relevant source generators to produce updated assemblies
- Properly cascade rebuilds through dependent projects
Use Case Example
In a typical GraphQL client setup using Strawberry Shake:
- Server Project: ASP.NET Core application
- GraphQL.Client Project: Contains
.graphql
query/mutation files - Source Generator: Strawberry Shake generates C# client code from
.graphql
files
When modifying .graphql
files during development, the source generator should automatically regenerate the client code and trigger a hot reload.
Proposed Solution
Introduce a ForceRebuild
attribute for <Watch>
items in MSBuild project files:
<ItemGroup>
<Watch Include="**\*.graphql" ForceRebuild="true" />
</ItemGroup>
This attribute would indicate that changes to these files require a full rebuild rather than attempting incremental compilation.
Additional Requirements
The <Watch>
mechanism should properly propagate through project references. Currently, parent projects must explicitly watch files in referenced projects:
<!-- Current workaround in parent project -->
<ItemGroup>
<Watch Include="..\**\*.graphql" Exclude="..\**\schema.graphql" />
</ItemGroup>
Ideally, if a referenced project defines watched files with ForceRebuild="true"
, the parent project should automatically respect these settings without requiring duplicate configuration.
Technical Considerations
- Initial implementation could treat these as rude edits, forcing application restart
- Future optimizations could explore more granular source generator invocation and assembly diffing
- The solution should be extensible to support other file types beyond GraphQL (e.g.,
.proto
,.json
,.yaml
)
Related Issues
Impact
This enhancement would significantly improve the developer experience for projects using source generators, reducing friction in the development workflow and ensuring consistency between source files and generated code during hot reload sessions.