-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Avoid error when building file-based libraries #51063
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: release/10.0.1xx
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
|
||
internal static RunProperties FromProject(ProjectInstance project) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like a call site of |
||
{ | ||
if (!TryFromProject(project, out var result)) | ||
{ | ||
RunCommand.ThrowUnableToRunError(project); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1483,6 +1483,230 @@ 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 | ||
333fred marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 Build_Library_MultiTarget() | ||
{ | ||
var testInstance = _testAssetsManager.CreateTestDirectory(); | ||
var programFile = Path.Join(testInstance.Path, "lib.cs"); | ||
File.WriteAllText(programFile, $""" | ||
#:property OutputType=Library | ||
#:property PublishAot=false | ||
#:property LangVersion=preview | ||
#:property TargetFrameworks=netstandard2.0;{ToolsetInfo.CurrentTargetFramework} | ||
class C; | ||
"""); | ||
|
||
// https://github.com/dotnet/sdk/issues/51077: cannot set this via `#:property` directive. | ||
File.WriteAllText(Path.Join(testInstance.Path, "Directory.Build.props"), """ | ||
<Project> | ||
<PropertyGroup> | ||
<TargetFramework></TargetFramework> | ||
</PropertyGroup> | ||
</Project> | ||
"""); | ||
|
||
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.RunCommandExceptionUnableToRunSpecifyFramework, "--framework")); | ||
|
||
new DotnetCommand(Log, "run", "lib.cs", "--framework", ToolsetInfo.CurrentTargetFramework) | ||
.WithWorkingDirectory(testInstance.Path) | ||
.Execute() | ||
.Should().Fail() | ||
.And.HaveStdErr(string.Format(CliCommandStrings.RunCommandExceptionUnableToRun, | ||
Path.ChangeExtension(programFile, ".csproj"), | ||
ToolsetInfo.CurrentTargetFrameworkVersion, | ||
"Library")); | ||
} | ||
|
||
[Fact] | ||
public void Build_Module() | ||
{ | ||
var testInstance = _testAssetsManager.CreateTestDirectory(); | ||
var programFile = Path.Join(testInstance.Path, "module.cs"); | ||
File.WriteAllText(programFile, """ | ||
#:property OutputType=Module | ||
#:property ProduceReferenceAssembly=false | ||
class C; | ||
"""); | ||
|
||
var artifactsDir = VirtualProjectBuildingCommand.GetArtifactsPath(programFile); | ||
if (Directory.Exists(artifactsDir)) Directory.Delete(artifactsDir, recursive: true); | ||
|
||
new DotnetCommand(Log, "build", "module.cs") | ||
.WithWorkingDirectory(testInstance.Path) | ||
.Execute() | ||
.Should().Pass(); | ||
|
||
new DotnetCommand(Log, "run", "module.cs") | ||
.WithWorkingDirectory(testInstance.Path) | ||
.Execute() | ||
.Should().Fail() | ||
.And.HaveStdErr(string.Format(CliCommandStrings.RunCommandExceptionUnableToRun, | ||
Path.ChangeExtension(programFile, ".csproj"), | ||
ToolsetInfo.CurrentTargetFrameworkVersion, | ||
"Module")); | ||
} | ||
|
||
[Fact] | ||
public void Build_WinExe() | ||
{ | ||
var testInstance = _testAssetsManager.CreateTestDirectory(); | ||
var programFile = Path.Join(testInstance.Path, "winexe.cs"); | ||
File.WriteAllText(programFile, """ | ||
#:property OutputType=WinExe | ||
Console.WriteLine("Hello WinExe"); | ||
"""); | ||
|
||
var artifactsDir = VirtualProjectBuildingCommand.GetArtifactsPath(programFile); | ||
if (Directory.Exists(artifactsDir)) Directory.Delete(artifactsDir, recursive: true); | ||
|
||
new DotnetCommand(Log, "build", "winexe.cs") | ||
.WithWorkingDirectory(testInstance.Path) | ||
.Execute() | ||
.Should().Pass(); | ||
|
||
new DotnetCommand(Log, "run", "winexe.cs") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This question isn't really relevant to the fix, but.. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Output from WinExe is visible when redirected, e.g., |
||
.WithWorkingDirectory(testInstance.Path) | ||
.Execute() | ||
.Should().Pass() | ||
.And.HaveStdOut("Hello WinExe"); | ||
} | ||
|
||
[Fact] | ||
public void Build_Exe() | ||
{ | ||
var testInstance = _testAssetsManager.CreateTestDirectory(); | ||
var programFile = Path.Join(testInstance.Path, "exe.cs"); | ||
File.WriteAllText(programFile, """ | ||
#:property OutputType=Exe | ||
Console.WriteLine("Hello Exe"); | ||
"""); | ||
|
||
var artifactsDir = VirtualProjectBuildingCommand.GetArtifactsPath(programFile); | ||
if (Directory.Exists(artifactsDir)) Directory.Delete(artifactsDir, recursive: true); | ||
|
||
new DotnetCommand(Log, "build", "exe.cs") | ||
.WithWorkingDirectory(testInstance.Path) | ||
.Execute() | ||
.Should().Pass(); | ||
|
||
new DotnetCommand(Log, "run", "exe.cs") | ||
.WithWorkingDirectory(testInstance.Path) | ||
.Execute() | ||
.Should().Pass() | ||
.And.HaveStdOut("Hello Exe"); | ||
} | ||
|
||
[Fact] | ||
public void Build_Exe_MultiTarget() | ||
{ | ||
var testInstance = _testAssetsManager.CreateTestDirectory(); | ||
var programFile = Path.Join(testInstance.Path, "exe.cs"); | ||
File.WriteAllText(programFile, $""" | ||
#:property OutputType=Exe | ||
#:property PublishAot=false | ||
#:property LangVersion=preview | ||
#:property TargetFrameworks=netstandard2.0;{ToolsetInfo.CurrentTargetFramework} | ||
Console.WriteLine("Hello Exe"); | ||
"""); | ||
|
||
// https://github.com/dotnet/sdk/issues/51077: cannot set this via `#:property` directive. | ||
File.WriteAllText(Path.Join(testInstance.Path, "Directory.Build.props"), """ | ||
<Project> | ||
<PropertyGroup> | ||
<TargetFramework></TargetFramework> | ||
</PropertyGroup> | ||
</Project> | ||
"""); | ||
|
||
var artifactsDir = VirtualProjectBuildingCommand.GetArtifactsPath(programFile); | ||
if (Directory.Exists(artifactsDir)) Directory.Delete(artifactsDir, recursive: true); | ||
|
||
new DotnetCommand(Log, "build", "exe.cs") | ||
.WithWorkingDirectory(testInstance.Path) | ||
.Execute() | ||
.Should().Pass(); | ||
|
||
new DotnetCommand(Log, "run", "exe.cs") | ||
.WithWorkingDirectory(testInstance.Path) | ||
.Execute() | ||
.Should().Fail() | ||
.And.HaveStdErr(string.Format(CliCommandStrings.RunCommandExceptionUnableToRunSpecifyFramework, "--framework")); | ||
|
||
new DotnetCommand(Log, "run", "exe.cs", "--framework", ToolsetInfo.CurrentTargetFramework) | ||
.WithWorkingDirectory(testInstance.Path) | ||
.Execute() | ||
.Should().Pass() | ||
.And.HaveStdOut("Hello Exe"); | ||
} | ||
|
||
[Fact] | ||
public void Build_AppContainerExe() | ||
{ | ||
var testInstance = _testAssetsManager.CreateTestDirectory(); | ||
var programFile = Path.Join(testInstance.Path, "appcontainerexe.cs"); | ||
File.WriteAllText(programFile, """ | ||
#:property OutputType=AppContainerExe | ||
Console.WriteLine("Hello AppContainerExe"); | ||
"""); | ||
|
||
var artifactsDir = VirtualProjectBuildingCommand.GetArtifactsPath(programFile); | ||
if (Directory.Exists(artifactsDir)) Directory.Delete(artifactsDir, recursive: true); | ||
|
||
new DotnetCommand(Log, "build", "appcontainerexe.cs") | ||
.WithWorkingDirectory(testInstance.Path) | ||
.Execute() | ||
.Should().Pass(); | ||
|
||
new DotnetCommand(Log, "run", "appcontainerexe.cs") | ||
.WithWorkingDirectory(testInstance.Path) | ||
.Execute() | ||
.Should().Fail() | ||
.And.HaveStdErr(string.Format(CliCommandStrings.RunCommandExceptionUnableToRun, | ||
Path.ChangeExtension(programFile, ".csproj"), | ||
ToolsetInfo.CurrentTargetFrameworkVersion, | ||
"AppContainerExe")); | ||
} | ||
|
||
[Fact] | ||
public void Publish() | ||
{ | ||
|
There was a problem hiding this comment.
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 isfalse
.