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

Workaround for MSBuildProjectLoader.LoadProjectInfoAsync throwing on unrecognized project language #44927

Merged
merged 3 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,17 @@ public async Task UpdateProjectConeAsync(string rootProjectPath, CancellationTok

var loader = new MSBuildProjectLoader(this);
var projectMap = ProjectMap.Create();
var projectInfos = await loader.LoadProjectInfoAsync(rootProjectPath, projectMap, progress: null, msbuildLogger: null, cancellationToken).ConfigureAwait(false);

ImmutableArray<ProjectInfo> projectInfos;
try
{
projectInfos = await loader.LoadProjectInfoAsync(rootProjectPath, projectMap, progress: null, msbuildLogger: null, cancellationToken).ConfigureAwait(false);
}
catch (InvalidOperationException)
{
// TODO: workaround for https://github.com/dotnet/roslyn/issues/75956
projectInfos = [];
}

var oldProjectIdsByPath = oldSolution.Projects.ToDictionary(keySelector: static p => p.FilePath!, elementSelector: static p => p.Id);

Expand Down
15 changes: 13 additions & 2 deletions src/BuiltInTools/dotnet-watch/HotReloadDotNetWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,13 @@ public override async Task WatchAsync(CancellationToken shutdownCancellationToke
// use normalized MSBuild path so that we can index into the ProjectGraph
rootProjectOptions = rootProjectOptions with { ProjectPath = rootProject.ProjectInstance.FullPath };

if (rootProject.GetCapabilities().Contains(AspireServiceFactory.AppHostProjectCapability))
var rootProjectCapabilities = rootProject.GetCapabilities();
if (rootProjectCapabilities.Contains(AspireServiceFactory.AppHostProjectCapability))
{
runtimeProcessLauncherFactory ??= AspireServiceFactory.Instance;
Context.Reporter.Verbose("Using Aspire process launcher.");
}

await using var browserConnector = new BrowserConnector(Context);
var projectMap = new ProjectNodeMap(evaluationResult.ProjectGraph, Context.Reporter);
compilationHandler = new CompilationHandler(Context.Reporter);
Expand Down Expand Up @@ -236,6 +237,16 @@ void FileChangedCallback(string path, ChangeKind kind)
continue;
}

if (!rootProjectCapabilities.Contains("SupportsHotReload"))
{
Context.Reporter.Warn($"Project '{rootProject.GetDisplayName()}' does not support Hot Reload and must be rebuilt.");

// file change already detected
waitForFileChangeBeforeRestarting = false;
iterationCancellationSource.Cancel();
break;
}

HotReloadEventSource.Log.HotReloadStart(HotReloadEventSource.StartType.Main);
var stopwatch = Stopwatch.StartNew();

Expand Down
54 changes: 54 additions & 0 deletions test/dotnet-watch.Tests/HotReload/ApplyDeltaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,60 @@ public static void Print()
await App.AssertOutputLineStartsWith("Changed!");
}

[Fact]
public async Task ChangeFileInFSharpProject()
{
var testAsset = TestAssets.CopyTestAsset("FSharpTestAppSimple")
.WithSource();

App.Start(testAsset, []);

await App.AssertOutputLineStartsWith(MessageDescriptor.WaitingForFileChangeBeforeRestarting);

UpdateSourceFile(Path.Combine(testAsset.Path, "Program.fs"), content => content.Replace("Hello World!", "<Updated>"));

await App.AssertOutputLineStartsWith("<Updated>");
}

[Fact]
public async Task ChangeFileInFSharpProjectWithLoop()
{
var testAsset = TestAssets.CopyTestAsset("FSharpTestAppSimple")
.WithSource();

var source = """
module ConsoleApplication.Program

open System
open System.Threading

[<EntryPoint>]
let main argv =
while true do
printfn "Waiting"
Thread.Sleep(200)
0
""";

var sourcePath = Path.Combine(testAsset.Path, "Program.fs");

File.WriteAllText(sourcePath, source);

App.Start(testAsset, []);

await App.AssertOutputLineStartsWith(MessageDescriptor.WaitingForChanges);

UpdateSourceFile(sourcePath, content => content.Replace("Waiting", "<Updated>"));

await App.AssertOutputLineStartsWith(MessageDescriptor.WaitingForChanges, failure: _ => false);
await App.AssertOutputLineStartsWith("<Updated>");

UpdateSourceFile(sourcePath, content => content.Replace("<Updated>", "<Updated2>"));

await App.AssertOutputLineStartsWith(MessageDescriptor.WaitingForChanges, failure: _ => false);
await App.AssertOutputLineStartsWith("<Updated2>");
}

// Test is timing out on .NET Framework: https://github.com/dotnet/sdk/issues/41669
[CoreMSBuildOnlyFact]
public async Task HandleTypeLoadFailure()
Expand Down
Loading