Skip to content

Commit

Permalink
Update FunctionCallGenerator.cs to address race condition (#3758)
Browse files Browse the repository at this point in the history
Update FunctionCallGenerator.cs to address file name race condition with simultaneous builds
  • Loading branch information
maxgolov authored Oct 11, 2024
1 parent e5e3f11 commit c48d0d7
Showing 1 changed file with 25 additions and 17 deletions.
42 changes: 25 additions & 17 deletions dotnet/src/AutoGen.SourceGenerator/FunctionCallGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// FunctionCallGenerator.cs

using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Xml.Linq;
Expand Down Expand Up @@ -100,7 +101,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
};
var functionSource = functionTT.TransformText();
var fileName = $"{className}.generated.cs";
// Avoid conflict with filename for parallel builds targeting several .NET versions
// at once. Without unique filenames, the build will fail with the 'IOException'
// with message 'The process cannot access the file '%TEMP%\{className}.generated.cs'
var fileName = $"{className}_{System.Guid.NewGuid()}.generated.cs";
ctx.AddSource(fileName, SourceText.From(functionSource, System.Text.Encoding.UTF8));
File.WriteAllText(Path.Combine(Path.GetTempPath(), fileName), functionSource);
Expand All @@ -110,25 +114,29 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
{
var overallFunctionDefinition = source.Right.SelectMany(x => x!.FunctionContracts.Select(y => new { fullClassName = x.FullClassName, y = y }));
var overallFunctionDefinitionObject = overallFunctionDefinition.Select(
x => new
x =>
{
fullClassName = x.fullClassName,
functionDefinition = new
Debug.Assert(x.y.Parameters != null, "x.y.Parameters != null");
return new
{
x.y.Name,
x.y.Description,
x.y.ReturnType,
Parameters = x.y.Parameters.Select(y => new
fullClassName = x.fullClassName,
functionDefinition = new
{
y.Name,
y.Description,
y.JsonType,
y.JsonItemType,
y.Type,
y.IsOptional,
y.DefaultValue,
}),
},
x.y.Name,
x.y.Description,
x.y.ReturnType,
Parameters = x.y.Parameters.Select(y => new
{
y.Name,
y.Description,
y.JsonType,
y.JsonItemType,
y.Type,
y.IsOptional,
y.DefaultValue,
}),
},
};
});
var json = JsonConvert.SerializeObject(overallFunctionDefinitionObject, formatting: Formatting.Indented);
Expand Down

0 comments on commit c48d0d7

Please sign in to comment.