Skip to content

Commit 56610bc

Browse files
authored
Fix Windows path too long (#2681)
* Fix Windows path too long. * Extract const
1 parent c7cbbf7 commit 56610bc

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

src/BenchmarkDotNet/Running/BuildPartition.cs

Lines changed: 30 additions & 6 deletions
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,33 @@ 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+
const int MaxLength = 36;
95+
if (!OsDetector.IsWindows() || programName.Length <= MaxLength)
96+
{
97+
return programName;
98+
}
99+
programName = $"{benchmarkAssemblyName}-{id}";
100+
if (programName.Length <= MaxLength)
101+
{
102+
return programName;
103+
}
104+
programName = $"{folderInfo}-{id}";
105+
if (programName.Length <= MaxLength)
106+
{
107+
return programName;
108+
}
109+
return id.ToString();
110+
}
111+
88112
internal bool ForcedNoDependenciesForIntegrationTests
89113
{
90114
get
@@ -100,4 +124,4 @@ internal bool ForcedNoDependenciesForIntegrationTests
100124
}
101125
}
102126
}
103-
}
127+
}

tests/BenchmarkDotNet.IntegrationTests/InProcessEmitTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,11 @@ private void DiffEmit(Summary summary)
7171
return;
7272

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

0 commit comments

Comments
 (0)