From 5a4ffb4a4af7753881d2973dfb0c38d8c85f918e Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 10 May 2021 15:22:41 -0700 Subject: [PATCH 1/3] Fix NullReferenceException related to CurrentImportDscResourceInvocations --- .../DscSupport/JsonDscClassCache.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/DscSupport/JsonDscClassCache.cs b/src/System.Management.Automation/DscSupport/JsonDscClassCache.cs index 5c3da715ccc..baa0101fea0 100755 --- a/src/System.Management.Automation/DscSupport/JsonDscClassCache.cs +++ b/src/System.Management.Automation/DscSupport/JsonDscClassCache.cs @@ -99,9 +99,16 @@ public static class DscClassCache private static readonly HashSet s_hiddenResourceCache = new HashSet(StringComparer.OrdinalIgnoreCase) { "MSFT_BaseConfigurationProviderRegistration", "MSFT_CimConfigurationProviderRegistration", "MSFT_PSConfigurationProviderRegistration" }; - // A collection to prevent circular importing case when Import-DscResource does not have a module specified + /// + /// A collection to prevent circular importing case when Import-DscResource does not have a module specified + /// + private static HashSet CurrentImportDscResourceInvocations + { + get => t_currentImportDscResourceInvocations ??= new HashSet(StringComparer.OrdinalIgnoreCase); + } + [ThreadStatic] - private static readonly HashSet t_currentImportDscResourceInvocations = new(StringComparer.OrdinalIgnoreCase); + private static HashSet t_currentImportDscResourceInvocations; /// /// Gets DSC class cache for this runspace. @@ -361,7 +368,7 @@ public static void ClearCache() ClassCache.Clear(); ByClassModuleCache.Clear(); CacheResourcesFromMultipleModuleVersions = false; - t_currentImportDscResourceInvocations.Clear(); + CurrentImportDscResourceInvocations.Clear(); } private static string GetModuleQualifiedResourceName(string moduleName, string moduleVersion, string className, string resourceName) @@ -1140,9 +1147,9 @@ internal static void LoadResourcesFromModuleInImportResourcePostParse( // Lookup the required resources under available PowerShell modules when modulename is not specified // Make sure that this is not a circular import/parsing var callLocation = string.Join(':', scriptExtent.File, scriptExtent.StartLineNumber, scriptExtent.StartColumnNumber, scriptExtent.Text); - if (!t_currentImportDscResourceInvocations.Contains(callLocation)) + if (!CurrentImportDscResourceInvocations.Contains(callLocation)) { - t_currentImportDscResourceInvocations.Add(callLocation); + CurrentImportDscResourceInvocations.Add(callLocation); using (var powerShell = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace)) { powerShell.AddCommand("Get-Module"); From f943a5189b938f4f09886e9b5db3e17892524e8a Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 10 May 2021 15:45:28 -0700 Subject: [PATCH 2/3] Fixed CodeFactor --- .../DscSupport/JsonDscClassCache.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/DscSupport/JsonDscClassCache.cs b/src/System.Management.Automation/DscSupport/JsonDscClassCache.cs index baa0101fea0..ee9d0eac7dc 100755 --- a/src/System.Management.Automation/DscSupport/JsonDscClassCache.cs +++ b/src/System.Management.Automation/DscSupport/JsonDscClassCache.cs @@ -100,7 +100,7 @@ public static class DscClassCache new HashSet(StringComparer.OrdinalIgnoreCase) { "MSFT_BaseConfigurationProviderRegistration", "MSFT_CimConfigurationProviderRegistration", "MSFT_PSConfigurationProviderRegistration" }; /// - /// A collection to prevent circular importing case when Import-DscResource does not have a module specified + /// Gets a collection to prevent circular importing case when Import-DscResource does not have a module specified. /// private static HashSet CurrentImportDscResourceInvocations { From 54b0a9948dd63acc1f45d412449085ae0b7e8358 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 10 May 2021 16:46:58 -0700 Subject: [PATCH 3/3] Simplified syntax for the property --- .../DscSupport/JsonDscClassCache.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/System.Management.Automation/DscSupport/JsonDscClassCache.cs b/src/System.Management.Automation/DscSupport/JsonDscClassCache.cs index ee9d0eac7dc..dfda9b8460a 100755 --- a/src/System.Management.Automation/DscSupport/JsonDscClassCache.cs +++ b/src/System.Management.Automation/DscSupport/JsonDscClassCache.cs @@ -103,9 +103,7 @@ public static class DscClassCache /// Gets a collection to prevent circular importing case when Import-DscResource does not have a module specified. /// private static HashSet CurrentImportDscResourceInvocations - { - get => t_currentImportDscResourceInvocations ??= new HashSet(StringComparer.OrdinalIgnoreCase); - } + => t_currentImportDscResourceInvocations ??= new HashSet(StringComparer.OrdinalIgnoreCase); [ThreadStatic] private static HashSet t_currentImportDscResourceInvocations;