From cbeb5b9912cc23fa0197533c4478faac96d426c5 Mon Sep 17 00:00:00 2001 From: Eric Erhardt Date: Thu, 13 Oct 2016 17:08:05 -0500 Subject: [PATCH] Report a meaningful error when trying to run a multi-TFM project. Add `--no-build` option to `dotnet run3` since even incrementally building a project takes a non-trivial amount of time. --- src/dotnet/commands/dotnet-run3/Program.cs | 4 ++ .../commands/dotnet-run3/Run3Command.cs | 47 ++++++++++++++----- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/dotnet/commands/dotnet-run3/Program.cs b/src/dotnet/commands/dotnet-run3/Program.cs index 843735376..00f5ae63e 100644 --- a/src/dotnet/commands/dotnet-run3/Program.cs +++ b/src/dotnet/commands/dotnet-run3/Program.cs @@ -28,6 +28,9 @@ namespace Microsoft.DotNet.Tools.Run CommandOption framework = app.Option( "-f|--framework ", "Compile a specific framework", CommandOptionType.SingleValue); + CommandOption noBuild = app.Option( + "--no-build", "Do not build the project before running.", + CommandOptionType.BoolValue); CommandOption project = app.Option( "-p|--project", "The path to the project file to run (defaults to the current directory if there is only one project).", CommandOptionType.SingleValue); @@ -38,6 +41,7 @@ namespace Microsoft.DotNet.Tools.Run runCmd.Configuration = configuration.Value(); runCmd.Framework = framework.Value(); + runCmd.NoBuild = noBuild.BoolValue ?? false; runCmd.Project = project.Value(); runCmd.Args = app.RemainingArguments; diff --git a/src/dotnet/commands/dotnet-run3/Run3Command.cs b/src/dotnet/commands/dotnet-run3/Run3Command.cs index c7c3ee09f..d23d20981 100644 --- a/src/dotnet/commands/dotnet-run3/Run3Command.cs +++ b/src/dotnet/commands/dotnet-run3/Run3Command.cs @@ -5,7 +5,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; -using Microsoft.Build.Execution; +using Microsoft.Build.Evaluation; using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Tools.MSBuild; @@ -15,6 +15,7 @@ namespace Microsoft.DotNet.Tools.Run { public string Configuration { get; set; } public string Framework { get; set; } + public bool NoBuild { get; set; } public string Project { get; set; } public IReadOnlyList Args { get; set; } @@ -28,7 +29,10 @@ namespace Microsoft.DotNet.Tools.Run { Initialize(); - EnsureProjectIsBuilt(); + if (!NoBuild) + { + EnsureProjectIsBuilt(); + } ICommand runCommand = GetRunCommand(); @@ -80,21 +84,16 @@ namespace Microsoft.DotNet.Tools.Run globalProperties.Add("TargetFramework", Framework); } - ProjectInstance projectInstance = new ProjectInstance(Project, globalProperties, null); + Project project = new Project(Project, globalProperties, null); - string runProgram = projectInstance.GetPropertyValue("RunCommand"); + string runProgram = project.GetPropertyValue("RunCommand"); if (string.IsNullOrEmpty(runProgram)) { - string outputType = projectInstance.GetPropertyValue("OutputType"); - - throw new GracefulException(string.Join(Environment.NewLine, - "Unable to run your project.", - "Please ensure you have a runnable project type and ensure 'dotnet run' supports this project.", - $"The current OutputType is '{outputType}'.")); + ThrowUnableToRunError(project); } - string runArguments = projectInstance.GetPropertyValue("RunArguments"); - string runWorkingDirectory = projectInstance.GetPropertyValue("RunWorkingDirectory"); + string runArguments = project.GetPropertyValue("RunArguments"); + string runWorkingDirectory = project.GetPropertyValue("RunWorkingDirectory"); string fullArguments = runArguments; if (_args.Any()) @@ -108,6 +107,30 @@ namespace Microsoft.DotNet.Tools.Run .WorkingDirectory(runWorkingDirectory); } + private void ThrowUnableToRunError(Project project) + { + string unableToRunYourProjectMessage = "Unable to run your project."; + + string targetFrameworks = project.GetPropertyValue("TargetFrameworks"); + if (!string.IsNullOrEmpty(targetFrameworks)) + { + string targetFramework = project.GetPropertyValue("TargetFramework"); + if (string.IsNullOrEmpty(targetFramework)) + { + throw new GracefulException(string.Join(Environment.NewLine, + unableToRunYourProjectMessage, + "Your project targets multiple frameworks. Please specify which framework to run using '--framework'.")); + } + } + + string outputType = project.GetPropertyValue("OutputType"); + + throw new GracefulException(string.Join(Environment.NewLine, + unableToRunYourProjectMessage, + "Please ensure you have a runnable project type and ensure 'dotnet run' supports this project.", + $"The current OutputType is '{outputType}'.")); + } + private void Initialize() { if (string.IsNullOrWhiteSpace(Project))