Skip to content

Commit

Permalink
Show error when using bad settings
Browse files Browse the repository at this point in the history
  • Loading branch information
karthiknadig committed Mar 29, 2024
1 parent fd92564 commit 0cb9ea7
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/common/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
import { ConfigurationChangeEvent, ConfigurationScope, Uri, WorkspaceConfiguration, WorkspaceFolder } from 'vscode';
import { getInterpreterDetails } from './python';
import { getConfiguration, getWorkspaceFolders } from './vscodeapi';
import { traceInfo, traceLog, traceWarn } from './logging';
import { traceError, traceInfo, traceLog, traceWarn } from './logging';
import { EXTENSION_ID } from './constants';
import { TransportKind } from 'vscode-languageclient/node';
import { trace } from 'console';

export interface ISettings {
cwd: string;
Expand All @@ -30,10 +31,23 @@ export function getExtensionSettings(namespace: string, includeInterpreter?: boo

function resolveVariables(
value: string[],
key: string,
workspace?: WorkspaceFolder,
interpreter?: string[],
env?: NodeJS.ProcessEnv,
): string[] {
for (const v of value) {
if (typeof v !== 'string') {
traceError(`Value [${v}] must be "string" for \`black-formatter.${key}\`: ${value}`);
throw new Error(`Value [${v}] must be "string" for \`black-formatter.${key}\`: ${value}`);
}
if (v.startsWith('--') && v.includes(' ')) {
traceError(
`Settings should be in the form ["--line-length=88"] or ["--line-length", "88"] but not ["--line-length 88"]`,
);
}
}

const substitutions = new Map<string, string>();
const home = process.env.HOME || process.env.USERPROFILE;
if (home) {
Expand Down Expand Up @@ -75,7 +89,7 @@ function resolveVariables(

function getCwd(config: WorkspaceConfiguration, workspace: WorkspaceFolder): string {
const cwd = config.get<string>('cwd', workspace.uri.fsPath);
return resolveVariables([cwd], workspace)[0];
return resolveVariables([cwd], 'cwd', workspace)[0];
}

export function getInterpreterFromSetting(namespace: string, scope?: ConfigurationScope) {
Expand Down Expand Up @@ -115,9 +129,9 @@ export async function getWorkspaceSettings(
const workspaceSetting = {
cwd: getCwd(config, workspace),
workspace: workspace.uri.toString(),
args: resolveVariables(config.get<string[]>('args', []), workspace),
path: resolveVariables(config.get<string[]>('path', []), workspace, interpreter),
interpreter: resolveVariables(interpreter, workspace),
args: resolveVariables(config.get<string[]>('args', []), 'args', workspace),
path: resolveVariables(config.get<string[]>('path', []), 'path', workspace, interpreter),
interpreter: resolveVariables(interpreter, 'interpreter', workspace),
importStrategy: config.get<string>('importStrategy', 'useBundled'),
showNotifications: config.get<string>('showNotifications', 'off'),
};
Expand Down

0 comments on commit 0cb9ea7

Please sign in to comment.