Skip to content

Commit a9c4ff1

Browse files
authored
fix issues with duplicated editorconfig sections, handle failures parsing editorconfigs (#993)
closes #989
1 parent 93baf5a commit a9c4ff1

File tree

3 files changed

+80
-8
lines changed

3 files changed

+80
-8
lines changed

Src/CSharpier.Cli/EditorConfig/ConfigFileParser.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ internal static class ConfigFileParser
1313
private static readonly Regex CommentRegex = new("^[;#].*$");
1414

1515
private static readonly IniParserConfiguration Configuration =
16-
new() { CommentRegex = CommentRegex, AllowDuplicateKeys = true };
16+
new()
17+
{
18+
CommentRegex = CommentRegex,
19+
AllowDuplicateKeys = true,
20+
AllowDuplicateSections = true,
21+
OverrideDuplicateKeys = true
22+
};
1723

1824
public static ConfigFile Parse(string filePath, IFileSystem fileSystem)
1925
{

Src/CSharpier.Cli/Options/OptionsProvider.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,20 @@ namespace CSharpier.Cli.Options;
33
using System.IO.Abstractions;
44
using System.Text.Json;
55
using CSharpier.Cli.EditorConfig;
6+
using Microsoft.CodeAnalysis.CSharp.Syntax;
67
using Microsoft.Extensions.Logging;
78
using PrinterOptions = CSharpier.PrinterOptions;
89

910
internal class OptionsProvider
1011
{
11-
private readonly List<EditorConfigSections> editorConfigs;
12+
private readonly IList<EditorConfigSections> editorConfigs;
1213
private readonly List<CSharpierConfigData> csharpierConfigs;
1314
private readonly IgnoreFile ignoreFile;
1415
private readonly PrinterOptions? specifiedPrinterOptions;
1516
private readonly IFileSystem fileSystem;
1617

1718
private OptionsProvider(
18-
List<EditorConfigSections> editorConfigs,
19+
IList<EditorConfigSections> editorConfigs,
1920
List<CSharpierConfigData> csharpierConfigs,
2021
IgnoreFile ignoreFile,
2122
PrinterOptions? specifiedPrinterOptions,
@@ -45,14 +46,24 @@ CancellationToken cancellationToken
4546
? ConfigurationFileOptions.FindForDirectoryName(directoryName, fileSystem, logger)
4647
: Array.Empty<CSharpierConfigData>().ToList();
4748

48-
var editorConfigSections = EditorConfigParser.FindForDirectoryName(
49-
directoryName,
50-
fileSystem
51-
);
49+
IList<EditorConfigSections>? editorConfigSections = null;
50+
51+
try
52+
{
53+
editorConfigSections = EditorConfigParser.FindForDirectoryName(
54+
directoryName,
55+
fileSystem
56+
);
57+
}
58+
catch (Exception ex)
59+
{
60+
logger.LogError(ex, $"Failure parsing editorconfig files for {directoryName}");
61+
}
62+
5263
var ignoreFile = await IgnoreFile.Create(directoryName, fileSystem, cancellationToken);
5364

5465
return new OptionsProvider(
55-
editorConfigSections,
66+
editorConfigSections ?? Array.Empty<EditorConfigSections>(),
5667
csharpierConfigs,
5768
ignoreFile,
5869
specifiedPrinterOptions,

Src/CSharpier.Tests/OptionsProviderTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,61 @@ public async Task Should_Support_EditorConfig_With_Comments()
244244
result.EndOfLine.Should().Be(EndOfLine.CRLF);
245245
}
246246

247+
[Test]
248+
public async Task Should_Support_EditorConfig_With_Duplicated_Sections()
249+
{
250+
var context = new TestContext();
251+
context.WhenAFileExists(
252+
"c:/test/.editorconfig",
253+
@"
254+
[*]
255+
indent_size = 2
256+
257+
[*]
258+
indent_size = 4
259+
"
260+
);
261+
262+
var result = await context.CreateProviderAndGetOptionsFor("c:/test", "c:/test/test.cs");
263+
264+
result.TabWidth.Should().Be(4);
265+
}
266+
267+
[Test]
268+
public async Task Should_Support_EditorConfig_With_Duplicated_Rules()
269+
{
270+
var context = new TestContext();
271+
context.WhenAFileExists(
272+
"c:/test/.editorconfig",
273+
@"
274+
[*]
275+
indent_size = 2
276+
indent_size = 4
277+
"
278+
);
279+
280+
var result = await context.CreateProviderAndGetOptionsFor("c:/test", "c:/test/test.cs");
281+
282+
result.TabWidth.Should().Be(4);
283+
}
284+
285+
[Test]
286+
public async Task Should_Not_Fail_With_Bad_EditorConfig()
287+
{
288+
var context = new TestContext();
289+
context.WhenAFileExists(
290+
"c:/test/.editorconfig",
291+
@"
292+
[*
293+
indent_size==
294+
"
295+
);
296+
297+
var result = await context.CreateProviderAndGetOptionsFor("c:/test", "c:/test/test.cs");
298+
299+
result.TabWidth.Should().Be(4);
300+
}
301+
247302
[TestCase("tab_width")]
248303
[TestCase("indent_size")]
249304
public async Task Should_Support_EditorConfig_Tabs(string propertyName)

0 commit comments

Comments
 (0)