Skip to content

Commit f6d3c25

Browse files
committed
Added comments.
Write to temp file before overwriting original file. Fixed formatting.
1 parent c9acbc2 commit f6d3c25

File tree

5 files changed

+82
-29
lines changed

5 files changed

+82
-29
lines changed

build/common.props

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
</PropertyGroup>
4343

4444
<PropertyGroup Condition=" '$(VersionPrefix)' == '' ">
45+
<!-- When this is changed, optionally reset WeaverVersionSuffix, then run `build.cmd pack-weaver`. -->
4546
<VersionPrefix>0.15.2</VersionPrefix>
4647
</PropertyGroup>
4748

@@ -57,6 +58,11 @@
5758
<PackageVersion>$(Version)</PackageVersion>
5859
</PropertyGroup>
5960

61+
<PropertyGroup>
62+
<!-- Increment this when the BenchmarkDotNet.Weaver package needs to be re-packed. -->
63+
<WeaverVersionSuffix>-1</WeaverVersionSuffix>
64+
</PropertyGroup>
65+
6066
<ItemGroup>
6167
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3">
6268
<PrivateAssets>all</PrivateAssets>
@@ -90,9 +96,4 @@
9096
</Code>
9197
</Task>
9298
</UsingTask>
93-
94-
<PropertyGroup>
95-
<!-- Increment this when the BenchmarkDotNet.Weaver package needs to be re-packed. -->
96-
<WeaverVersionSuffix>-1</WeaverVersionSuffix>
97-
</PropertyGroup>
9899
</Project>

src/BenchmarkDotNet.Annotations/BenchmarkDotNet.Annotations.csproj

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@
1616
</ItemGroup>
1717

1818
<Choose>
19-
<When Condition="'$(IsFullPack)' == 'true'">
20-
<ItemGroup>
19+
<When Condition="'$(IsFullPack)' == 'true'">
20+
<ItemGroup>
2121
<!-- Include the BenchmarkDotNet.Weaver dlls in tasks without making it an explicit dependency. -->
2222
<Content Include="$(MSBuildThisFileDirectory)..\BenchmarkDotNet.Weaver\bin\Release\$(TargetFramework)\**\*.dll" Pack="true" PackagePath="tasks/$(TargetFramework)" />
2323
<Content Include="$(MSBuildThisFileDirectory)buildTransitive\**\*.targets" Pack="true" PackagePath="buildTransitive" />
24-
</ItemGroup>
25-
</When>
26-
<Otherwise>
27-
<!-- PackageReference for transitive weaver dependency for ProjectReferences to this. -->
28-
<ItemGroup>
29-
<PackageReference Include="BenchmarkDotNet.Weaver" Version="$(Version)$(WeaverVersionSuffix)" />
30-
</ItemGroup>
31-
</Otherwise>
24+
</ItemGroup>
25+
</When>
26+
<Otherwise>
27+
<!-- PackageReference for transitive weaver dependency for ProjectReferences to this. -->
28+
<ItemGroup>
29+
<PackageReference Include="BenchmarkDotNet.Weaver" Version="$(Version)$(WeaverVersionSuffix)" />
30+
</ItemGroup>
31+
</Otherwise>
3232
</Choose>
3333
</Project>

src/BenchmarkDotNet.Weaver/BenchmarkDotNet.Weaver.csproj

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ then run `build.cmd pack-weaver`.
66
<Project Sdk="Microsoft.NET.Sdk">
77
<Import Project="..\..\build\common.props" />
88
<PropertyGroup>
9-
<TargetFrameworks>netstandard2.0</TargetFrameworks>
10-
<VersionSuffix Condition="'$(IsFullPack)' != 'true'">$(VersionSuffix)$(WeaverVersionSuffix)</VersionSuffix>
11-
<OutputPath>$(MSBuildThisFileDirectory)bin\$(Configuration)</OutputPath>
12-
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
13-
<IncludeBuildOutput>false</IncludeBuildOutput>
14-
<NoWarn>$(NoWarn);NU5100;NU5128</NoWarn>
9+
<TargetFrameworks>netstandard2.0</TargetFrameworks>
10+
<VersionSuffix Condition="'$(IsFullPack)' != 'true'">$(VersionSuffix)$(WeaverVersionSuffix)</VersionSuffix>
11+
<OutputPath>$(MSBuildThisFileDirectory)bin\$(Configuration)</OutputPath>
12+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
13+
<IncludeBuildOutput>false</IncludeBuildOutput>
14+
<NoWarn>$(NoWarn);NU5100;NU5128</NoWarn>
1515
<!-- AsmResolver is not signed. -->
1616
<SignAssembly>false</SignAssembly>
1717
<DelaySign>false</DelaySign>
@@ -20,15 +20,15 @@ then run `build.cmd pack-weaver`.
2020
</PropertyGroup>
2121

2222
<ItemGroup>
23-
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-beta.3" PrivateAssets="all" />
24-
<PackageReference Include="Microsoft.Build.Framework" Version="17.14.8" PrivateAssets="all" />
25-
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.14.8" PrivateAssets="all" />
23+
<PackageReference Include="AsmResolver.DotNet" Version="6.0.0-beta.3" PrivateAssets="all" />
24+
<PackageReference Include="Microsoft.Build.Framework" Version="17.14.8" PrivateAssets="all" />
25+
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.14.8" PrivateAssets="all" />
2626
</ItemGroup>
2727

2828
<ItemGroup>
29-
<!-- Include .targets file and all DLLs in the output directory in the NuGet package -->
30-
<Content Include="$(MSBuildThisFileDirectory)buildTransitive\**\*.targets" Pack="true" PackagePath="buildTransitive" />
31-
<Content Include="$(OutputPath)**\*.dll" Pack="true" PackagePath="tasks/$(TargetFramework)" />
32-
<None Remove="packages\**" />
29+
<!-- Include .targets file and all DLLs in the output directory in the NuGet package -->
30+
<Content Include="$(MSBuildThisFileDirectory)buildTransitive\**\*.targets" Pack="true" PackagePath="buildTransitive" />
31+
<Content Include="$(OutputPath)**\*.dll" Pack="true" PackagePath="tasks/$(TargetFramework)" />
32+
<None Remove="packages\**" />
3333
</ItemGroup>
3434
</Project>
Binary file not shown.

src/BenchmarkDotNet.Weaver/src/WeaveAssemblyTask.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public override bool Execute()
6464

6565
if (benchmarkMethodsImplAdjusted)
6666
{
67-
module.Write(TargetAssembly);
67+
WriteChanges(module);
6868
}
6969
}
7070
catch (Exception e)
@@ -86,4 +86,56 @@ private static bool IsBenchmarkAttribute(CustomAttribute attribute)
8686
}
8787
return false;
8888
}
89+
90+
private void WriteChanges(ModuleDefinition module)
91+
{
92+
// Write to temp file, then overwrite the original file in case an exception occurs during the write (like unsupported platform).
93+
// https://github.com/Washi1337/AsmResolver/issues/640
94+
string tempFile = null;
95+
FileStream tempStream = null;
96+
try
97+
{
98+
// Attempting to avoid race conditions with parallel builds,
99+
// try to get exclusive access to a temp file, retrying with a new temp file if access fails.
100+
const int maxAttempts = 10;
101+
for (int attempt = 0; attempt < maxAttempts; ++attempt)
102+
{
103+
// Add a bit more randomness to the temp file name to reduce the chance of collisions.
104+
// Only use a few characters of the guid to reduce path length for Windows, increasing length by 1 on each attempt.
105+
var rand = Guid.NewGuid().ToString().Substring(0, attempt + 1);
106+
// Add a constant suffix to make it less likely to collide with other processes.
107+
tempFile = $"{Path.GetTempFileName()}{rand}bdnweave.dll";
108+
try
109+
{
110+
tempStream = new FileStream(tempFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
111+
break; // Got exclusive access
112+
}
113+
catch (IOException)
114+
{
115+
// Could not get exclusive access, try another temp file
116+
try { File.Delete(tempFile); } catch { }
117+
tempFile = null;
118+
}
119+
}
120+
121+
if (tempStream == null)
122+
throw new IOException("Failed to acquire exclusive access to a temp file after multiple attempts.");
123+
124+
// Write the module to the file stream
125+
module.Write(tempStream);
126+
tempStream.Flush();
127+
tempStream.Dispose();
128+
tempStream = null;
129+
130+
File.Copy(tempFile, TargetAssembly, overwrite: true);
131+
}
132+
finally
133+
{
134+
tempStream?.Dispose();
135+
if (tempFile != null)
136+
{
137+
try { File.Delete(tempFile); } catch { }
138+
}
139+
}
140+
}
89141
}

0 commit comments

Comments
 (0)