Skip to content

Commit 3663252

Browse files
committed
Initial attempt to fix PowerShell#122 analyzer settings path
This adds a scriptAnalysis.settingsPath setting but uncovers some issues about how to be an absolute path back to the language host from the client. Depending on where the user "set" this setting a relative path has a different meaning. If set in .vscode\settings.json then relative probably should be relative to the workspaceRoot. In the global settings file, it should probably be the extensionInstallDir. If in the user's settings file, then what ... their home dir? If we treated this like the other HostPaths then relative would *always* be relative to the extensionInstallDir. If we could get VSCode to expand ${workspaceRoot} that would allow the user to specify a path in the current workspace folder.
1 parent 4293111 commit 3663252

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

PSScriptAnalyzerSettings.psd1

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# The PowerShell Script Analyzer will generate a warning
2+
# diagnostic record for this file due to a bug -
3+
# https://github.com/PowerShell/PSScriptAnalyzer/issues/472
4+
@{
5+
# Only diagnostic records of the specified severity will be generated.
6+
# Uncomment the following line if you only want Errors and Warnings but
7+
# not Information diagnostic records.
8+
#Severity = @('Error','Warning')
9+
10+
# Analyze **only** the following rules. Use IncludeRules when you want
11+
# to invoke only a small subset of the defualt rules.
12+
IncludeRules = @('PSAvoidDefaultValueSwitchParameter',
13+
'PSMissingModuleManifestField',
14+
'PSReservedCmdletChar',
15+
'PSReservedParams',
16+
'PSShouldProcess',
17+
'PSUseApprovedVerbs',
18+
'PSUseDeclaredVarsMoreThanAssigments')
19+
20+
# Do not analyze the following rules. Use ExcludeRules when you have
21+
# commented out the IncludeRules settings above and want to include all
22+
# the default rules except for those you exclude below.
23+
# Note: if a rule is in both IncludeRules and ExcludeRules, the rule
24+
# will be excluded.
25+
#ExcludeRules = @('PSAvoidUsingWriteHost')
26+
}

package.json

+5
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@
233233
"default": true,
234234
"description": "Enables real-time script analysis using PowerShell Script Analyzer."
235235
},
236+
"powershell.scriptAnalysis.settingsPath": {
237+
"type": "string",
238+
"default": "./PSScriptAnalyzerSettings.psd1",
239+
"description": "Specifies the path to the PowerShell Script Analyzer settings file. The settings file can be used to customize which rules to include or exclude as well as what severity levels to report."
240+
},
236241
"powershell.developer.editorServicesHostPath": {
237242
"type": "string",
238243
"default": "../bin/",

src/main.ts

+32-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ export function activate(context: vscode.ExtensionContext): void {
7373

7474
try
7575
{
76+
settings.scriptAnalysis.settingsPath = resolveScriptAnalysisSettingsPath(settings);
77+
7678
let serverPath = resolveLanguageServerPath(settings);
7779
let serverOptions = {
7880
run: {
@@ -151,7 +153,7 @@ function resolveLanguageServerPath(settings: settingsManager.ISettings): string
151153
console.log(" Resolved path to: " + editorServicesHostPath);
152154
}
153155
else {
154-
// Use the default path in the plugin's 'bin' folder
156+
// Use the default path in the extension's 'bin' folder
155157
editorServicesHostPath =
156158
path.join(
157159
__dirname,
@@ -165,6 +167,35 @@ function resolveLanguageServerPath(settings: settingsManager.ISettings): string
165167
return editorServicesHostPath;
166168
}
167169

170+
function resolveScriptAnalysisSettingsPath(settings: settingsManager.ISettings): string {
171+
var scriptAnalysisSettingsPath = settings.scriptAnalysis.settingsPath;
172+
173+
if (scriptAnalysisSettingsPath) {
174+
console.log("Found scriptAnalysis.settingsPath from config: " + scriptAnalysisSettingsPath);
175+
176+
// Make the path absolute if it's not
177+
scriptAnalysisSettingsPath =
178+
path.resolve(
179+
__dirname,
180+
'..',
181+
scriptAnalysisSettingsPath);
182+
183+
console.log(" Resolved path to: " + scriptAnalysisSettingsPath);
184+
}
185+
else {
186+
// Use the default path in the extension's 'root' folder
187+
scriptAnalysisSettingsPath =
188+
path.join(
189+
__dirname,
190+
'..',
191+
'PSScriptAnalyzerSettings.psd1');
192+
193+
console.log("Using default scriptAnalysis.settingsPath: " + scriptAnalysisSettingsPath);
194+
}
195+
196+
return scriptAnalysisSettingsPath;
197+
}
198+
168199
function getHostExeName(useX86Host: boolean): string {
169200
// The useX86Host setting is only relevant on 64-bit OS
170201
var is64BitOS = process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432');

src/settings.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import vscode = require('vscode');
88

99
export interface IScriptAnalysisSettings {
1010
enable?: boolean
11+
settingsPath: string
1112
}
1213

1314
export interface IDeveloperSettings {
@@ -26,7 +27,8 @@ export function load(myPluginId: string): ISettings {
2627
let configuration = vscode.workspace.getConfiguration(myPluginId);
2728

2829
let defaultScriptAnalysisSettings = {
29-
enable: true
30+
enable: true,
31+
settingsPath: "./PSScriptAnalyzerSettings.psd1"
3032
};
3133

3234
let defaultDeveloperSettings = {

0 commit comments

Comments
 (0)