Skip to content

Commit

Permalink
Merge release/dev17.13 to main (#76387)
Browse files Browse the repository at this point in the history
This is an automatically generated pull request from release/dev17.13
into main.


Once all conflicts are resolved and all the tests pass, you are free to
merge the pull request. 🐯

## Troubleshooting conflicts

### Identify authors of changes which introduced merge conflicts
Scroll to the bottom, then for each file containing conflicts copy its
path into the following searches:
- https://github.com/dotnet/roslyn/find/release/dev17.13
- https://github.com/dotnet/roslyn/find/main

Usually the most recent change to a file between the two branches is
considered to have introduced the conflicts, but sometimes it will be
necessary to look for the conflicting lines and check the blame in each
branch. Generally the author whose change introduced the conflicts
should pull down this PR, fix the conflicts locally, then push up a
commit resolving the conflicts.

### Resolve merge conflicts using your local repo
Sometimes merge conflicts may be present on GitHub but merging locally
will work without conflicts. This is due to differences between the
merge algorithm used in local git versus the one used by GitHub.
``` bash
git fetch --all
git checkout -t upstream/merges/release/dev17.13-to-main
git reset --hard upstream/main
git merge upstream/release/dev17.13
# Fix merge conflicts
git commit
git push upstream merges/release/dev17.13-to-main --force
```
  • Loading branch information
dotnet-bot authored Dec 19, 2024
2 parents c07f28f + e5f5519 commit 074b1f3
Show file tree
Hide file tree
Showing 18 changed files with 526 additions and 124 deletions.
4 changes: 2 additions & 2 deletions azure-pipelines-official.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ extends:
displayName: Build and Test

jobs:
- ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/release/dev17.12') }}:
- ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/release/dev17.13') }}:
- template: /eng/common/templates-official/job/onelocbuild.yml@self
parameters:
MirrorRepo: roslyn
MirrorBranch: release/dev17.12
MirrorBranch: release/dev17.13
LclSource: lclFilesfromPackage
LclPackageId: 'LCL-JUNO-PROD-ROSLYN'

Expand Down

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions src/Features/TestUtilities/Options/NamingStyleTestUtilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Xml.Linq;
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.NamingStyles;

namespace Microsoft.CodeAnalysis.Test.Utilities;

internal static class NamingStyleTestUtilities
{
public static string Inspect(this NamingRule rule)
=> $"{rule.NamingStyle.Inspect()} {rule.SymbolSpecification.Inspect()} {rule.EnforcementLevel}";

public static string Inspect(this NamingStyle style)
=> $"{style.Name} prefix='{style.Prefix}' suffix='{style.Suffix}' separator='{style.WordSeparator}'";

public static string Inspect(this SymbolSpecification symbol)
=> $"{symbol.Name} {Inspect(symbol.ApplicableSymbolKindList)} {Inspect(symbol.ApplicableAccessibilityList)} {Inspect(symbol.RequiredModifierList)}";

public static string Inspect<T>(ImmutableArray<T> items) where T : notnull
=> string.Join(",", items.Select(item => item.ToString()));

public static string Inspect(this NamingStylePreferences preferences, string[]? excludeNodes = null)
{
var xml = preferences.CreateXElement();

// filter out insignificant elements:
var elementsToRemove = new List<XElement>();
foreach (var element in xml.DescendantsAndSelf())
{
if (excludeNodes != null && excludeNodes.Contains(element.Name.LocalName))
{
elementsToRemove.Add(element);
}
}

foreach (var element in elementsToRemove)
{
element.Remove();
}

// replaces GUIDs with unique deterministic numbers:
var ordinal = 0;
var guidMap = new Dictionary<Guid, int>();
foreach (var element in xml.DescendantsAndSelf())
{
foreach (var attribute in element.Attributes())
{
if (Guid.TryParse(attribute.Value, out var guid))
{
if (!guidMap.TryGetValue(guid, out var existingOrdinal))
{
existingOrdinal = ordinal++;
guidMap.Add(guid, existingOrdinal);
}

attribute.Value = existingOrdinal.ToString();
}
}
}

return xml.ToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
using System.Composition;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.ErrorReporting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Host.Mef;
Expand Down Expand Up @@ -86,8 +86,21 @@ Solution UpdateOptions(Solution oldSolution)

// update changed values:
var configName = key.Option.Definition.ConfigName;
var configValue = key.Option.Definition.Serializer.Serialize(value);
lazyBuilder[configName] = configValue;
if (value is NamingStylePreferences preferences)
{
NamingStylePreferencesEditorConfigSerializer.WriteNamingStylePreferencesToEditorConfig(
preferences.SymbolSpecifications,
preferences.NamingStyles,
preferences.NamingRules,
language,
entryWriter: (name, value) => lazyBuilder[name] = value,
triviaWriter: null,
setPrioritiesToPreserveOrder: true);
}
else
{
lazyBuilder[configName] = key.Option.Definition.Serializer.Serialize(value);
}
}

if (lazyBuilder != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Utilities;
using Microsoft.CodeAnalysis.UnitTests;
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Options.UnitTests;
Expand Down Expand Up @@ -95,6 +101,154 @@ void AssertOptionValue(IOption2 option, string language, string expectedValue)
}
}

[Fact]
[WorkItem("https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2297536")]
public void FlowsNamingStylePreferencesToWorkspace()
{
using var workspace = CreateWorkspace();

var testProjectWithoutConfig = new TestHostProject(workspace, "proj_without_config", LanguageNames.CSharp);

testProjectWithoutConfig.AddDocument(new TestHostDocument("""
class MyClass1;
""",
filePath: Path.Combine(TempRoot.Root, "proj_without_config", "test.cs")));

var testProjectWithConfig = new TestHostProject(workspace, "proj_with_config", LanguageNames.CSharp);

// explicitly specified style should override style specified in the fallback:
testProjectWithConfig.AddAnalyzerConfigDocument(new TestHostDocument(
"""
[*.cs]
dotnet_naming_rule.rule1.severity = warning
dotnet_naming_rule.rule1.symbols = symbols1
dotnet_naming_rule.rule1.style = style1
dotnet_naming_symbols.symbols1.applicable_kinds = class
dotnet_naming_symbols.symbols1.applicable_accessibilities = *
dotnet_naming_style.style1.capitalization = camel_case
""",
filePath: Path.Combine(TempRoot.Root, "proj_with_config", ".editorconfig")));

testProjectWithConfig.AddDocument(new TestHostDocument("""
class MyClass2;
""",
filePath: Path.Combine(TempRoot.Root, "proj_with_config", "test.cs")));

workspace.AddTestProject(testProjectWithoutConfig);
workspace.AddTestProject(testProjectWithConfig);

var globalOptions = workspace.GetService<IGlobalOptionService>();

var hostPeferences = OptionsTestHelpers.CreateNamingStylePreferences(
([MethodKind.Ordinary], Capitalization.PascalCase, ReportDiagnostic.Error),
([MethodKind.Ordinary, SymbolKind.Field], Capitalization.PascalCase, ReportDiagnostic.Error));

globalOptions.SetGlobalOption(NamingStyleOptions.NamingPreferences, LanguageNames.CSharp, hostPeferences);

Assert.True(workspace.CurrentSolution.FallbackAnalyzerOptions.TryGetValue(LanguageNames.CSharp, out var fallbackOptions));

// Note: rules are ordered but symbol and naming style specifications are not.
AssertEx.Equal(
hostPeferences.Rules.NamingRules.Select(r => r.Inspect()),
fallbackOptions.GetNamingStylePreferences().Rules.NamingRules.Select(r => r.Inspect()));

var projectWithConfig = workspace.CurrentSolution.GetRequiredProject(testProjectWithConfig.Id);
var treeWithConfig = projectWithConfig.Documents.Single().GetSyntaxTreeSynchronously(CancellationToken.None);
Assert.NotNull(treeWithConfig);
var documentOptions = projectWithConfig.HostAnalyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(treeWithConfig);

Assert.True(documentOptions.TryGetEditorConfigOption<NamingStylePreferences>(NamingStyleOptions.NamingPreferences, out var documentPreferences));
Assert.NotNull(documentPreferences);

// Only naming styles specified in the editorconfig are present.
// Host preferences are ignored. This behavior is consistent with VS 16.11.
AssertEx.EqualOrDiff("""
<NamingPreferencesInfo SerializationVersion="5">
<SymbolSpecifications>
<SymbolSpecification ID="0" Name="symbols1">
<ApplicableSymbolKindList>
<TypeKind>Class</TypeKind>
</ApplicableSymbolKindList>
<ApplicableAccessibilityList>
<AccessibilityKind>NotApplicable</AccessibilityKind>
<AccessibilityKind>Public</AccessibilityKind>
<AccessibilityKind>Internal</AccessibilityKind>
<AccessibilityKind>Private</AccessibilityKind>
<AccessibilityKind>Protected</AccessibilityKind>
<AccessibilityKind>ProtectedAndInternal</AccessibilityKind>
<AccessibilityKind>ProtectedOrInternal</AccessibilityKind>
</ApplicableAccessibilityList>
<RequiredModifierList />
</SymbolSpecification>
</SymbolSpecifications>
<NamingStyles>
<NamingStyle ID="1" Name="style1" Prefix="" Suffix="" WordSeparator="" CapitalizationScheme="CamelCase" />
</NamingStyles>
<NamingRules>
<SerializableNamingRule SymbolSpecificationID="0" NamingStyleID="1" EnforcementLevel="Warning" />
</NamingRules>
</NamingPreferencesInfo>
""",
documentPreferences.Inspect());

var projectWithoutConfig = workspace.CurrentSolution.GetRequiredProject(testProjectWithoutConfig.Id);
var treeWithoutConfig = projectWithoutConfig.Documents.Single().GetSyntaxTreeSynchronously(CancellationToken.None);
Assert.NotNull(treeWithoutConfig);
documentOptions = projectWithoutConfig.HostAnalyzerOptions.AnalyzerConfigOptionsProvider.GetOptions(treeWithoutConfig);

Assert.True(documentOptions.TryGetEditorConfigOption(NamingStyleOptions.NamingPreferences, out documentPreferences));
Assert.NotNull(documentPreferences);

// Host preferences:
AssertEx.EqualOrDiff("""
<NamingPreferencesInfo SerializationVersion="5">
<SymbolSpecifications>
<SymbolSpecification ID="0" Name="symbols1">
<ApplicableSymbolKindList>
<MethodKind>Ordinary</MethodKind>
<SymbolKind>Field</SymbolKind>
</ApplicableSymbolKindList>
<ApplicableAccessibilityList>
<AccessibilityKind>NotApplicable</AccessibilityKind>
<AccessibilityKind>Public</AccessibilityKind>
<AccessibilityKind>Internal</AccessibilityKind>
<AccessibilityKind>Private</AccessibilityKind>
<AccessibilityKind>Protected</AccessibilityKind>
<AccessibilityKind>ProtectedAndInternal</AccessibilityKind>
<AccessibilityKind>ProtectedOrInternal</AccessibilityKind>
</ApplicableAccessibilityList>
<RequiredModifierList />
</SymbolSpecification>
<SymbolSpecification ID="1" Name="symbols0">
<ApplicableSymbolKindList>
<MethodKind>Ordinary</MethodKind>
</ApplicableSymbolKindList>
<ApplicableAccessibilityList>
<AccessibilityKind>NotApplicable</AccessibilityKind>
<AccessibilityKind>Public</AccessibilityKind>
<AccessibilityKind>Internal</AccessibilityKind>
<AccessibilityKind>Private</AccessibilityKind>
<AccessibilityKind>Protected</AccessibilityKind>
<AccessibilityKind>ProtectedAndInternal</AccessibilityKind>
<AccessibilityKind>ProtectedOrInternal</AccessibilityKind>
</ApplicableAccessibilityList>
<RequiredModifierList />
</SymbolSpecification>
</SymbolSpecifications>
<NamingStyles>
<NamingStyle ID="2" Name="style1" Prefix="" Suffix="" WordSeparator="" CapitalizationScheme="PascalCase" />
<NamingStyle ID="3" Name="style0" Prefix="" Suffix="" WordSeparator="" CapitalizationScheme="PascalCase" />
</NamingStyles>
<NamingRules>
<SerializableNamingRule SymbolSpecificationID="1" NamingStyleID="3" EnforcementLevel="Error" />
<SerializableNamingRule SymbolSpecificationID="0" NamingStyleID="2" EnforcementLevel="Error" />
</NamingRules>
</NamingPreferencesInfo>
""",
documentPreferences.Inspect());
}

[Fact]
public void IgnoresNonEditorConfigOptions()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,18 @@ public void LanguageSpecificOptionsHaveCorrectPrefix(string configName)
return;
}

if (!info.Option.Definition.IsEditorConfigOption)
// TODO: https://github.com/dotnet/roslyn/issues/65787
if (info.Option.Name is
"csharp_format_on_return" or
"csharp_format_on_typing" or
"csharp_format_on_semicolon" or
"csharp_format_on_close_brace" or
"csharp_enable_inlay_hints_for_types" or
"csharp_enable_inlay_hints_for_implicit_variable_types" or
"csharp_enable_inlay_hints_for_lambda_parameter_types" or
"csharp_enable_inlay_hints_for_implicit_object_creation" or
"csharp_enable_inlay_hints_for_collection_expressions")
{
// TODO: remove condition once all options have config name https://github.com/dotnet/roslyn/issues/65787
return;
}

Expand Down
67 changes: 44 additions & 23 deletions src/Workspaces/CoreTestUtilities/Options/OptionsTestHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Collections.Immutable;
using Microsoft.CodeAnalysis.CodeStyle;
using Microsoft.CodeAnalysis.CSharp.Formatting;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Diagnostics.Analyzers.NamingStyles;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.NamingStyles;
Expand Down Expand Up @@ -147,33 +148,53 @@ private static object GetDifferentEnumValue(Type type, object defaultValue)
}

public static NamingStylePreferences GetNonDefaultNamingStylePreference()
=> CreateNamingStylePreferences(([TypeKind.Class], Capitalization.PascalCase, ReportDiagnostic.Error));

public static NamingStylePreferences CreateNamingStylePreferences(
params (SymbolSpecification.SymbolKindOrTypeKind[], Capitalization capitalization, ReportDiagnostic severity)[] rules)
{
var symbolSpecification = new SymbolSpecification(
Guid.NewGuid(),
"Name",
ImmutableArray.Create(new SymbolSpecification.SymbolKindOrTypeKind(TypeKind.Class)),
accessibilityList: default,
modifiers: default);

var namingStyle = new NamingStyle(
Guid.NewGuid(),
capitalizationScheme: Capitalization.PascalCase,
name: "Name",
prefix: "",
suffix: "",
wordSeparator: "");

var namingRule = new SerializableNamingRule()
var symbolSpecifications = new List<SymbolSpecification>();
var namingStyles = new List<NamingStyle>();
var namingRules = new List<SerializableNamingRule>();

foreach (var (kinds, capitalization, severity) in rules)
{
SymbolSpecificationID = symbolSpecification.ID,
NamingStyleID = namingStyle.ID,
EnforcementLevel = ReportDiagnostic.Error
};
var id = namingRules.Count;

var symbolSpecification = new SymbolSpecification(
Guid.NewGuid(),
name: $"symbols{id}",
[.. kinds],
accessibilityList: default,
modifiers: default);

symbolSpecifications.Add(symbolSpecification);

var namingStyle = new NamingStyle(
Guid.NewGuid(),
capitalizationScheme: capitalization,
name: $"style{id}",
prefix: "",
suffix: "",
wordSeparator: "");

namingStyles.Add(namingStyle);

namingRules.Add(new SerializableNamingRule()
{
SymbolSpecificationID = symbolSpecification.ID,
NamingStyleID = namingStyle.ID,
EnforcementLevel = severity
});
}

return new NamingStylePreferences(
ImmutableArray.Create(symbolSpecification),
ImmutableArray.Create(namingStyle),
ImmutableArray.Create(namingRule));
[.. symbolSpecifications],
[.. namingStyles],
[.. namingRules]);
}

public static NamingStylePreferences ParseNamingStylePreferences(Dictionary<string, string> options)
=> EditorConfigNamingStyleParser.ParseDictionary(new DictionaryAnalyzerConfigOptions(options.ToImmutableDictionary()));
}
}
Loading

0 comments on commit 074b1f3

Please sign in to comment.