Skip to content

Commit 438a8e7

Browse files
stephentoubtarekgh
andauthored
Fix logging source generator CS0234 error with FormattableString namespace (#118691)
* Enable logging generator tests on netfx * Fix build error in the logging source generator tests when targeting netfx --------- Co-authored-by: Tarek Mahmoud Sayed <tarekms@microsoft.com>
1 parent 72fc822 commit 438a8e7

File tree

4 files changed

+18
-8
lines changed

4 files changed

+18
-8
lines changed

eng/Versions.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@
129129
<JsonSchemaNetVersion>7.0.2</JsonSchemaNetVersion>
130130
<NewtonsoftJsonVersion>13.0.3</NewtonsoftJsonVersion>
131131
<NewtonsoftJsonBsonVersion>1.0.2</NewtonsoftJsonBsonVersion>
132-
<SQLitePCLRawbundle_greenVersion>2.0.4</SQLitePCLRawbundle_greenVersion>
133132
<MoqVersion>4.18.4</MoqVersion>
134133
<AwesomeAssertionsVersion>8.0.2</AwesomeAssertionsVersion>
135134
<FsCheckVersion>2.14.3</FsCheckVersion>

src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Emitter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private void GenStruct(LoggerMethod lm, string nestedIndentation)
177177
string formatMethodBegin =
178178
!lm.Message.Contains('{') ? "" :
179179
_hasStringCreate ? "string.Create(global::System.Globalization.CultureInfo.InvariantCulture, " :
180-
"global::System.Diagnostics.CodeAnalysis.FormattableString.Invariant(";
180+
"global::System.FormattableString.Invariant(";
181181
string formatMethodEnd = formatMethodBegin.Length > 0 ? ")" : "";
182182

183183
_builder.Append($@"

src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/LoggerMessageGeneratorEmitterTests.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.IO;
6+
using System.Linq;
67
using System.Reflection;
78
using System.Threading.Tasks;
89
using SourceGenerators.Tests;
@@ -20,7 +21,7 @@ public async Task TestEmitter()
2021
string[] sources = Directory.GetFiles("TestClasses");
2122
foreach (var src in sources)
2223
{
23-
var testSourceCode = await File.ReadAllTextAsync(src).ConfigureAwait(false);
24+
var testSourceCode = File.ReadAllText(src);
2425

2526
var (d, r) = await RoslynTestUtils.RunGenerator(
2627
new LoggerMessageGenerator(),
@@ -253,9 +254,9 @@ public void GenericTypeParameterAttributesAreRetained()
253254

254255
private async Task VerifyAgainstBaselineUsingFile(string filename, string testSourceCode)
255256
{
256-
string baseline = LineEndingsHelper.Normalize(await File.ReadAllTextAsync(Path.Combine("Baselines", filename)).ConfigureAwait(false));
257+
string baseline = LineEndingsHelper.Normalize(File.ReadAllText(Path.Combine("Baselines", filename)));
257258
string[] expectedLines = baseline.Replace("%VERSION%", typeof(LoggerMessageGenerator).Assembly.GetName().Version?.ToString())
258-
.Split(Environment.NewLine);
259+
.Split([Environment.NewLine], StringSplitOptions.None);
259260

260261
var (d, r) = await RoslynTestUtils.RunGenerator(
261262
new LoggerMessageGenerator(),
@@ -265,6 +266,13 @@ private async Task VerifyAgainstBaselineUsingFile(string filename, string testSo
265266
Assert.Empty(d);
266267
Assert.Single(r);
267268

269+
if (PlatformDetection.IsNetFramework)
270+
{
271+
expectedLines = expectedLines.Select(line => line.Replace(
272+
"string.Create(global::System.Globalization.CultureInfo.InvariantCulture, ",
273+
"global::System.FormattableString.Invariant(")).ToArray();
274+
}
275+
268276
Assert.True(RoslynTestUtils.CompareLines(expectedLines, r[0].SourceText,
269277
out string errorMessage), errorMessage);
270278
}

src/libraries/Microsoft.Extensions.Logging.Abstractions/tests/Microsoft.Extensions.Logging.Generators.Tests/Microsoft.Extensions.Logging.Generators.targets

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project>
22

33
<PropertyGroup>
4-
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
4+
<TargetFrameworks>$(NetCoreAppCurrent);$(NetFrameworkCurrent)</TargetFrameworks>
55
<EnableDefaultItems>true</EnableDefaultItems>
66
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
77
<Nullable>enable</Nullable>
8-
<IgnoreForCI Condition="'$(TargetsMobile)' == 'true' or '$(TargetsLinuxBionic)' == 'true' or '$(TargetArchitecture)' == 'ARMv6'">true</IgnoreForCI>
8+
<IgnoreForCI Condition="'$(TargetsMobile)' == 'true' or '$(TargetsLinuxBionic)' == 'true' or '$(TargetArchitecture)' == 'ARMv6'">true</IgnoreForCI>
99
<!-- SQLitePCLRaw currently has non-portable RIDs (alpine). Update to newer version once it released.
1010
https://github.com/ericsink/SQLitePCL.raw/issues/543 -->
1111
<NoWarn>$(NoWarn);NETSDK1206</NoWarn>
@@ -16,10 +16,13 @@
1616
Link="SourceGenerators\RoslynTestUtils.cs" />
1717
</ItemGroup>
1818

19+
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
20+
<Compile Include="$(CoreLibSharedDir)System\Runtime\CompilerServices\IsExternalInit.cs" />
21+
</ItemGroup>
22+
1923
<ItemGroup>
2024
<ProjectReference Include="$(LibrariesProjectRoot)Microsoft.Extensions.Logging.Abstractions\src\Microsoft.Extensions.Logging.Abstractions.csproj" />
2125
<PackageReference Include="Microsoft.CodeAnalysis" Version="$(RoslynApiVersion)" />
22-
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="$(SQLitePCLRawbundle_greenVersion)" />
2326
</ItemGroup>
2427

2528
<ItemGroup>

0 commit comments

Comments
 (0)