diff --git a/src/buildifier/buildifier_availability.ts b/src/buildifier/buildifier_availability.ts index 23e7031c..adbcaacb 100644 --- a/src/buildifier/buildifier_availability.ts +++ b/src/buildifier/buildifier_availability.ts @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +import * as fs from "fs/promises"; +import * as path from "path"; import * as vscode from "vscode"; import * as which from "which"; @@ -20,6 +22,15 @@ import { getDefaultBuildifierExecutablePath, } from "./buildifier"; +async function fileExists(filename: string) { + try { + await fs.stat(filename); + return true; + } catch { + return false; + } +} + /** The URL to load for buildifier's releases. */ const BUILDTOOLS_RELEASES_URL = "https://github.com/bazelbuild/buildtools/releases"; @@ -33,9 +44,20 @@ const BUILDTOOLS_RELEASES_URL = */ export async function checkBuildifierIsAvailable() { const buildifierExecutable = getDefaultBuildifierExecutablePath(); + // Check if the program exists (in case it's an actual executable and not // an target name starting with `@`). - if (!buildifierExecutable.startsWith("@")) { + const isTarget = buildifierExecutable.startsWith("@"); + + // Check if the program exists as a relative path of the workspace + const pathExists = await fileExists( + path.join( + vscode.workspace.workspaceFolders?.[0]?.uri?.fsPath, + buildifierExecutable, + ), + ); + + if (!isTarget && !pathExists) { try { await which(buildifierExecutable); } catch (e) {