Skip to content

fix: Handle relative buildifier path without warning #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
24 changes: 23 additions & 1 deletion src/buildifier/buildifier_availability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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";
Expand All @@ -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) {
Expand Down