Skip to content

Revise TryGetProjectOrSolutionFilePath and GetSolutionFilePaths logic for the new dotnet test experience #47635

@Youssef1313

Description

@Youssef1313

string[] solutionFiles = Directory.GetFiles(directory, CliConstants.SolutionExtensionPattern, SearchOption.TopDirectoryOnly);
solutionFiles.AddRange(Directory.GetFiles(directory, CliConstants.SolutionXExtensionPattern, SearchOption.TopDirectoryOnly));

The second line above should crash. An array is fixed-length. It's implicitly converted to ICollection<string> in that code path and should throw some exception at run-time.


public static (bool SolutionOrProjectFileFound, string Message) TryGetProjectOrSolutionFilePath(string directory, out string projectOrSolutionFilePath, out bool isSolution)
{
projectOrSolutionFilePath = string.Empty;
isSolution = false;
if (!Directory.Exists(directory))
{
return (false, string.Format(LocalizableStrings.CmdNonExistentDirectoryErrorDescription, directory));
}
var solutionPaths = GetSolutionFilePaths(directory);
// If more than a single sln file is found, an error is thrown since we can't determine which one to choose.
if (solutionPaths.Length > 1)
{
return (false, string.Format(CommonLocalizableStrings.MoreThanOneSolutionInDirectory, directory));
}
if (solutionPaths.Length == 1)
{
var projectPaths = GetProjectFilePaths(directory);
if (projectPaths.Length == 0)
{
projectOrSolutionFilePath = solutionPaths[0];
isSolution = true;
return (true, string.Empty);
}
return (false, LocalizableStrings.CmdMultipleProjectOrSolutionFilesErrorDescription);
}
else // If no solutions are found, look for a project file
{
string[] projectPaths = GetProjectFilePaths(directory);
if (projectPaths.Length == 0)
{
var solutionFilterPaths = GetSolutionFilterFilePaths(directory);
if (solutionFilterPaths.Length == 0)
{
return (false, LocalizableStrings.CmdNoProjectOrSolutionFileErrorDescription);
}
if (solutionFilterPaths.Length == 1)
{
projectOrSolutionFilePath = solutionFilterPaths[0];
isSolution = true;
return (true, string.Empty);
}
else
{
return (false, LocalizableStrings.CmdMultipleProjectOrSolutionFilesErrorDescription);
}
}
if (projectPaths.Length == 1)
{
projectOrSolutionFilePath = projectPaths[0];
return (true, string.Empty);
}
return (false, string.Format(CommonLocalizableStrings.MoreThanOneSolutionInDirectory, directory));
}
}

I'm reading this logic as "If there are no sln/slnx files at all, and a single project file is found, use the project file". But what if the directory has slnf and project file, without sln/slnx? I think we should either prefer the slnf, or error. It's a pattern I have never seen, but ...

Anyways, we should validate the behavior of dotnet build, and make sure we match it, and share some common helpers if needed.

Metadata

Metadata

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions