Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 8 additions & 3 deletions src/Cli/dotnet/Commands/Run/RunProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,22 @@ internal RunProperties(string command, string? arguments, string? workingDirecto
{
}

internal static RunProperties FromProject(ProjectInstance project)
internal static bool TryFromProject(ProjectInstance project, out RunProperties result)
{
var result = new RunProperties(
result = new RunProperties(
Command: project.GetPropertyValue("RunCommand"),
Arguments: project.GetPropertyValue("RunArguments"),
WorkingDirectory: project.GetPropertyValue("RunWorkingDirectory"),
RuntimeIdentifier: project.GetPropertyValue("RuntimeIdentifier"),
DefaultAppHostRuntimeIdentifier: project.GetPropertyValue("DefaultAppHostRuntimeIdentifier"),
TargetFrameworkVersion: project.GetPropertyValue("TargetFrameworkVersion"));

if (string.IsNullOrEmpty(result.Command))
return !string.IsNullOrEmpty(result.Command);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems a little strange for result to contain non-null when the return value is false.

}

internal static RunProperties FromProject(ProjectInstance project)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a call site of FromProject in src/Cli/dotnet/Commands/Test/MTP/SolutionAndProjectUtility.cs:308 is reimplementing some aspects of FromProject. It doesn't seem like it would lead to incorrect behavior, but, thought I would call it out anyway.

{
if (!TryFromProject(project, out var result))
{
RunCommand.ThrowUnableToRunError(project);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ public override int Execute()
Debug.Assert(buildRequest.ProjectInstance != null);

// Cache run info (to avoid re-evaluating the project instance).
cache.CurrentEntry.Run = RunProperties.FromProject(buildRequest.ProjectInstance);
cache.CurrentEntry.Run = RunProperties.TryFromProject(buildRequest.ProjectInstance, out var runProperties)
? runProperties
: null;

TryCacheCscArguments(cache, buildResult, buildRequest.ProjectInstance);

Expand Down Expand Up @@ -897,7 +899,7 @@ private BuildLevel GetBuildLevel(out CacheInfo cache)
{
if (!NeedsToBuild(out cache))
{
Reporter.Verbose.WriteLine("No need to build, the output is up to date.");
Reporter.Verbose.WriteLine("No need to build, the output is up to date. Cache: " + ArtifactsPath);
return BuildLevel.None;
}

Expand Down
28 changes: 28 additions & 0 deletions test/dotnet.Tests/CommandTests/Run/RunFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1483,6 +1483,34 @@ public void NoBuild_02()
.And.HaveStdOut("Changed");
}

[Fact]
public void Build_Library()
{
var testInstance = _testAssetsManager.CreateTestDirectory();
var programFile = Path.Join(testInstance.Path, "lib.cs");
File.WriteAllText(programFile, """
#:property OutputType=Library
class C;
""");

var artifactsDir = VirtualProjectBuildingCommand.GetArtifactsPath(programFile);
if (Directory.Exists(artifactsDir)) Directory.Delete(artifactsDir, recursive: true);

new DotnetCommand(Log, "build", "lib.cs")
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should().Pass();

new DotnetCommand(Log, "run", "lib.cs")
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should().Fail()
.And.HaveStdErr(string.Format(CliCommandStrings.RunCommandExceptionUnableToRun,
Path.ChangeExtension(programFile, ".csproj"),
ToolsetInfo.CurrentTargetFrameworkVersion,
"Library"));
}

[Fact]
public void Publish()
{
Expand Down
Loading