|
19 | 19 | using Microsoft.DotNet.Cli.Extensions; |
20 | 20 | using Microsoft.DotNet.Cli.Utils; |
21 | 21 | using Microsoft.DotNet.Cli.Utils.Extensions; |
| 22 | +using Microsoft.DotNet.FileBasedPrograms; |
22 | 23 |
|
23 | 24 | namespace Microsoft.DotNet.Cli.Commands.Run; |
24 | 25 |
|
@@ -56,8 +57,11 @@ public class RunCommand |
56 | 57 |
|
57 | 58 | /// <summary> |
58 | 59 | /// Parsed structure representing the MSBuild arguments that will be used to build the project. |
| 60 | + /// |
| 61 | + /// Note: This property has a private setter and is mutated within the class when framework selection modifies it. |
| 62 | + /// This mutability is necessary to allow the command to update MSBuild arguments after construction based on framework selection. |
59 | 63 | /// </summary> |
60 | | - public MSBuildArgs MSBuildArgs { get; } |
| 64 | + public MSBuildArgs MSBuildArgs { get; private set; } |
61 | 65 | public bool Interactive { get; } |
62 | 66 |
|
63 | 67 | /// <summary> |
@@ -124,6 +128,18 @@ public int Execute() |
124 | 128 | return 1; |
125 | 129 | } |
126 | 130 |
|
| 131 | + // Pre-run evaluation: Handle target framework selection for multi-targeted projects |
| 132 | + if (ProjectFileFullPath is not null && !TrySelectTargetFrameworkIfNeeded()) |
| 133 | + { |
| 134 | + return 1; |
| 135 | + } |
| 136 | + |
| 137 | + // For file-based projects, check for multi-targeting before building |
| 138 | + if (EntryPointFileFullPath is not null && !TrySelectTargetFrameworkForFileBasedProject()) |
| 139 | + { |
| 140 | + return 1; |
| 141 | + } |
| 142 | + |
127 | 143 | Func<ProjectCollection, ProjectInstance>? projectFactory = null; |
128 | 144 | RunProperties? cachedRunProperties = null; |
129 | 145 | VirtualProjectBuildingCommand? virtualCommand = null; |
@@ -182,6 +198,100 @@ public int Execute() |
182 | 198 | } |
183 | 199 | } |
184 | 200 |
|
| 201 | + /// <summary> |
| 202 | + /// Checks if target framework selection is needed for multi-targeted projects. |
| 203 | + /// If needed and we're in interactive mode, prompts the user to select a framework. |
| 204 | + /// If needed and we're in non-interactive mode, shows an error. |
| 205 | + /// </summary> |
| 206 | + /// <returns>True if we can continue, false if we should exit</returns> |
| 207 | + private bool TrySelectTargetFrameworkIfNeeded() |
| 208 | + { |
| 209 | + Debug.Assert(ProjectFileFullPath is not null); |
| 210 | + |
| 211 | + var globalProperties = CommonRunHelpers.GetGlobalPropertiesFromArgs(MSBuildArgs); |
| 212 | + if (TargetFrameworkSelector.TrySelectTargetFramework( |
| 213 | + ProjectFileFullPath, |
| 214 | + globalProperties, |
| 215 | + Interactive, |
| 216 | + out string? selectedFramework)) |
| 217 | + { |
| 218 | + ApplySelectedFramework(selectedFramework); |
| 219 | + return true; |
| 220 | + } |
| 221 | + |
| 222 | + return false; |
| 223 | + } |
| 224 | + |
| 225 | + /// <summary> |
| 226 | + /// Checks if target framework selection is needed for file-based projects. |
| 227 | + /// Parses directives from the source file to detect multi-targeting. |
| 228 | + /// </summary> |
| 229 | + /// <returns>True if we can continue, false if we should exit</returns> |
| 230 | + private bool TrySelectTargetFrameworkForFileBasedProject() |
| 231 | + { |
| 232 | + Debug.Assert(EntryPointFileFullPath is not null); |
| 233 | + |
| 234 | + var globalProperties = CommonRunHelpers.GetGlobalPropertiesFromArgs(MSBuildArgs); |
| 235 | + |
| 236 | + // If a framework is already specified via --framework, no need to check |
| 237 | + if (globalProperties.TryGetValue("TargetFramework", out var existingFramework) && !string.IsNullOrWhiteSpace(existingFramework)) |
| 238 | + { |
| 239 | + return true; |
| 240 | + } |
| 241 | + |
| 242 | + // Get frameworks from source file directives |
| 243 | + var frameworks = GetTargetFrameworksFromSourceFile(EntryPointFileFullPath); |
| 244 | + if (frameworks is null || frameworks.Length == 0) |
| 245 | + { |
| 246 | + return true; // Not multi-targeted |
| 247 | + } |
| 248 | + |
| 249 | + // Use TargetFrameworkSelector to handle multi-target selection (or single framework selection) |
| 250 | + if (TargetFrameworkSelector.TrySelectTargetFramework(frameworks, Interactive, out string? selectedFramework)) |
| 251 | + { |
| 252 | + ApplySelectedFramework(selectedFramework); |
| 253 | + return true; |
| 254 | + } |
| 255 | + |
| 256 | + return false; |
| 257 | + } |
| 258 | + |
| 259 | + /// <summary> |
| 260 | + /// Parses a source file to extract target frameworks from directives. |
| 261 | + /// </summary> |
| 262 | + /// <returns>Array of frameworks if TargetFrameworks is specified, null otherwise</returns> |
| 263 | + private static string[]? GetTargetFrameworksFromSourceFile(string sourceFilePath) |
| 264 | + { |
| 265 | + var sourceFile = SourceFile.Load(sourceFilePath); |
| 266 | + var directives = FileLevelDirectiveHelpers.FindDirectives(sourceFile, reportAllErrors: false, DiagnosticBag.Ignore()); |
| 267 | + |
| 268 | + var targetFrameworksDirective = directives.OfType<CSharpDirective.Property>() |
| 269 | + .FirstOrDefault(p => string.Equals(p.Name, "TargetFrameworks", StringComparison.OrdinalIgnoreCase)); |
| 270 | + |
| 271 | + if (targetFrameworksDirective is null) |
| 272 | + { |
| 273 | + return null; |
| 274 | + } |
| 275 | + |
| 276 | + return targetFrameworksDirective.Value.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); |
| 277 | + } |
| 278 | + |
| 279 | + /// <summary> |
| 280 | + /// Applies the selected target framework to MSBuildArgs if a framework was provided. |
| 281 | + /// </summary> |
| 282 | + /// <param name="selectedFramework">The framework to apply, or null if no framework selection was needed</param> |
| 283 | + private void ApplySelectedFramework(string? selectedFramework) |
| 284 | + { |
| 285 | + // If selectedFramework is null, it means no framework selection was needed |
| 286 | + // (e.g., user already specified --framework, or single-target project) |
| 287 | + if (selectedFramework is not null) |
| 288 | + { |
| 289 | + var additionalProperties = new ReadOnlyDictionary<string, string>( |
| 290 | + new Dictionary<string, string> { { "TargetFramework", selectedFramework } }); |
| 291 | + MSBuildArgs = MSBuildArgs.CloneWithAdditionalProperties(additionalProperties); |
| 292 | + } |
| 293 | + } |
| 294 | + |
185 | 295 | internal void ApplyLaunchSettingsProfileToCommand(ICommand targetCommand, ProjectLaunchSettingsModel? launchSettings) |
186 | 296 | { |
187 | 297 | if (launchSettings == null) |
@@ -431,7 +541,8 @@ static ProjectInstance EvaluateProject(string? projectFilePath, Func<ProjectColl |
431 | 541 |
|
432 | 542 | static void ValidatePreconditions(ProjectInstance project) |
433 | 543 | { |
434 | | - if (string.IsNullOrWhiteSpace(project.GetPropertyValue("TargetFramework"))) |
| 544 | + // there must be some kind of TFM available to run a project |
| 545 | + if (string.IsNullOrWhiteSpace(project.GetPropertyValue("TargetFramework")) && string.IsNullOrEmpty(project.GetPropertyValue("TargetFrameworks"))) |
435 | 546 | { |
436 | 547 | ThrowUnableToRunError(project); |
437 | 548 | } |
@@ -504,16 +615,6 @@ static void InvokeRunArgumentsTarget(ProjectInstance project, bool noBuild, Faca |
504 | 615 | [DoesNotReturn] |
505 | 616 | internal static void ThrowUnableToRunError(ProjectInstance project) |
506 | 617 | { |
507 | | - string targetFrameworks = project.GetPropertyValue("TargetFrameworks"); |
508 | | - if (!string.IsNullOrEmpty(targetFrameworks)) |
509 | | - { |
510 | | - string targetFramework = project.GetPropertyValue("TargetFramework"); |
511 | | - if (string.IsNullOrEmpty(targetFramework)) |
512 | | - { |
513 | | - throw new GracefulException(CliCommandStrings.RunCommandExceptionUnableToRunSpecifyFramework, "--framework"); |
514 | | - } |
515 | | - } |
516 | | - |
517 | 618 | throw new GracefulException( |
518 | 619 | string.Format( |
519 | 620 | CliCommandStrings.RunCommandExceptionUnableToRun, |
|
0 commit comments