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 updating solution fallback options #75417

Merged
merged 1 commit into from
Oct 10, 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
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,17 @@ Solution UpdateOptions(Solution oldSolution)
continue;
}

lazyBuilder ??= ImmutableDictionary.CreateBuilder<string, string>(AnalyzerConfigOptions.KeyComparer);

// copy existing option values:
foreach (var oldKey in languageOptions.Keys)
if (lazyBuilder == null)
{
if (languageOptions.TryGetValue(oldKey, out var oldValue))
JoeRobich marked this conversation as resolved.
Show resolved Hide resolved
lazyBuilder = ImmutableDictionary.CreateBuilder<string, string>(AnalyzerConfigOptions.KeyComparer);

// copy existing option values:
foreach (var oldKey in languageOptions.Keys)
{
lazyBuilder.Add(oldKey, oldValue);
if (languageOptions.TryGetValue(oldKey, out var oldValue))
{
lazyBuilder.Add(oldKey, oldValue);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
// 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.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.Host;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Options.UnitTests;
Expand All @@ -32,21 +34,30 @@ public void FlowsGlobalOptionsToWorkspace()

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

// default value is false:
// default values:
Assert.False(globalOptions.GetOption(FormattingOptions2.InsertFinalNewLine));
Assert.Equal(4, globalOptions.GetOption(FormattingOptions2.IndentationSize, LanguageNames.CSharp));
Assert.Equal(4, globalOptions.GetOption(FormattingOptions2.IndentationSize, LanguageNames.VisualBasic));

// C# project hasn't been loaded to the workspace yet:
Assert.Empty(workspace.CurrentSolution.FallbackAnalyzerOptions);

var project = new TestHostProject(workspace, "proj1", LanguageNames.CSharp);
workspace.AddTestProject(project);

AssertOptionValue(LanguageNames.CSharp, "false");
AssertOptionValue(FormattingOptions2.InsertFinalNewLine, LanguageNames.CSharp, "false");
AssertOptionValue(FormattingOptions2.IndentationSize, LanguageNames.CSharp, "4");

globalOptions.SetGlobalOption(FormattingOptions2.InsertFinalNewLine, true);
globalOptions.SetGlobalOptions(
[
new KeyValuePair<OptionKey2, object?>(FormattingOptions2.InsertFinalNewLine, true),
new KeyValuePair<OptionKey2, object?>(new OptionKey2(FormattingOptions2.IndentationSize, LanguageNames.CSharp), 3),
new KeyValuePair<OptionKey2, object?>(new OptionKey2(FormattingOptions2.IndentationSize, LanguageNames.VisualBasic), 5)
]);

// editorconfig option set as a global option should flow to the solution snapshot:
AssertOptionValue(LanguageNames.CSharp, "true");
AssertOptionValue(FormattingOptions2.InsertFinalNewLine, LanguageNames.CSharp, "true");
AssertOptionValue(FormattingOptions2.IndentationSize, LanguageNames.CSharp, "3");

workspace.OnProjectRemoved(project.Id);

Expand All @@ -55,26 +66,31 @@ public void FlowsGlobalOptionsToWorkspace()

workspace.AddTestProject(new TestHostProject(workspace, "proj2", LanguageNames.VisualBasic));

AssertOptionValue(LanguageNames.VisualBasic, "true");
AssertOptionValue(FormattingOptions2.InsertFinalNewLine, LanguageNames.VisualBasic, "true");
AssertOptionValue(FormattingOptions2.IndentationSize, LanguageNames.VisualBasic, "5");

Assert.False(workspace.CurrentSolution.FallbackAnalyzerOptions.TryGetValue(LanguageNames.CSharp, out _));

// VB and C# projects added:

workspace.AddTestProject(new TestHostProject(workspace, "proj3", LanguageNames.CSharp));

AssertOptionValue(LanguageNames.VisualBasic, "true");
AssertOptionValue(LanguageNames.CSharp, "true");
AssertOptionValue(FormattingOptions2.InsertFinalNewLine, LanguageNames.VisualBasic, "true");
AssertOptionValue(FormattingOptions2.InsertFinalNewLine, LanguageNames.CSharp, "true");
AssertOptionValue(FormattingOptions2.IndentationSize, LanguageNames.VisualBasic, "5");
AssertOptionValue(FormattingOptions2.IndentationSize, LanguageNames.CSharp, "3");

globalOptions.SetGlobalOption(FormattingOptions2.InsertFinalNewLine, false);

AssertOptionValue(LanguageNames.VisualBasic, "false");
AssertOptionValue(LanguageNames.CSharp, "false");
AssertOptionValue(FormattingOptions2.InsertFinalNewLine, LanguageNames.VisualBasic, "false");
AssertOptionValue(FormattingOptions2.InsertFinalNewLine, LanguageNames.CSharp, "false");
AssertOptionValue(FormattingOptions2.IndentationSize, LanguageNames.VisualBasic, "5");
AssertOptionValue(FormattingOptions2.IndentationSize, LanguageNames.CSharp, "3");

void AssertOptionValue(string language, string expectedValue)
void AssertOptionValue(IOption2 option, string language, string expectedValue)
{
Assert.True(workspace.CurrentSolution.FallbackAnalyzerOptions.TryGetValue(language, out var fallbackOptions));
Assert.True(fallbackOptions!.TryGetValue(FormattingOptions2.InsertFinalNewLine.Definition.ConfigName, out var configValue));
Assert.True(fallbackOptions!.TryGetValue(option.Definition.ConfigName, out var configValue));
Assert.Equal(expectedValue, configValue);
}
}
Expand Down
Loading