Skip to content
Open
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
3 changes: 3 additions & 0 deletions src/Dotnet.Watch/Watch/Build/BuildNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ namespace Microsoft.DotNet.Watch;

internal static class PropertyNames
{
public const string Device = nameof(Device);
public const string RuntimeIdentifier = nameof(RuntimeIdentifier);
public const string IsRunnableApplication = "_IsRunnableApplication";
public const string TargetFramework = nameof(TargetFramework);
public const string NetCoreSdkVersion = nameof(NetCoreSdkVersion);
public const string TargetFrameworkIdentifier = nameof(TargetFrameworkIdentifier);
Expand Down
5 changes: 3 additions & 2 deletions src/Dotnet.Watch/Watch/Build/ProjectGraphFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ private static string GetProductTargetFramework()
/// <summary>
/// Tries to create a project graph by running the build evaluation phase on root projects.
/// </summary>
public LoadedProjectGraph? TryLoadProjectGraph(bool projectGraphRequired, string? virtualProjectTargetFramework, CancellationToken cancellationToken)
public LoadedProjectGraph? TryLoadProjectGraph(bool projectGraphRequired, string? virtualProjectTargetFramework, CancellationToken cancellationToken, ImmutableDictionary<string, string>? additionalBuildProperties = null)
{
virtualProjectTargetFramework ??= GetProductTargetFramework();

var entryPoints = rootProjects.Select(p => new ProjectGraphEntryPoint(p.ProjectGraphPath, buildProperties));
var globalProperties = additionalBuildProperties != null ? buildProperties.SetItems(additionalBuildProperties) : buildProperties;
var entryPoints = rootProjects.Select(p => new ProjectGraphEntryPoint(p.ProjectGraphPath, globalProperties));
try
{
var stopwatch = Stopwatch.StartNew();
Expand Down
20 changes: 20 additions & 0 deletions src/Dotnet.Watch/Watch/Context/DeviceId.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.DotNet.Watch;

/// <summary>
/// The selected device and the runtime identifier it requires, if any.
/// Used to evaluate, build, and launch the project. See <see cref="DeviceInfo"/> for device selection.
/// </summary>
internal readonly record struct DeviceId
{
public DeviceId(string name, string? runtimeIdentifier)
{
Name = name;
RuntimeIdentifier = string.IsNullOrEmpty(runtimeIdentifier) ? null : runtimeIdentifier;
}

public string Name { get; }
public string? RuntimeIdentifier { get; }
}
9 changes: 2 additions & 7 deletions src/Dotnet.Watch/Watch/Context/ProjectOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,11 @@ internal sealed record ProjectOptions
public string? TargetFramework { get; init; }

/// <summary>
/// Device identifier to use when launching the project.
/// Device to use when launching the project.
/// If the project supports device selection and <see cref="Device"/> is null
/// the user will be prompted for a device in interactive mode.
/// </summary>
public string? Device { get; init; }

/// <summary>
/// RuntimeIdentifier provided by the selected device, if any.
/// </summary>
public string? DeviceRuntimeIdentifier { get; init; }
public DeviceId? Device { get; init; }

/// <summary>
/// No value indicates that no launch profile should be used.
Expand Down
118 changes: 85 additions & 33 deletions src/Dotnet.Watch/Watch/HotReload/HotReloadDotNetWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,16 @@ public async Task WatchAsync(CancellationToken shutdownCancellationToken)
_context.MainProjectOptions,
frameworkSelector: _selectionPrompt != null ? _selectionPrompt.SelectTargetFrameworkAsync : null,
deviceSelector: _selectionPrompt != null ? _selectionPrompt.SelectDeviceAsync : null,
evaluatedProjectGraph: null,
iterationCancellationToken);

// Try load project graph and perform design-time build even if the build failed.
// This allows us to watch the project and source files for changes that will trigger restart.

projectGraph = rootProjectsBuildResult.ProjectGraph ?? TryLoadProjectGraph(rootProjectsBuildResult.MainProjectTargetFramework, iterationCancellationToken);
projectGraph = rootProjectsBuildResult.ProjectGraph ?? TryLoadProjectGraph(
rootProjectsBuildResult.MainProjectTargetFramework,
rootProjectsBuildResult.SelectedDevice,
iterationCancellationToken);
if (projectGraph == null)
{
continue;
Expand Down Expand Up @@ -155,7 +159,6 @@ public async Task WatchAsync(CancellationToken shutdownCancellationToken)
{
TargetFramework = rootProjectsBuildResult.MainProjectTargetFramework,
Device = rootProjectsBuildResult.SelectedDevice,
DeviceRuntimeIdentifier = rootProjectsBuildResult.SelectedDeviceRuntimeIdentifier,
};

if (projectGraph.Graph.GraphRoots.Single()?.GetCapabilities().Contains(AspireServiceFactory.AppHostProjectCapability) == true)
Expand Down Expand Up @@ -300,6 +303,7 @@ [.. updatesBuilder.ProjectsToRebuild.Select(ProjectRepresentation.FromProjectOrE
mainProjectOptions,
frameworkSelector: null,
deviceSelector: null,
evaluationResult.ProjectGraph,
iterationCancellationToken);

if (result.Success)
Expand Down Expand Up @@ -384,7 +388,7 @@ async Task<ImmutableArray<ChangedFile>> CaptureChangedFilesSnapshot(IReadOnlyLis
{
iterationCancellationToken.ThrowIfCancellationRequested();

projectGraph = TryLoadProjectGraph(targetFramework, iterationCancellationToken);
projectGraph = TryLoadProjectGraph(targetFramework, mainProjectOptions?.Device, iterationCancellationToken);
if (projectGraph != null)
{
fileWatcher.WatchFiles(projectGraph.BuildFiles);
Expand Down Expand Up @@ -947,8 +951,21 @@ static string GetPluralMessage(ChangeKind kind)
};
}

private LoadedProjectGraph? TryLoadProjectGraph(string? virtualProjectTargetFramework, CancellationToken cancellationToken)
=> _designTimeBuildGraphFactory.TryLoadProjectGraph(projectGraphRequired: true, virtualProjectTargetFramework, cancellationToken);
private LoadedProjectGraph? TryLoadProjectGraph(string? virtualProjectTargetFramework, DeviceId? device, CancellationToken cancellationToken)
{
var properties = ImmutableDictionary<string, string>.Empty;
if (device is { } selectedDevice)
{
properties = properties.Add(PropertyNames.Device, selectedDevice.Name);

if (selectedDevice.RuntimeIdentifier != null)
{
properties = properties.Add(PropertyNames.RuntimeIdentifier, selectedDevice.RuntimeIdentifier);
Comment thread
jonathanpeppers marked this conversation as resolved.
Resolved
}
}

return _designTimeBuildGraphFactory.TryLoadProjectGraph(projectGraphRequired: true, virtualProjectTargetFramework, cancellationToken, properties);
}

private ValueTask<EvaluationResult?> TryEvaluateProjectGraphAsync(LoadedProjectGraph projectGraph, string? mainProjectTargetFramework, bool restore, CancellationToken cancellationToken)
=> EvaluationResult.TryCreateAsync(
Expand All @@ -968,11 +985,10 @@ private enum BuildAction
}

// internal for testing
internal sealed class BuildProjectsResult(string? mainProjectTargetFramework, string? selectedDevice, string? selectedDeviceRuntimeIdentifier, LoadedProjectGraph? projectGraph, bool success)
internal sealed class BuildProjectsResult(string? mainProjectTargetFramework, DeviceId? selectedDevice, LoadedProjectGraph? projectGraph, bool success)
{
public string? MainProjectTargetFramework { get; } = mainProjectTargetFramework;
public string? SelectedDevice { get; } = selectedDevice;
public string? SelectedDeviceRuntimeIdentifier { get; } = selectedDeviceRuntimeIdentifier;
public DeviceId? SelectedDevice { get; } = selectedDevice;
public LoadedProjectGraph? ProjectGraph { get; } = projectGraph;
public bool Success { get; } = success;
}
Expand All @@ -984,14 +1000,14 @@ internal async Task<BuildProjectsResult> BuildProjectsAsync(
ProjectOptions? mainProjectOptions,
Func<IReadOnlyList<string>, CancellationToken, ValueTask<string>>? frameworkSelector,
Func<IReadOnlyList<DeviceInfo>, CancellationToken, ValueTask<DeviceInfo>>? deviceSelector,
LoadedProjectGraph? evaluatedProjectGraph,
CancellationToken cancellationToken)
{
Debug.Assert(projects.Any());

LoadedProjectGraph? projectGraph = null;
var targetFramework = mainProjectOptions?.TargetFramework;
var selectedDevice = mainProjectOptions?.Device;
var selectedDeviceRuntimeIdentifier = mainProjectOptions?.DeviceRuntimeIdentifier;

_context.Logger.Log(MessageDescriptor.BuildStartedNotification, projects);

Expand All @@ -1001,7 +1017,7 @@ internal async Task<BuildProjectsResult> BuildProjectsAsync(
{
var success = await BuildWithFrameworkAndDeviceSelectionAsync();
_context.Logger.Log(MessageDescriptor.BuildCompletedNotification, (projects, success));
return new BuildProjectsResult(targetFramework, selectedDevice, selectedDeviceRuntimeIdentifier, projectGraph, success);
return new BuildProjectsResult(targetFramework, selectedDevice, projectGraph, success);
}
finally
{
Expand Down Expand Up @@ -1049,16 +1065,16 @@ async ValueTask<bool> BuildWithFrameworkAndDeviceSelectionAsync()
if (mainProjectOptions == null ||
(!needsFrameworkSelection && !needsDeviceSelection))
{
return await BuildAsync(BuildAction.RestoreAndBuild, targetFramework);
return await BuildAsync(BuildAction.RestoreAndBuild, targetFramework, selectedDevice);
}

if (!await BuildAsync(BuildAction.RestoreOnly, targetFramework: null))
if (!await BuildAsync(BuildAction.RestoreOnly, targetFramework: null, selectedDevice))
{
return false;
}

// load project graph after restore so that props and targets files from packages are imported:
projectGraph = TryLoadProjectGraph(targetFramework, cancellationToken);
projectGraph = TryLoadProjectGraph(targetFramework, selectedDevice, cancellationToken);
if (projectGraph == null)
{
return false;
Expand Down Expand Up @@ -1098,33 +1114,65 @@ async ValueTask<bool> BuildWithFrameworkAndDeviceSelectionAsync()
if (projectNodeForDeviceCheck != null
&& projectNodeForDeviceCheck.ProjectInstance.Targets.ContainsKey(TargetNames.ComputeAvailableDevices))
{
var deviceInfo = await TrySelectDeviceAsync(projectGraph, rootProject, targetFramework, deviceSelector, cancellationToken);
if (deviceInfo == null)
if (await TrySelectDeviceAsync(projectGraph, rootProject, targetFramework, deviceSelector, cancellationToken) is not { } deviceInfo)
{
return false;
}

selectedDevice = deviceInfo.Id;
selectedDeviceRuntimeIdentifier = deviceInfo.RuntimeIdentifier;
_context.Logger.LogDebug("Selected device: {DeviceId}", selectedDevice);
var device = new DeviceId(deviceInfo.Id, deviceInfo.RuntimeIdentifier);
selectedDevice = device;
_context.Logger.LogDebug("Selected device: {DeviceId}", device.Name);
projectGraph = null;

// If the device provides a RuntimeIdentifier, re-restore so the assets file
// includes the RID target. This mirrors the dotnet-run behavior.
if (!string.IsNullOrEmpty(selectedDeviceRuntimeIdentifier))
if (device.RuntimeIdentifier != null &&
!await BuildAsync(BuildAction.RestoreOnly, targetFramework, device))
{
if (!await BuildAsync(BuildAction.RestoreOnly, targetFramework, deviceInfo))
{
return false;
}
return false;
}

// Reevaluate with the selected Device and RuntimeIdentifier as global properties,
// so that RID-specific outputs and imports flow to referenced projects.
projectGraph = TryLoadProjectGraph(targetFramework, device, cancellationToken);
if (projectGraph == null)
{
return false;
}
}
}

return await BuildAsync(BuildAction.BuildOnly, targetFramework, selectedDevice != null ? new DeviceInfo(selectedDevice, null, null, null, selectedDeviceRuntimeIdentifier) : null);
return await BuildAsync(BuildAction.BuildOnly, targetFramework, selectedDevice);
}

async Task<bool> BuildAsync(BuildAction action, string? targetFramework, DeviceInfo? device = null)
async Task<bool> BuildAsync(BuildAction action, string? targetFramework, DeviceId? device = null)
{
if (evaluatedProjectGraph != null && device is { } selectedDevice)
{
foreach (var project in projects)
{
var nodes = evaluatedProjectGraph.GetProjectNodes(project.ProjectGraphPath);
var node = nodes is [var singleNode] ? singleNode : evaluatedProjectGraph.TryGetProjectNode(project.ProjectGraphPath, targetFramework);
Comment thread
jonathanpeppers marked this conversation as resolved.
Resolved
if (node == null)
{
return false;
}

// The graph node inherits the app's RuntimeIdentifier, so its IsRidAgnostic value is always false.
var projectDevice = new DeviceId(
selectedDevice.Name,
node.ProjectInstance.GetBooleanPropertyValue(PropertyNames.IsRunnableApplication)
? node.ProjectInstance.GetPropertyValue(PropertyNames.RuntimeIdentifier)
: null);
Comment thread
jonathanpeppers marked this conversation as resolved.
Resolved
if (!await BuildFileOrProjectOrSolutionAsync(project.ProjectOrEntryPointFilePath, node.ProjectInstance.GetTargetFramework(), projectDevice, action, cancellationToken))
{
return false;
}
}

return true;
}

if (projects is [var singleProject])
{
return await BuildFileOrProjectOrSolutionAsync(singleProject.ProjectOrEntryPointFilePath, targetFramework, device, action, cancellationToken);
Expand All @@ -1134,11 +1182,15 @@ async Task<bool> BuildAsync(BuildAction action, string? targetFramework, DeviceI

var projectPaths = projects.Where(p => p.PhysicalPath != null).Select(p => p.PhysicalPath!).ToArray();

if (projectPaths is [var singleProjectPath])
// Solution builds do not support a global RuntimeIdentifier.
if (projectPaths.Length == 1 || !string.IsNullOrEmpty(device?.RuntimeIdentifier))
{
if (!await BuildFileOrProjectOrSolutionAsync(singleProjectPath, targetFramework, device, action, cancellationToken))
foreach (var projectPath in projectPaths)
{
return false;
if (!await BuildFileOrProjectOrSolutionAsync(projectPath, targetFramework, device, action, cancellationToken))
{
return false;
}
}
}
else if (projectPaths is not [])
Expand Down Expand Up @@ -1247,7 +1299,7 @@ async Task<bool> BuildAsync(BuildAction action, string? targetFramework, DeviceI
return await deviceSelector(devices, cancellationToken);
}

private async Task<bool> BuildFileOrProjectOrSolutionAsync(string path, string? targetFramework, DeviceInfo? device, BuildAction action, CancellationToken cancellationToken)
private async Task<bool> BuildFileOrProjectOrSolutionAsync(string path, string? targetFramework, DeviceId? device, BuildAction action, CancellationToken cancellationToken)
{
var arguments = new List<string>
{
Expand All @@ -1263,14 +1315,14 @@ private async Task<bool> BuildFileOrProjectOrSolutionAsync(string path, string?
arguments.Add(targetFramework);
}

if (device != null)
if (device is { } selectedDevice)
{
arguments.Add($"-p:Device={device.Id}");
arguments.Add($"-p:Device={selectedDevice.Name}");

if (!string.IsNullOrEmpty(device.RuntimeIdentifier))
if (selectedDevice.RuntimeIdentifier != null)
{
arguments.Add("--runtime");
arguments.Add(device.RuntimeIdentifier);
arguments.Add(selectedDevice.RuntimeIdentifier);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/Dotnet.Watch/Watch/Process/ProjectLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ private static IReadOnlyList<string> GetProcessArguments(ProjectOptions projectO
arguments.Add(projectOptions.TargetFramework);
}

if (projectOptions.Device != null)
if (projectOptions.Device is { } device)
{
arguments.Add("--device");
arguments.Add(projectOptions.Device);
arguments.Add(device.Name);

if (projectOptions.DeviceRuntimeIdentifier != null)
if (device.RuntimeIdentifier != null)
{
arguments.Add("--runtime");
arguments.Add(projectOptions.DeviceRuntimeIdentifier);
arguments.Add(device.RuntimeIdentifier);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/Dotnet.Watch/Watch/UI/DeviceInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ namespace Microsoft.DotNet.Watch;

/// <summary>
/// Represents a device item returned from the ComputeAvailableDevices MSBuild target.
/// Includes display information for the device selection prompt.
/// </summary>
internal sealed record DeviceInfo(string Id, string? Description, string? Type, string? Status, string? RuntimeIdentifier);
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ public ProjectOptions GetMainProjectOptions(ProjectRepresentation project, strin
Representation = project,
WorkingDirectory = workingDirectory,
TargetFramework = TargetFramework,
Device = Device,
Device = Device != null ? new DeviceId(Device, runtimeIdentifier: null) : null,
Command = Command.Name,
CommandArguments = CommandArguments,
LaunchEnvironmentVariables = [],
Expand Down
4 changes: 2 additions & 2 deletions src/Dotnet.Watch/dotnet-watch/Watch/BuildEvaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ public IReadOnlyList<string> GetProcessArguments(int iteration)
arguments.Add(MainProjectOptions.TargetFramework);
}

if (MainProjectOptions.Device != null)
if (MainProjectOptions.Device is { } device)
{
arguments.Add("--device");
arguments.Add(MainProjectOptions.Device);
arguments.Add(device.Name);
}

arguments.AddRange(MainProjectOptions.CommandArguments);
Expand Down
Loading
Loading