Skip to content
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 @@ -89,38 +89,42 @@ private static ValidationResult ValidateOptionsAreNotDuplicated(
Dictionary<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>> extensionOptionsByProvider,
Dictionary<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>> systemOptionsByProvider)
{
// Use a dictionary to track option names and their distinct providers. OrdinalIgnoreCase
// ensures we catch case-differing duplicates (e.g. "Timeout" vs "timeout") with a friendly
// error rather than a later ArgumentException when building the lookup dictionary.
// We cover both extension and system providers so that any pair of case-differing
// duplicates — extension/extension, extension/system, or system/system — surfaces as a
// ValidationResult.Invalid rather than crashing the platform.
var optionNameToProviders = new Dictionary<string, HashSet<ICommandLineOptionsProvider>>(StringComparer.OrdinalIgnoreCase);
// Most options are unique, so retain only their first provider. Allocate a HashSet only
// after finding a provider collision, while preserving the registration order used by
// diagnostics. OrdinalIgnoreCase ensures we catch case-differing duplicates (e.g.
// "Timeout" vs "timeout") with a friendly error rather than a later ArgumentException.
var optionNameToProviders = new Dictionary<string, (ICommandLineOptionsProvider FirstProvider, HashSet<ICommandLineOptionsProvider>? DuplicateProviders)>(StringComparer.OrdinalIgnoreCase);
foreach (KeyValuePair<ICommandLineOptionsProvider, IReadOnlyCollection<CommandLineOption>> kvp in extensionOptionsByProvider.Concat(systemOptionsByProvider))
{
ICommandLineOptionsProvider provider = kvp.Key;
foreach (CommandLineOption option in kvp.Value)
{
string name = option.Name;
if (!optionNameToProviders.TryGetValue(name, out HashSet<ICommandLineOptionsProvider>? providers))
if (!optionNameToProviders.TryGetValue(name, out (ICommandLineOptionsProvider FirstProvider, HashSet<ICommandLineOptionsProvider>? DuplicateProviders) registration))
{
providers = [];
optionNameToProviders[name] = providers;
optionNameToProviders.Add(name, (provider, null));
continue;
}

if (registration.DuplicateProviders is null)
{
registration.DuplicateProviders = [registration.FirstProvider];
}

providers.Add(provider);
registration.DuplicateProviders.Add(provider);
optionNameToProviders[name] = registration;
}
}

// Check for duplications
StringBuilder? stringBuilder = null;
foreach (KeyValuePair<string, HashSet<ICommandLineOptionsProvider>> kvp in optionNameToProviders)
foreach (KeyValuePair<string, (ICommandLineOptionsProvider FirstProvider, HashSet<ICommandLineOptionsProvider>? DuplicateProviders)> kvp in optionNameToProviders)
{
if (kvp.Value.Count > 1)
if (kvp.Value.DuplicateProviders is { Count: > 1 } providers)
{
string duplicatedOption = kvp.Key;
stringBuilder ??= new();
IEnumerable<string> faultyProvidersDisplayNames = kvp.Value.Select(p => p.DisplayName);
IEnumerable<string> faultyProvidersDisplayNames = providers.Select(p => p.DisplayName);
stringBuilder.AppendLine(string.Format(CultureInfo.InvariantCulture, PlatformResources.CommandLineOptionIsDeclaredByMultipleProviders, duplicatedOption, string.Join("', '", faultyProvidersDisplayNames)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ public async Task ParseAndValidateAsync_DuplicateOptionWithDistinctProviderNames
ICommandLineOptionsProvider[] extensionCommandLineOptionsProviders =
[
new ExtensionCommandLineProviderMockWithNamedOption("userOption", "ProviderOne"),
new ExtensionCommandLineProviderMockWithNamedOption("userOption", "ProviderTwo")
new ExtensionCommandLineProviderMockWithNamedOption("userOption", "ProviderTwo"),
new ExtensionCommandLineProviderMockWithNamedOption("userOption", "ProviderThree")
];

// Act
Expand All @@ -595,7 +596,7 @@ public async Task ParseAndValidateAsync_DuplicateOptionWithDistinctProviderNames

// Assert
Assert.IsFalse(result.IsValid);
Assert.Contains("Option '--userOption' is declared by multiple providers: 'ProviderOne', 'ProviderTwo'", result.ErrorMessage);
Assert.Contains("Option '--userOption' is declared by multiple providers: 'ProviderOne', 'ProviderTwo', 'ProviderThree'", result.ErrorMessage);
}

[TestMethod]
Expand Down