Skip to content

Change Compatibility rule logic for finding the module base #1176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2019
Merged
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
22 changes: 16 additions & 6 deletions Rules/CompatibilityRules/CompatibilityRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not rather throw? If a user accidentally deletes/moves/rename the file in the installation folder, then we'd have an actionable error message

}
}

return Path.GetFullPath(nonNormalizedRoot);
}
Expand Down