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

fix: Correctly support incremental source generation #62

Merged
merged 18 commits into from
Oct 16, 2024
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
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal -f net6.0
run: dotnet test --no-restore --verbosity normal -f net8.0
- name: Set Version
if: startsWith(github.ref, 'refs/tags')
run: |
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: '6.0.x'
dotnet-version: '8.0.x'
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal -f net6.0
run: dotnet test --no-restore --verbosity normal -f net8.0
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ obj
*.DotSettings.user
.vs
.DS_Store

Generator.Equals.Tests/Generated/**
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<IsPackable>false</IsPackable>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzer.Testing" Version="1.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.3.1" PrivateAssets="all" />
<PackageReference Include="FluentAssertions" Version="6.8.0" />
<PackageReference Include="Verify.ClipboardAccept" Version="19.5.0" />
<PackageReference Include="Verify.SourceGenerators" Version="2.0.1" />
<PackageReference Include="Verify.Xunit" Version="19.5.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
Expand Down
24 changes: 18 additions & 6 deletions Generator.Equals.Tests/Generator.Equals.Tests.csproj
diegofrata marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;net48</TargetFrameworks>
<TargetFrameworks>net8.0;net48</TargetFrameworks>
<IsPackable>false</IsPackable>
<ImplicitUsings>disable</ImplicitUsings>
<NoWarn>NU1701</NoWarn>
<Nullable>enable</Nullable>
</PropertyGroup>


<!-- For debugging -->
JKamsker marked this conversation as resolved.
Show resolved Hide resolved
<!--
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(MSBuildProjectDirectory)\Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Generated\**" />
<None Include="Generated\**" />
</ItemGroup>
-->

<ItemGroup>

<PackageReference Include="Microsoft.Bcl.HashCode" Version="1.1.1" Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'" />
<PackageReference Include="FluentAssertions" Version="6.8.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.1" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.2.0">
<PackageReference Include="coverlet.collector" Version="6.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
2 changes: 1 addition & 1 deletion Generator.Equals.Tests/Structs/UnorderedEquality.Sample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ public partial struct Sample
[UnorderedEquality] public List<int>? Properties { get; set; }
}
}
}
}
4 changes: 2 additions & 2 deletions Generator.Equals.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generator.Equals.Tests", "G
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generator.Equals.SnapshotTests", "Generator.Equals.SnapshotTests\Generator.Equals.SnapshotTests.csproj", "{F11B4229-E761-4135-B758-E61AC4FF5303}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Generator.Equals.Tests.TopLevel", "Generator.Equals.Tests.TopLevel\Generator.Equals.Tests.TopLevel.csproj", "{9A48A8A6-4213-4CBB-B184-FE1E50F3721E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generator.Equals.Tests.TopLevel", "Generator.Equals.Tests.TopLevel\Generator.Equals.Tests.TopLevel.csproj", "{9A48A8A6-4213-4CBB-B184-FE1E50F3721E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Generator.Equals.Runtime", "Generator.Equals.Runtime\Generator.Equals.Runtime.csproj", "{2AD99F42-5E2C-451A-97EE-59C64EC8B270}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Generator.Equals.Runtime", "Generator.Equals.Runtime\Generator.Equals.Runtime.csproj", "{2AD99F42-5E2C-451A-97EE-59C64EC8B270}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
43 changes: 0 additions & 43 deletions Generator.Equals/AttributesMetadata.cs

This file was deleted.

69 changes: 16 additions & 53 deletions Generator.Equals/ContainingTypesBuilder.cs

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opening this file in VS 2022 comes up with 9 warnings. These should all be addressed.

Original file line number Diff line number Diff line change
@@ -1,88 +1,51 @@
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;

using Generator.Equals.Models;

using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace Generator.Equals
{
static class ContainingTypesBuilder
internal static class ContainingTypesBuilder
{
static IEnumerable<INamespaceOrTypeSymbol> ContainingNamespaceAndTypes(ISymbol symbol, bool includeSelf)
{
foreach (var item in AllContainingNamespacesAndTypes(symbol, includeSelf))
{
yield return item;

if (item.IsNamespace)
yield break;
}
}

static IEnumerable<INamespaceOrTypeSymbol> AllContainingNamespacesAndTypes(ISymbol symbol, bool includeSelf)
public static string Build(ImmutableArray<ContainingSymbol> containingSymbols, Action<IndentedTextWriter> content)
{
if (includeSelf && symbol is INamespaceOrTypeSymbol self)
yield return self;
using var buffer = new StringWriter(new StringBuilder(capacity: 4096));
using var writer = new IndentedTextWriter(buffer);

while (true)
foreach (var parentSymbol in containingSymbols.Reverse())
{
symbol = symbol.ContainingSymbol;

if (!(symbol is INamespaceOrTypeSymbol namespaceOrTypeSymbol))
yield break;

yield return namespaceOrTypeSymbol;
}
}

public static string Build(ISymbol symbol, Action<IndentedTextWriter> content, bool includeSelf = false)
{
var buffer = new StringWriter(new StringBuilder(capacity: 4096));
var writer = new IndentedTextWriter(buffer);
var symbols = ContainingNamespaceAndTypes(symbol, includeSelf).ToList();

for (var i = symbols.Count - 1; i >= 0; i--)
{
var s = symbols[i];

if (s.IsNamespace)
if (parentSymbol is NamespaceContainingSymbol namespaceSymbol)
{
writer.WriteLine();
writer.WriteLine(EqualityGeneratorBase.EnableNullableContext);
writer.WriteLine(EqualityGeneratorBase.SuppressObsoleteWarningsPragma);
writer.WriteLine(EqualityGeneratorBase.SuppressTypeConflictsWarningsPragma);
writer.WriteLine();

var namespaceName = s.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat.WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted));

if (!string.IsNullOrEmpty(namespaceName))
if (!string.IsNullOrEmpty(namespaceSymbol.Name))
{
writer.WriteLine($"namespace {namespaceName}");
writer.WriteLine($"namespace {namespaceSymbol.Name}");
writer.AppendOpenBracket();
}
}
else
else if (parentSymbol is TypeContainingSymbol typeContainingSymbol)
{
var typeDeclarationSyntax = s.DeclaringSyntaxReferences
.Select(x => x.GetSyntax())
.OfType<TypeDeclarationSyntax>()
.First();

var keyword = typeDeclarationSyntax.Kind() switch
var keyword = typeContainingSymbol.Kind switch

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(SyntaxKind)9068 can be updated to SyntaxKind.RecordStructDeclaration

{
SyntaxKind.ClassDeclaration => "class",
SyntaxKind.RecordDeclaration => "record",
(SyntaxKind)9068 => "record struct", // RecordStructDeclaration
SyntaxKind.RecordStructDeclaration => "record struct",
SyntaxKind.StructDeclaration => "struct",
var x => throw new ArgumentOutOfRangeException($"Syntax kind {x} not supported")
};

var typeName = s.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat);
writer.WriteLine($"partial {keyword} {typeName}");
writer.WriteLine($"partial {keyword} {parentSymbol.Name}");
writer.AppendOpenBracket();
}
}
Expand Down
Loading
Loading