Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3847cfb

Browse files
committed
small tweaks to enable running against singular-valued targetframeworks lists
1 parent 9141a23 commit 3847cfb

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

src/Cli/dotnet/Commands/Run/RunCommand.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class RunCommand
5454

5555
/// <summary>
5656
/// Parsed structure representing the MSBuild arguments that will be used to build the project.
57-
///
57+
///
5858
/// Note: This property has a private setter and is mutated within the class when framework selection modifies it.
5959
/// This mutability is necessary to allow the command to update MSBuild arguments after construction based on framework selection.
6060
/// </summary>
@@ -468,7 +468,8 @@ static ProjectInstance EvaluateProject(string? projectFilePath, Func<ProjectColl
468468

469469
static void ValidatePreconditions(ProjectInstance project)
470470
{
471-
if (string.IsNullOrWhiteSpace(project.GetPropertyValue("TargetFramework")))
471+
// there must be some kind of TFM available to run a project
472+
if (string.IsNullOrWhiteSpace(project.GetPropertyValue("TargetFramework")) && string.IsNullOrEmpty(project.GetPropertyValue("TargetFrameworks")))
472473
{
473474
ThrowUnableToRunError(project);
474475
}
@@ -853,11 +854,11 @@ private void SendProjectBasedTelemetry(ProjectLaunchSettingsModel? launchSetting
853854
{
854855
Debug.Assert(ProjectFileFullPath != null);
855856
var projectIdentifier = RunTelemetry.GetProjectBasedIdentifier(ProjectFileFullPath, GetRepositoryRoot(), Sha256Hasher.Hash);
856-
857+
857858
// Get package and project reference counts for project-based apps
858859
int packageReferenceCount = 0;
859860
int projectReferenceCount = 0;
860-
861+
861862
// Try to get project information for telemetry if we built the project
862863
if (ShouldBuild)
863864
{
@@ -866,10 +867,10 @@ private void SendProjectBasedTelemetry(ProjectLaunchSettingsModel? launchSetting
866867
var globalProperties = MSBuildArgs.GlobalProperties?.ToDictionary() ?? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
867868
globalProperties[Constants.EnableDefaultItems] = "false";
868869
globalProperties[Constants.MSBuildExtensionsPath] = AppContext.BaseDirectory;
869-
870+
870871
using var collection = new ProjectCollection(globalProperties: globalProperties);
871872
var project = collection.LoadProject(ProjectFileFullPath).CreateProjectInstance();
872-
873+
873874
packageReferenceCount = RunTelemetry.CountPackageReferences(project);
874875
projectReferenceCount = RunTelemetry.CountProjectReferences(project);
875876
}
@@ -898,10 +899,10 @@ private void SendProjectBasedTelemetry(ProjectLaunchSettingsModel? launchSetting
898899
{
899900
try
900901
{
901-
var currentDir = ProjectFileFullPath != null
902+
var currentDir = ProjectFileFullPath != null
902903
? Path.GetDirectoryName(ProjectFileFullPath)
903904
: Directory.GetCurrentDirectory();
904-
905+
905906
while (currentDir != null)
906907
{
907908
if (Directory.Exists(Path.Combine(currentDir, ".git")))
@@ -915,7 +916,7 @@ private void SendProjectBasedTelemetry(ProjectLaunchSettingsModel? launchSetting
915916
{
916917
// Ignore errors when trying to find repo root
917918
}
918-
919+
919920
return null;
920921
}
921922
}

src/Cli/dotnet/Commands/Run/TargetFrameworkSelector.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,15 @@ public static bool TrySelectTargetFramework(
5454
return true;
5555
}
5656

57-
var frameworks = targetFrameworks.Split(';', StringSplitOptions.RemoveEmptyEntries);
57+
// parse the TargetFrameworks property and make sure to account for any additional whitespace
58+
// users may have added for formatting reasons.
59+
var frameworks = targetFrameworks.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
5860

59-
// If there's only one framework, no selection needed
60-
if (frameworks.Length <= 1)
61+
// If there's only one framework in the TargetFrameworks, we do need to pick it to force the subsequent builds/evaluations
62+
// to act against the correct 'view' of the project
63+
if (frameworks.Length == 1)
6164
{
65+
selectedFramework = frameworks[0];
6266
return true;
6367
}
6468

@@ -73,12 +77,12 @@ public static bool TrySelectTargetFramework(
7377
Reporter.Error.WriteLine();
7478
Reporter.Error.WriteLine(CliCommandStrings.RunCommandAvailableTargetFrameworks);
7579
Reporter.Error.WriteLine();
76-
80+
7781
for (int i = 0; i < frameworks.Length; i++)
7882
{
7983
Reporter.Error.WriteLine($" {i + 1}. {frameworks[i]}");
8084
}
81-
85+
8286
Reporter.Error.WriteLine();
8387
Reporter.Error.WriteLine($"{CliCommandStrings.RunCommandExampleText}: dotnet run --framework {frameworks[0]}");
8488
Reporter.Error.WriteLine();
@@ -98,7 +102,7 @@ public static bool TrySelectTargetFramework(
98102
.PageSize(10)
99103
.MoreChoicesText($"[grey]({Markup.Escape(CliCommandStrings.RunCommandMoreFrameworksText)})[/]")
100104
.AddChoices(frameworks);
101-
105+
102106
return Spectre.Console.AnsiConsole.Prompt(prompt);
103107
}
104108
catch (Exception)

0 commit comments

Comments
 (0)