From a2fcb044f69baf53d6e96f609b1ba631678177f2 Mon Sep 17 00:00:00 2001 From: John Vilk Date: Tue, 22 Dec 2020 17:20:42 -0800 Subject: [PATCH 1/2] Do not run Vale on out-of-workspace files when ${workspaceFolder} is used in settings. Fixes a bug where Vale throws multiple errors whenever you do this. --- src/features/vsProvider.ts | 5 +++++ src/features/vsUtils.ts | 38 ++++++++++++++++++-------------------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/features/vsProvider.ts b/src/features/vsProvider.ts index 9c6ff2e..128452c 100644 --- a/src/features/vsProvider.ts +++ b/src/features/vsProvider.ts @@ -64,6 +64,11 @@ export default class ValeServerProvider implements vscode.CodeActionProvider { const binaryLocation = utils.readBinaryLocation(file); const configLocation = utils.readFileLocation(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..26fa71d 100644 --- a/src/features/vsUtils.ts +++ b/src/features/vsUtils.ts @@ -6,38 +6,36 @@ 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(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 + ); + } + return null; +} + +export const readBinaryLocation = (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(customBinaryPath, file); } return which.sync("vale"); }; -export const readFileLocation = (file: vscode.TextDocument) => { +export const readFileLocation = (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(customConfigPath, file); } return ""; }; From e77be87e8d968ec33137508c1cb83f6d7f44e04e Mon Sep 17 00:00:00 2001 From: John Vilk Date: Mon, 4 Jan 2021 09:01:49 -0800 Subject: [PATCH 2/2] Append a line to the output console when Vale ignores a file --- src/features/vsProvider.ts | 4 ++-- src/features/vsUtils.ts | 11 ++++++----- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/features/vsProvider.ts b/src/features/vsProvider.ts index 128452c..5f8e325 100644 --- a/src/features/vsProvider.ts +++ b/src/features/vsProvider.ts @@ -62,8 +62,8 @@ 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; diff --git a/src/features/vsUtils.ts b/src/features/vsUtils.ts index 26fa71d..c139ad1 100644 --- a/src/features/vsUtils.ts +++ b/src/features/vsUtils.ts @@ -8,7 +8,7 @@ import * as vscode from "vscode"; // 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(customPath: string, file: vscode.TextDocument): string | null { +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) { @@ -17,25 +17,26 @@ function replaceWorkspaceFolder(customPath: string, file: vscode.TextDocument): 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 = (file: vscode.TextDocument): string | 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) { - return replaceWorkspaceFolder(customBinaryPath, file); + return replaceWorkspaceFolder(logger, customBinaryPath, file); } return which.sync("vale"); }; -export const readFileLocation = (file: vscode.TextDocument): string | null => { +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) { - return replaceWorkspaceFolder(customConfigPath, file); + return replaceWorkspaceFolder(logger, customConfigPath, file); } return ""; };