From 853b37c3144c250924e3b60765668bbaa5aa52e5 Mon Sep 17 00:00:00 2001 From: James Truher Date: Thu, 14 Mar 2019 16:32:04 -0700 Subject: [PATCH] Change logic for finding the module base to hunt for the module .psd1 file This is because we might be installed in a versioned directory, rather than the a directory name PSScriptAnalyzer, we'll be in PSScriptAnalyzer/1.18.0 --- Rules/CompatibilityRules/CompatibilityRule.cs | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/Rules/CompatibilityRules/CompatibilityRule.cs b/Rules/CompatibilityRules/CompatibilityRule.cs index 5b3246506..59215b478 100644 --- a/Rules/CompatibilityRules/CompatibilityRule.cs +++ b/Rules/CompatibilityRules/CompatibilityRule.cs @@ -218,12 +218,22 @@ private string NormalizeProfileNameToAbsolutePath(string profileName) private static string GetModuleRootDirPath() { string asmDirLocation = Path.GetDirectoryName(typeof(CompatibilityRule).Assembly.Location); - - string topDir = Path.GetFileName(asmDirLocation); - - string nonNormalizedRoot = "PSScriptAnalyzer".Equals(topDir, StringComparison.OrdinalIgnoreCase) - ? Path.Combine(asmDirLocation) - : Path.Combine(asmDirLocation, ".."); + // We check our assembly location and then parent, looking for PSScriptAnalyzer.psd1, + // because the assembly might be in the root of the module or in a child directory (ex: coreclr). + // That's the base where we will find our compatibility zip file. + // We can't hunt for the directory 'PSScriptAnalyzer' because we may be installed in + // PSScriptAnalyzer/1.18.0 or PSScriptAnalyzer. + const string psdFile = "PSScriptAnalyzer.psd1"; + string nonNormalizedRoot = asmDirLocation; + string psmPath = Path.Combine(nonNormalizedRoot, psdFile); + if ( ! File.Exists(psmPath) ) { + nonNormalizedRoot = Path.Combine(nonNormalizedRoot, ".."); + psmPath = Path.Combine(nonNormalizedRoot, psdFile); + if ( ! File.Exists(psmPath) ) { + // Couldn't find it, give up + return String.Empty; + } + } return Path.GetFullPath(nonNormalizedRoot); }