Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions src/Build/Logging/SimpleErrorLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ private void LogWithColor(string message, TerminalColor color)
{
if (acceptAnsiColorCodes)
{
Console.Error.Write(AnsiCodes.Colorize(message, color));
Console.Error.WriteLine(AnsiCodes.Colorize(message, color));
}
else
{
Console.Error.Write(message);
Console.Error.WriteLine(message);
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/MSBuild.UnitTests/XMake_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,17 @@ public void InvalidMaxCPUCountSwitch4()
});
}

[Fact]
public void GetPropertyWithInvalidProjectThrowsInvalidProjectFileExceptionNotInternalError()
{
using TestEnvironment env = TestEnvironment.Create();
TransientTestFile project = env.CreateFile("testProject.csproj", "Project");
string result = RunnerUtilities.ExecMSBuild($" {project.Path} -getProperty:Foo", out bool success);
success.ShouldBeFalse();
result.ShouldContain("MSB4025");
result.ShouldNotContain("MSB1025");
}

[Theory]
[InlineData("-getProperty:Foo;Bar", true, "EvalValue", false, false, false, true, false)]
[InlineData("-getProperty:Foo;Bar -t:Build", true, "TargetValue", false, false, false, true, false)]
Expand Down
43 changes: 21 additions & 22 deletions src/MSBuild/XMake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -809,11 +809,18 @@ public static ExitType Execute(
}
else if ((getProperty.Length > 0 || getItem.Length > 0) && (targets is null || targets.Length == 0))
{
using (ProjectCollection collection = new(globalProperties, loggers, ToolsetDefinitionLocations.Default))
try
{
Project project = collection.LoadProject(projectFile, globalProperties, toolsVersion);
exitType = OutputPropertiesAfterEvaluation(getProperty, getItem, project);
collection.LogBuildFinishedEvent(exitType == ExitType.Success);
using (ProjectCollection collection = new(globalProperties, loggers, ToolsetDefinitionLocations.Default))
{
Project project = collection.LoadProject(projectFile, globalProperties, toolsVersion);
exitType = OutputPropertiesAfterEvaluation(getProperty, getItem, project);
collection.LogBuildFinishedEvent(exitType == ExitType.Success);
}
}
catch (InvalidProjectFileException)
{
exitType = ExitType.BuildError;
}
}
else // regular build
Expand Down Expand Up @@ -1031,28 +1038,20 @@ public static ExitType Execute(

private static ExitType OutputPropertiesAfterEvaluation(string[] getProperty, string[] getItem, Project project)
{
try
// Special case if the user requests exactly one property: skip json formatting
if (getProperty.Length == 1 && getItem.Length == 0)
{
// Special case if the user requests exactly one property: skip json formatting
if (getProperty.Length == 1 && getItem.Length == 0)
{
Console.WriteLine(project.GetPropertyValue(getProperty[0]));
}
else
{
JsonOutputFormatter jsonOutputFormatter = new();
jsonOutputFormatter.AddPropertiesInJsonFormat(getProperty, property => project.GetPropertyValue(property));
jsonOutputFormatter.AddItemsInJsonFormat(getItem, project);
Console.WriteLine(jsonOutputFormatter.ToString());
}

return ExitType.Success;
Console.WriteLine(project.GetPropertyValue(getProperty[0]));
}
catch (InvalidProjectFileException e)
else
{
Console.Error.WriteLine(e.Message);
return ExitType.BuildError;
JsonOutputFormatter jsonOutputFormatter = new();
jsonOutputFormatter.AddPropertiesInJsonFormat(getProperty, property => project.GetPropertyValue(property));
jsonOutputFormatter.AddItemsInJsonFormat(getItem, project);
Console.WriteLine(jsonOutputFormatter.ToString());
}

return ExitType.Success;
}

private static ExitType OutputBuildInformationInJson(BuildResult result, string[] getProperty, string[] getItem, string[] getTargetResult, ILogger[] loggers, ExitType exitType)
Expand Down