Skip to content
Merged
Show file tree
Hide file tree
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
38 changes: 38 additions & 0 deletions src/binaries/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { BinaryConfig, BinaryInfo } from "./types";
export function resolveBinary(config: BinaryConfig, fallbackPath?: string): BinaryInfo {
let path = vscode.workspace.getConfiguration("opa.dependency_paths").get<string>(config.configKey);

let configuredOriginalPath: string | undefined;

if (path?.trim()) {
const originalPath = path;
path = replaceWorkspaceFolderPathVariable(path);
Expand All @@ -27,6 +29,8 @@ export function resolveBinary(config: BinaryConfig, fallbackPath?: string): Bina
...(versionInfo.error && { error: versionInfo.error }),
};
}

configuredOriginalPath = originalPath;
}

const systemPath = fallbackPath || config.configKey;
Expand All @@ -36,12 +40,46 @@ export function resolveBinary(config: BinaryConfig, fallbackPath?: string): Bina
path: systemPath,
source: "system",
version: versionInfo.version,
...(configuredOriginalPath && {
originalPath: configuredOriginalPath,
configuredPathMissing: true,
}),
...(versionInfo.error && { error: versionInfo.error }),
};
}

return {
source: "missing",
version: "missing",
...(configuredOriginalPath && {
originalPath: configuredOriginalPath,
configuredPathMissing: true,
}),
};
}

// warnConfiguredPathMissing logs to the output channel and shows a popup
// indicating the configured binary path didn't exist on disk. Callers should
// gate this on binaryInfo.configuredPathMissing being set.
export function warnConfiguredPathMissing(
config: BinaryConfig,
binaryInfo: BinaryInfo,
outputChannel: vscode.OutputChannel,
): void {
const inspected = vscode.workspace
.getConfiguration("opa.dependency_paths")
.inspect<string>(config.configKey);
const sourceDetail = inspected?.workspaceValue
? "workspace settings (.vscode/settings.json)"
: "user settings";

outputChannel.appendLine(
`${config.name}: configured path '${binaryInfo.originalPath}' from ${sourceDetail} not found; falling back to ${
binaryInfo.source === "system" ? "system PATH" : "no binary available"
}.`,
);

vscode.window.showWarningMessage(
`${config.name} binary not found at configured path '${binaryInfo.originalPath}' (from ${sourceDetail}).`,
);
}
1 change: 1 addition & 0 deletions src/binaries/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface BinaryInfo {
path?: string;
source: "configured" | "system" | "missing";
originalPath?: string;
configuredPathMissing?: boolean;
version: string;
error?: string;
}
15 changes: 14 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@ import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";

import { BinaryConfig, installBinary, OPA_CONFIG, REGAL_CONFIG, resolveBinary } from "./binaries";
import {
BinaryConfig,
installBinary,
OPA_CONFIG,
REGAL_CONFIG,
resolveBinary,
warnConfiguredPathMissing,
} from "./binaries";
import { activateDebugger } from "./da/activate";
import {
activateRegal,
Expand Down Expand Up @@ -144,6 +151,12 @@ export async function activate(context: vscode.ExtensionContext) {
// check for missing binaries and prompt to install them
checkMissingBinaries();

// warn if the configured opa.dependency_paths.opa points to a non-existent file
const opaBinaryInfo = resolveBinary(OPA_CONFIG, "opa");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should this not be done for the Regal binary too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep, the binary was already being resolved on Regal startup though, so it didn't show up in the diff: https://github.com/open-policy-agent/vscode-opa/pull/449/changes#diff-1944111a462efb504e2d39ee24a9cb532c03164ada37caada104fecc06d874bdL156

So I just needed to add the OPA binary resolution here so I could know when to send the warning.

if (opaBinaryInfo.configuredPathMissing) {
warnConfiguredPathMissing(OPA_CONFIG, opaBinaryInfo, opaOutputChannel);
}

// start Regal language server and wire up the client
const result = await activateRegal(regalOptions);
if (result) {
Expand Down
6 changes: 5 additions & 1 deletion src/ls/clients/regal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
ServerOptions,
State,
} from "vscode-languageclient/node";
import { REGAL_CONFIG, resolveBinary } from "../../binaries";
import { REGAL_CONFIG, resolveBinary, warnConfiguredPathMissing } from "../../binaries";
import {
evalResultDecorationType,
evalResultTargetSuccessDecorationType,
Expand Down Expand Up @@ -155,6 +155,10 @@ export async function activateRegal(

const binaryInfo = resolveBinary(REGAL_CONFIG, "regal");

if (binaryInfo.configuredPathMissing) {
warnConfiguredPathMissing(REGAL_CONFIG, binaryInfo, opaOutputChannel);
}

// Validate binary availability
if (!binaryInfo.path) {
clientLock = false;
Expand Down
Loading