Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed infinite loop cycles when running dotnet watch. #3446

Merged
merged 4 commits into from
Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace StrawberryShake.CodeGeneration.CSharp.Analyzers
public class CSharpClientGenerator : ISourceGenerator
{
private const string _category = "StrawberryShakeGenerator";
private readonly MD5DocumentHashProvider _hashProvider = new();

private static string _location = System.IO.Path.GetDirectoryName(
typeof(CSharpClientGenerator).Assembly.Location)!;
Expand Down Expand Up @@ -94,8 +95,6 @@ private void Execute(ClientGeneratorContext context)
{
try
{
CreateDirectoryIfNotExists(context.OutputDirectory);

if (!TryGenerateClient(context, out CSharpGeneratorResult? result))
{
// there were unexpected errors and we will stop generating this client.
Expand Down Expand Up @@ -157,14 +156,13 @@ private void WriteGraphQLQuery(
string documentName = document.Hash + ".graphql";
string fileName = IOPath.Combine(persistedQueryDirectory, documentName);

context.Log.WriteDocument(documentName);

if (File.Exists(fileName))
// we only write the file if it does not exist to not trigger
// dotnet watch.
if (!File.Exists(fileName))
{
File.Delete(fileName);
context.Log.WriteDocument(documentName);
File.WriteAllText(fileName, document.SourceText, Encoding.UTF8);
}

File.WriteAllText(fileName, document.SourceText, Encoding.UTF8);
}

private void Clean(ClientGeneratorContext context)
Expand Down Expand Up @@ -362,14 +360,6 @@ private IReadOnlyList<string> GetGraphQLConfigFiles(
.Where(t => IOPath.GetFileName(t).EqualsOrdinal(".graphqlrc.json"))
.ToList();

private void CreateDirectoryIfNotExists(string? directory)
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
}

private ILogger CreateLogger(GeneratorExecutionContext context)
{
if (context.AnalyzerConfigOptions.GlobalOptions.TryGetValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using static StrawberryShake.CodeGeneration.ErrorHelper;
using static StrawberryShake.CodeGeneration.CSharp.Analyzers.SourceGeneratorErrorCodes;
using static StrawberryShake.CodeGeneration.CSharp.Analyzers.DiagnosticErrorHelper;
using System.IO;

namespace StrawberryShake.CodeGeneration.CSharp.Analyzers
{
Expand All @@ -28,7 +29,7 @@ public ClientGeneratorContext(
Filter = filter;
ClientDirectory = clientDirectory;
OutputDirectory = IOPath.Combine(
clientDirectory,
clientDirectory,
settings.OutputDirectoryName ?? ".generated");
OutputFiles = settings.OutputDirectoryName is not null;
_allDocuments = allDocuments;
Expand Down Expand Up @@ -127,5 +128,24 @@ public string GetNamespace()

return null;
}

public string? GetStateDirectory()
{
if (Execution.AnalyzerConfigOptions.GlobalOptions.TryGetValue(
"build_property.StrawberryShake_State",
out string? value) &&
!string.IsNullOrEmpty(value))
{
if (!Directory.Exists(value))
{
Directory.CreateDirectory(value);
}

return value;
}

throw new GraphQLException(
$"The MSBuild property `StrawberryShake_State` cannot be empty.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Text;
using HotChocolate.Language;
using IOPath = System.IO.Path;

namespace StrawberryShake.CodeGeneration.CSharp.Analyzers
{
public class SingleFileDocumentWriter : IDocumentWriter
{
private StringBuilder _content = new();
private readonly StringBuilder _content = new();
private readonly MD5DocumentHashProvider _hashProvider = new();
private GeneratorExecutionContext? _execution;
private string? _fileName;
private string? _hashFile;
private bool _emitCode;

public void WriteDocument(ClientGeneratorContext context, SourceDocument document)
Expand All @@ -28,6 +31,9 @@ public void WriteDocument(ClientGeneratorContext context, SourceDocument documen
_fileName = IOPath.Combine(
context.OutputDirectory,
$"{context.Settings.Name}.StrawberryShake.cs");
_hashFile = IOPath.Combine(
context.GetStateDirectory(),
context.Settings.Name + ".md5");
_emitCode = context.Settings.EmitGeneratedCode;
context.FileNames.Add(_fileName);
}
Expand All @@ -51,19 +57,34 @@ public void Flush()

if (_emitCode && _fileName is not null)
{
string directory = IOPath.GetDirectoryName(_fileName);
string? hash = _hashFile is not null && File.Exists(_hashFile)
? File.ReadAllText(_hashFile)
: null;

if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
string currentHash = _hashProvider.ComputeHash(
Encoding.UTF8.GetBytes(_content.ToString()));

if (File.Exists(_fileName))
bool fileExists = File.Exists(_fileName);

// we only write the file if it has changed so we do not trigger a loop on
// dotnet watch.
if (!fileExists || !currentHash.Equals(hash, StringComparison.Ordinal))
{
File.Delete(_fileName);
}
string directory = IOPath.GetDirectoryName(_fileName);

File.WriteAllText(_fileName, _content.ToString(), Encoding.UTF8);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}

if (fileExists)
{
File.Delete(_fileName);
}

File.WriteAllText(_fileName, _content.ToString(), Encoding.UTF8);
File.WriteAllText(_hashFile, currentHash, Encoding.UTF8);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="StrawberryShake.CodeGeneration.CSharp" Version="11.2.0-local.100" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="StrawberryShake.CodeGeneration.CSharp" Version="11.2.0-local-152" PrivateAssets="all" GeneratePathProperty="true" />
<PackageReference Include="DotNet.Glob" Version="3.1.2" PrivateAssets="all" GeneratePathProperty="true" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<CompilerVisibleProperty Include="StrawberryShake_LogFile" />
<CompilerVisibleProperty Include="StrawberryShake_BuildDirectory" />
<CompilerVisibleProperty Include="StrawberryShake_PersistedQueryDirectory" />
<CompilerVisibleProperty Include="StrawberryShake_State" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<StrawberryShake_DesignTimeBuild Condition="$(IsOmniSharp)">false</StrawberryShake_DesignTimeBuild>
<StrawberryShake_DefaultNamespace Condition="'$(StrawberryShake_DefaultNamespace)' == ''">$(RootNamespace)</StrawberryShake_DefaultNamespace>
<StrawberryShake_DefaultNamespace Condition="'$(StrawberryShake_DefaultNamespace)' == ''">$(AssemblyName)</StrawberryShake_DefaultNamespace>
<StrawberryShake_State>$(BaseIntermediateOutputPath)\berry</StrawberryShake_State>
</PropertyGroup>

<Import Project="$(MSBuildThisFileDirectory)VSCode.targets" Condition="'$(IsOmniSharp)' == 'true'" />
Expand Down
Loading