Skip to content

Commit

Permalink
Workaround for MSbuildProjectLoader.LoadProjectInfoAsync throwing on …
Browse files Browse the repository at this point in the history
…unrecognized project language (#44928)
  • Loading branch information
tmat authored Dec 12, 2024
1 parent c5a9b26 commit 20d85a1
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void SetVariable(string name, string value)

public void ConfigureProcess(ProcessSpec processSpec)
{
processSpec.Arguments = [.. GetCommandLineDirectives(), .. processSpec.Arguments];
processSpec.Arguments = [.. GetCommandLineDirectives(), .. processSpec.Arguments ?? []];
AddToEnvironment(processSpec.EnvironmentVariables);
}

Expand Down
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
11 changes: 11 additions & 0 deletions src/BuiltInTools/dotnet-watch/HotReloadDotNetWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public override async Task WatchAsync(CancellationToken shutdownCancellationToke

var rootProjectOptions = Context.RootProjectOptions;
var rootProjectNode = Context.ProjectGraph.GraphRoots.Single();
var rootProjectCapabilities = rootProjectNode.GetCapabilities();

await using var runtimeProcessLauncher = _runtimeProcessLauncherFactory?.TryCreate(rootProjectNode, projectLauncher, rootProjectOptions.BuildProperties);
if (runtimeProcessLauncher != null)
Expand Down Expand Up @@ -211,6 +212,16 @@ public override async Task WatchAsync(CancellationToken shutdownCancellationToke
await compilationHandler.Workspace.UpdateFileContentAsync(changedFiles, iterationCancellationToken);
}

if (!rootProjectCapabilities.Contains("SupportsHotReload"))
{
Context.Reporter.Warn($"Project '{rootProjectNode.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 @@ -68,6 +68,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

0 comments on commit 20d85a1

Please sign in to comment.