Skip to content

Commit

Permalink
Respect response files (ignoreProjectExtensions) property (#9677)
Browse files Browse the repository at this point in the history
  • Loading branch information
f-alizada authored Feb 6, 2024
1 parent bea6b4a commit 668b199
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
17 changes: 17 additions & 0 deletions src/MSBuild.UnitTests/XMake_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1442,6 +1442,23 @@ public void ResponseFileInProjectDirectoryButCommandLineNoAutoResponseSwitch()
output.ShouldContain("[A=]");
}

/// <summary>
/// Directory.Build.rsp in the directory of the specified project/solution should be respected when searching the files (solution/proj) to build.
/// </summary>
[Fact]
public void ResponseFileInProjectDirectoryWithSolutionProjectDifferentNamesShouldBeRespected()
{
var directory = _env.CreateFolder();
var content = ObjectModelHelpers.CleanupFileContents("<Project><Target Name='t'><Message Text='Completed'/></Target></Project>");
directory.CreateFile("projectFile.proj", content);
directory.CreateFile("solutionFile.sln", string.Empty);
directory.CreateFile("Directory.Build.rsp", "-ignoreProjectExtensions:.sln");

var msbuildParameters = "\"" + directory.Path + "\"";
RunnerUtilities.ExecMSBuild(msbuildParameters, out var successfulExit, _output);
successfulExit.ShouldBeTrue();
}

/// <summary>
/// Any msbuild.rsp in the directory of the specified project/solution should be read, and should
/// take priority over any other response files. Sanity test when there isn't one.
Expand Down
49 changes: 38 additions & 11 deletions src/MSBuild/XMake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ private static bool CanRunServerBasedOnCommandLineSwitches(
{
GatherAllSwitches(commandLine, out var switchesFromAutoResponseFile, out var switchesNotFromAutoResponseFile, out string fullCommandLine);
CommandLineSwitches commandLineSwitches = CombineSwitchesRespectingPriority(switchesFromAutoResponseFile, switchesNotFromAutoResponseFile, fullCommandLine);
if (CheckAndGatherProjectAutoResponseFile(switchesFromAutoResponseFile, commandLineSwitches, false, fullCommandLine, out string projectFile))
if (CheckAndGatherProjectAutoResponseFile(switchesFromAutoResponseFile, commandLineSwitches, false, fullCommandLine))
{
commandLineSwitches = CombineSwitchesRespectingPriority(switchesFromAutoResponseFile, switchesNotFromAutoResponseFile, fullCommandLine);
}

string projectFile = ProcessProjectSwitch(commandLineSwitches[CommandLineSwitches.ParameterizedSwitch.Project], commandLineSwitches[CommandLineSwitches.ParameterizedSwitch.IgnoreProjectExtensions], Directory.GetFiles);
if (commandLineSwitches[CommandLineSwitches.ParameterlessSwitch.Help] ||
commandLineSwitches.IsParameterizedSwitchSet(CommandLineSwitches.ParameterizedSwitch.NodeMode) ||
commandLineSwitches[CommandLineSwitches.ParameterlessSwitch.Version] ||
Expand Down Expand Up @@ -2503,7 +2503,7 @@ private static bool ProcessCommandLineSwitches(
}
else
{
bool foundProjectAutoResponseFile = CheckAndGatherProjectAutoResponseFile(switchesFromAutoResponseFile, commandLineSwitches, recursing, commandLine, out projectFile);
bool foundProjectAutoResponseFile = CheckAndGatherProjectAutoResponseFile(switchesFromAutoResponseFile, commandLineSwitches, recursing, commandLine);

if (foundProjectAutoResponseFile)
{
Expand Down Expand Up @@ -2556,6 +2556,8 @@ private static bool ProcessCommandLineSwitches(
commandLine);
}

projectFile = ProcessProjectSwitch(commandLineSwitches[CommandLineSwitches.ParameterizedSwitch.Project], commandLineSwitches[CommandLineSwitches.ParameterizedSwitch.IgnoreProjectExtensions], Directory.GetFiles);

// figure out which targets we are building
targets = ProcessTargetSwitch(commandLineSwitches[CommandLineSwitches.ParameterizedSwitch.Target]);

Expand Down Expand Up @@ -2912,18 +2914,43 @@ private static CommandLineSwitches CombineSwitchesRespectingPriority(CommandLine
return commandLineSwitches;
}

private static bool CheckAndGatherProjectAutoResponseFile(CommandLineSwitches switchesFromAutoResponseFile, CommandLineSwitches commandLineSwitches, bool recursing, string commandLine, out string projectFile)
private static string GetProjectDirectory(string[] projectSwitchParameters)
{
bool found = false;
string projectDirectory = ".";
ErrorUtilities.VerifyThrow(projectSwitchParameters.Length <= 1, "Expect exactly one project at a time.");

// figure out what project we are building
projectFile = ProcessProjectSwitch(commandLineSwitches[CommandLineSwitches.ParameterizedSwitch.Project], commandLineSwitches[CommandLineSwitches.ParameterizedSwitch.IgnoreProjectExtensions], Directory.GetFiles);
if (projectSwitchParameters.Length == 1)
{
var projectFile = FileUtilities.FixFilePath(projectSwitchParameters[0]);

if (FileSystems.Default.DirectoryExists(projectFile))
{
// the provided argument value is actually the directory
projectDirectory = projectFile;
}
else
{
InitializationException.VerifyThrow(FileSystems.Default.FileExists(projectFile), "ProjectNotFoundError", projectFile);
projectDirectory = Path.GetDirectoryName(Path.GetFullPath(projectFile));
}
}

return projectDirectory;
}


/// <summary>
/// Identifies if there is rsp files near the project file
/// </summary>
/// <returns>true if there autoresponse file was found</returns>
private static bool CheckAndGatherProjectAutoResponseFile(CommandLineSwitches switchesFromAutoResponseFile, CommandLineSwitches commandLineSwitches, bool recursing, string commandLine)
{
bool found = false;

var projectDirectory = GetProjectDirectory(commandLineSwitches[CommandLineSwitches.ParameterizedSwitch.Project]);

if (!recursing && !commandLineSwitches[CommandLineSwitches.ParameterlessSwitch.NoAutoResponse])
{
// gather any switches from an msbuild.rsp that is next to the project or solution file itself
string projectDirectory = Path.GetDirectoryName(Path.GetFullPath(projectFile));

// gather any switches from the first Directory.Build.rsp found in the project directory or above
string directoryResponseFile = FileUtilities.GetPathOfFileAbove(directoryResponseFileName, projectDirectory);

Expand Down Expand Up @@ -3377,7 +3404,7 @@ internal static string ProcessProjectSwitch(
string[] projectsExtensionsToIgnore,
DirectoryGetFiles getFiles)
{
ErrorUtilities.VerifyThrow(parameters.Length <= 1, "It should not be possible to specify more than 1 project at a time.");
ErrorUtilities.VerifyThrow(parameters.Length <= 1, "Expect exactly one project at a time.");
string projectFile = null;

string projectDirectory = null;
Expand Down

0 comments on commit 668b199

Please sign in to comment.