Skip to content
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

EnC: detect changes in code that only runs once and need to force restart #77949

Open
1 task
tmat opened this issue Apr 1, 2025 · 1 comment
Open
1 task
Assignees

Comments

@tmat
Copy link
Member

tmat commented Apr 1, 2025

Changes in code that only runs once are currently applied during Hot Reload but they never take effect.
Instead of applying such changes Roslyn Hot Reload service should report warning that the change had likely no effect.

The host (IDE/dotnet-watch) may decide to force restart the app when it observes this warning.

It's a non-goal to detect whether or not changes in arbitrary changed code will have effect when applied. That'd be equivalent to solving the Halting problem. However, we can identify scenarios where it's obvious and provide a configuration for application frameworks to participate in the decision.

When to report the warning

Hot Reload will issue a rude edit if the statement or expression being changed is directly

  • in a top-level code or in Main method,
  • in a static constructor body,
  • in a static member initializer

unless an active statement is present in the modified method body (i.e. the change is modifying an active method frame during EnC).

Changes to bodies of local functions and lambdas defined in top-level code are not considered to be directly in the top-level code and thus, by default, won't require restart.

Hot Reload will recognize the following attribute that allows customization of the above behavior:

namespace System.Runtime.CompilerServices;

[AttributeUsage(Method | Parameter)]
public class RestartRequiredOnMetadataUpdateAttribute : Attribute;

If this attribute is applied on a method or a local function any change made directly to its body will require restart. Changes to bodies of local functions and lambdas defined within the method/function won't require restart.

If this attribute is applied on a parameter such parameter must be of Delegate type. If a lambda expression is passed directly as an argument to such parameter any change to the body of the lambda will require restart, unless the lambda body itself or any encompassing lambda/function/member body contains an active statement.

Consider:

  • Adding bool parameter that allows to apply the attribute on Main method and static constructor and change the default behavior

Related: CreateNewOnMetadataUpdateAttribute

Host action

How does the host decide whether or not to restart in presence of the warning?

Scenario: User renames a method that's called from top-level code. They don't care about the "effect" the rename has in top-level code (likely none). It'd be unnecessary to restart the app.

Scenario: User changes app configuration in top-level code. The host should restart, otherwise the app will still use the old configuration.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Interactive untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 1, 2025
@tmat tmat changed the title EnC: detect changes in top-level code that does not run after applied EnC: detect changes in code that only runs once and need to force restart Apr 1, 2025
@tmat
Copy link
Member Author

tmat commented Apr 2, 2025

@davidwengier fyi

@deepakrathore33 deepakrathore33 removed the untriaged Issues and PRs which have not yet been triaged by a lead label Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants