Skip to content

Commit aaa24d9

Browse files
authored
Change logic for finding the module base to hunt for the module .psd1 file (#1176)
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
1 parent 9642bf7 commit aaa24d9

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

Rules/CompatibilityRules/CompatibilityRule.cs

+16-6
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,22 @@ private string NormalizeProfileNameToAbsolutePath(string profileName)
218218
private static string GetModuleRootDirPath()
219219
{
220220
string asmDirLocation = Path.GetDirectoryName(typeof(CompatibilityRule).Assembly.Location);
221-
222-
string topDir = Path.GetFileName(asmDirLocation);
223-
224-
string nonNormalizedRoot = "PSScriptAnalyzer".Equals(topDir, StringComparison.OrdinalIgnoreCase)
225-
? Path.Combine(asmDirLocation)
226-
: Path.Combine(asmDirLocation, "..");
221+
// We check our assembly location and then parent, looking for PSScriptAnalyzer.psd1,
222+
// because the assembly might be in the root of the module or in a child directory (ex: coreclr).
223+
// That's the base where we will find our compatibility zip file.
224+
// We can't hunt for the directory 'PSScriptAnalyzer' because we may be installed in
225+
// PSScriptAnalyzer/1.18.0 or PSScriptAnalyzer.
226+
const string psdFile = "PSScriptAnalyzer.psd1";
227+
string nonNormalizedRoot = asmDirLocation;
228+
string psmPath = Path.Combine(nonNormalizedRoot, psdFile);
229+
if ( ! File.Exists(psmPath) ) {
230+
nonNormalizedRoot = Path.Combine(nonNormalizedRoot, "..");
231+
psmPath = Path.Combine(nonNormalizedRoot, psdFile);
232+
if ( ! File.Exists(psmPath) ) {
233+
// Couldn't find it, give up
234+
return String.Empty;
235+
}
236+
}
227237

228238
return Path.GetFullPath(nonNormalizedRoot);
229239
}

0 commit comments

Comments
 (0)