From 1d89752d226ca92b2444ba494128c480f7c2ee18 Mon Sep 17 00:00:00 2001 From: Bernie White Date: Wed, 14 Feb 2024 13:01:25 +1000 Subject: [PATCH] Additional fixes for #1746 (#1759) --- docs/CHANGELOG-v3.md | 6 ++++ .../Commands/ModuleCommand.cs | 31 ++++++++++--------- src/PSRule/Pipeline/Dependencies/LockFile.cs | 8 +++-- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/docs/CHANGELOG-v3.md b/docs/CHANGELOG-v3.md index 976f6c2b10..9d48488dbb 100644 --- a/docs/CHANGELOG-v3.md +++ b/docs/CHANGELOG-v3.md @@ -27,6 +27,12 @@ See [upgrade notes][1] for helpful information when upgrading from previous vers ## Unreleased +What's changed since pre-release v3.0.0-B0151: + +- Bug fixes: + - Fixes null references for CLI module handling by @BernieWhite. + [#1746](https://github.com/microsoft/PSRule/issues/1746) + ## v3.0.0-B0151 (pre-release) What's changed since pre-release v3.0.0-B0141: diff --git a/src/PSRule.CommandLine/Commands/ModuleCommand.cs b/src/PSRule.CommandLine/Commands/ModuleCommand.cs index 0cb3e72ff6..061e21fc13 100644 --- a/src/PSRule.CommandLine/Commands/ModuleCommand.cs +++ b/src/PSRule.CommandLine/Commands/ModuleCommand.cs @@ -77,7 +77,7 @@ public static int ModuleRestore(RestoreOptions operationOptions, ClientContext c } // Restore from included modules. - if (clientContext.Option.Include?.Module != null && clientContext.Option.Include.Module.Length > 0) + if (clientContext.Option?.Include?.Module != null && clientContext.Option.Include.Module.Length > 0) { foreach (var includeModule in clientContext.Option.Include.Module) { @@ -140,7 +140,7 @@ public static int ModuleInit(ModuleOptions operationOptions, ClientContext clien using var pwsh = PowerShell.Create(); // Add for any included modules. - if (clientContext.Option.Include?.Module != null && clientContext.Option.Include.Module.Length > 0) + if (clientContext.Option?.Include?.Module != null && clientContext.Option.Include.Module.Length > 0) { foreach (var includeModule in clientContext.Option.Include.Module) { @@ -335,7 +335,7 @@ public static int ModuleUpgrade(ModuleOptions operationOptions, ClientContext cl #region Helper methods - private static IEnumerable GetModules(PowerShell pwsh, LockFile file, PSRuleOption option) + private static IEnumerable GetModules(PowerShell pwsh, LockFile file, PSRuleOption? option) { var results = new List(); @@ -352,19 +352,22 @@ private static IEnumerable GetModules(PowerShell pwsh, LockFile fi } // Process included modules from options. - foreach (var includeModule in option.Include.Module) + if (option?.Include?.Module != null && option.Include.Module.Length > 0) { - // Skip modules already in the lock. - if (file.Modules.ContainsKey(includeModule)) - continue; + foreach (var includeModule in option.Include.Module) + { + // Skip modules already in the lock. + if (file.Modules.ContainsKey(includeModule)) + continue; - var installed = IsInstalled(pwsh, includeModule, null, out var installedVersion); - results.Add(new ModuleRecord( - Name: includeModule, - Version: installedVersion?.ToString() ?? "latest", - Installed: installed, - Locked: false - )); + var installed = IsInstalled(pwsh, includeModule, null, out var installedVersion); + results.Add(new ModuleRecord( + Name: includeModule, + Version: installedVersion?.ToString() ?? "latest", + Installed: installed, + Locked: false + )); + } } return results; diff --git a/src/PSRule/Pipeline/Dependencies/LockFile.cs b/src/PSRule/Pipeline/Dependencies/LockFile.cs index e681b95fb2..46d6be3d30 100644 --- a/src/PSRule/Pipeline/Dependencies/LockFile.cs +++ b/src/PSRule/Pipeline/Dependencies/LockFile.cs @@ -48,18 +48,22 @@ public static LockFile Read(string path) { path = Environment.GetRootedPath(path); path = Path.GetExtension(path) == ".json" ? path : Path.Combine(path, DEFAULT_FILE); + LockFile result = null; if (File.Exists(path)) { var json = File.ReadAllText(path, Encoding.UTF8); - return JsonConvert.DeserializeObject(json, new JsonSerializerSettings + result = JsonConvert.DeserializeObject(json, new JsonSerializerSettings { Converters = new List { new SemanticVersionConverter() }, }); + return result; } - return new LockFile(); + result ??= new LockFile(); + result.Modules ??= new Dictionary(StringComparer.OrdinalIgnoreCase); + return result; } ///