diff --git a/src/features/vsProvider.ts b/src/features/vsProvider.ts index 9c6ff2e..5f8e325 100644 --- a/src/features/vsProvider.ts +++ b/src/features/vsProvider.ts @@ -62,8 +62,13 @@ export default class ValeServerProvider implements vscode.CodeActionProvider { private async runVale(file: vscode.TextDocument) { const folder = path.dirname(file.fileName); - const binaryLocation = utils.readBinaryLocation(file); - const configLocation = utils.readFileLocation(file); + const binaryLocation = utils.readBinaryLocation(this.logger, file); + const configLocation = utils.readFileLocation(this.logger, file); + if (binaryLocation == null || configLocation == null) { + // `file` is not part of the workspace, so we could not resolve a workspace-relative path. Ignore this file. + return; + } + // There are two cases we need to handle here: // // (1) If we're given an explicit value for `--config`, then we should diff --git a/src/features/vsUtils.ts b/src/features/vsUtils.ts index 2435d42..c139ad1 100644 --- a/src/features/vsUtils.ts +++ b/src/features/vsUtils.ts @@ -6,38 +6,37 @@ import { execFile } from "child_process"; import * as vscode from "vscode"; -export const readBinaryLocation = (file: vscode.TextDocument) => { +// If `customPath` contains `${workspaceFolder}`, replaces it with the workspace that `file` comes from. +// Return `null` if `customPath` contains `${workspaceFolder}` and `file` is _not_ part of the workspace. +function replaceWorkspaceFolder(logger: vscode.OutputChannel, customPath: string, file: vscode.TextDocument): string | null { + customPath = path.normalize(customPath); + const workspaceFolder = vscode.workspace.getWorkspaceFolder(file.uri); + if (workspaceFolder) { + return customPath.replace( + "${workspaceFolder}", + workspaceFolder.uri.fsPath + ); + } + logger.appendLine(`Not running Vale on file '${file.uri}' as it is not contained within the workspace`); + return null; +} + +export const readBinaryLocation = (logger: vscode.OutputChannel, file: vscode.TextDocument): string | null => { const configuration = vscode.workspace.getConfiguration(); let customBinaryPath = configuration.get("vale.valeCLI.path"); if (customBinaryPath) { - customBinaryPath = path.normalize(customBinaryPath); - const workspaceFolder = vscode.workspace.getWorkspaceFolder(file.uri); - if (workspaceFolder) { - customBinaryPath = customBinaryPath.replace( - "${workspaceFolder}", - workspaceFolder.uri.fsPath - ); - } - return customBinaryPath; + return replaceWorkspaceFolder(logger, customBinaryPath, file); } return which.sync("vale"); }; -export const readFileLocation = (file: vscode.TextDocument) => { +export const readFileLocation = (logger: vscode.OutputChannel, file: vscode.TextDocument): string | null => { const configuration = vscode.workspace.getConfiguration(); let customConfigPath = configuration.get("vale.valeCLI.config"); if (customConfigPath) { - customConfigPath = path.normalize(customConfigPath); - const workspaceFolder = vscode.workspace.getWorkspaceFolder(file.uri); - if (workspaceFolder) { - customConfigPath = customConfigPath.replace( - "${workspaceFolder}", - workspaceFolder.uri.fsPath - ); - } - return customConfigPath; + return replaceWorkspaceFolder(logger, customConfigPath, file); } return ""; };