Skip to content

Commit 1f5637c

Browse files
authored
throw an exception when multiple benchmark projects with the same name are found, fixes #2126 (#2143)
1 parent 5ed46d5 commit 1f5637c

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/BenchmarkDotNet/Toolchains/CsProj/CsProjGenerator.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,19 +154,24 @@ protected virtual FileInfo GetProjectFilePath(Type benchmarkTarget, ILogger logg
154154
// important assumption! project's file name === output dll name
155155
string projectName = benchmarkTarget.GetTypeInfo().Assembly.GetName().Name;
156156

157-
// I was afraid of using .GetFiles with some smart search pattern due to the fact that the method was designed for Windows
158-
// and now .NET is cross platform so who knows if the pattern would be supported for other OSes
159157
var possibleNames = new HashSet<string> { $"{projectName}.csproj", $"{projectName}.fsproj", $"{projectName}.vbproj" };
160-
var projectFile = rootDirectory
161-
.EnumerateFiles("*.*", SearchOption.AllDirectories)
162-
.FirstOrDefault(file => possibleNames.Contains(file.Name));
158+
var projectFiles = rootDirectory
159+
.EnumerateFiles("*proj", SearchOption.AllDirectories)
160+
.Where(file => possibleNames.Contains(file.Name))
161+
.ToArray();
163162

164-
if (projectFile == default(FileInfo))
163+
if (projectFiles.Length == 0)
165164
{
166165
throw new NotSupportedException(
167166
$"Unable to find {projectName} in {rootDirectory.FullName} and its subfolders. Most probably the name of output exe is different than the name of the .(c/f)sproj");
168167
}
169-
return projectFile;
168+
else if (projectFiles.Length > 1)
169+
{
170+
throw new NotSupportedException(
171+
$"Found more than one matching project file for {projectName} in {rootDirectory.FullName} and its subfolders: {string.Join(",", projectFiles.Select(pf => $"'{pf.FullName}'"))}. Benchmark project names needs to be unique.");
172+
}
173+
174+
return projectFiles[0];
170175
}
171176

172177
public override bool Equals(object obj) => obj is CsProjGenerator other && Equals(other);

0 commit comments

Comments
 (0)