Skip to content

Commit 00adc98

Browse files
committed
Fix Windows path too long.
1 parent cd50f7b commit 00adc98

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/BenchmarkDotNet/Running/BuildPartition.cs

+28-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Threading;
55
using BenchmarkDotNet.Characteristics;
66
using BenchmarkDotNet.Configs;
7+
using BenchmarkDotNet.Detectors;
78
using BenchmarkDotNet.Environments;
89
using BenchmarkDotNet.Helpers;
910
using BenchmarkDotNet.Jobs;
@@ -28,11 +29,7 @@ public BuildPartition(BenchmarkBuildInfo[] benchmarks, IResolver resolver)
2829
Resolver = resolver;
2930
RepresentativeBenchmarkCase = benchmarks[0].BenchmarkCase;
3031
Benchmarks = benchmarks;
31-
// Combine the benchmark's assembly name, folder info, and build partition id.
32-
string benchmarkAssemblyName = RepresentativeBenchmarkCase.Descriptor.Type.Assembly.GetName().Name;
33-
string folderInfo = RepresentativeBenchmarkCase.Job.FolderInfo;
34-
int id = Interlocked.Increment(ref s_partitionCounter);
35-
ProgramName = $"{benchmarkAssemblyName}-{folderInfo}-{id}";
32+
ProgramName = GetProgramName(RepresentativeBenchmarkCase, Interlocked.Increment(ref s_partitionCounter));
3633
LogBuildOutput = benchmarks[0].Config.Options.IsSet(ConfigOptions.LogBuildOutput);
3734
GenerateMSBuildBinLog = benchmarks[0].Config.Options.IsSet(ConfigOptions.GenerateMSBuildBinLog);
3835
}
@@ -85,6 +82,32 @@ private static string GetResolvedAssemblyLocation(Assembly assembly) =>
8582
// manually construct the path.
8683
assembly.Location.Length == 0 ? Path.Combine(AppContext.BaseDirectory, assembly.GetName().Name) : assembly.Location;
8784

85+
internal static string GetProgramName(BenchmarkCase representativeBenchmarkCase, int id)
86+
{
87+
// Combine the benchmark's assembly name, folder info, and build partition id.
88+
string benchmarkAssemblyName = representativeBenchmarkCase.Descriptor.Type.Assembly.GetName().Name;
89+
string folderInfo = representativeBenchmarkCase.Job.FolderInfo;
90+
var programName = $"{benchmarkAssemblyName}-{folderInfo}-{id}";
91+
// Very long program name can cause the path to exceed Windows' 260 character limit,
92+
// for example BenchmarkDotNet.IntegrationTests.ManualRunning.MultipleFrameworks.
93+
// 36 is an arbitrary limit, but it's the length of Guid strings which is what was used previously.
94+
if (!OsDetector.IsWindows() || programName.Length <= 36)
95+
{
96+
return programName;
97+
}
98+
programName = $"{benchmarkAssemblyName}-{id}";
99+
if (programName.Length <= 36)
100+
{
101+
return programName;
102+
}
103+
programName = $"{folderInfo}-{id}";
104+
if (programName.Length <= 36)
105+
{
106+
return programName;
107+
}
108+
return id.ToString();
109+
}
110+
88111
internal bool ForcedNoDependenciesForIntegrationTests
89112
{
90113
get

tests/BenchmarkDotNet.IntegrationTests/InProcessEmitTest.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,11 @@ private void DiffEmit(Summary summary)
7070
return;
7171

7272
var benchmarkCase = summary.BenchmarksCases.First();
73-
var caseName = $"{benchmarkCase.Descriptor.Type.Assembly.GetName().Name}-{benchmarkCase.Job.FolderInfo}";
7473
// The benchmark config built jobs with 2 toolchains, 1 InProcessEmit and 1 Roslyn,
7574
// so we need to subtract 1 from the partition counter to obtain the emit output.
7675
NaiveRunnableEmitDiff.RunDiff(
77-
$@"{caseName}-{BuildPartition.s_partitionCounter}.exe",
78-
$@"{caseName}-{BuildPartition.s_partitionCounter - 1}Emitted.dll",
76+
$@"{BuildPartition.GetProgramName(benchmarkCase, BuildPartition.s_partitionCounter)}.exe",
77+
$@"{BuildPartition.GetProgramName(benchmarkCase, BuildPartition.s_partitionCounter - 1)}Emitted.dll",
7978
ConsoleLogger.Default);
8079
}
8180

0 commit comments

Comments
 (0)