diff --git a/package.json b/package.json index af8e3618..546bf4c8 100644 --- a/package.json +++ b/package.json @@ -121,6 +121,12 @@ "markdownDescription": "The name of the Buildifier executable. This may be an absolute path, or a simple name that will be searched for on the system path. Paths starting with `@` are interpreted as Bazel targets and are run via `bazel run`. If empty, \"buildifier\" on the system path will be used.\n\nBuildifier can be downloaded from https://github.com/bazelbuild/buildtools/releases.", "scope": "machine-overridable" }, + "bazel.buildifierConfigJsonPath": { + "type": "string", + "default": "", + "markdownDescription": "The path of the Buildifier config json file, ex: `.buildifier.json`. This may be an absolute path, or a relative path within the workspace. If this option is unset, `buildifier` will automatically look for a `.buildifier.json` file in the workspace.", + "scope": "machine-overridable" + }, "bazel.buildifierFixOnFormat": { "type": "boolean", "default": false, @@ -461,4 +467,4 @@ "vscode-uri": "^3.0.2", "which": "^4.0.0" } -} +} \ No newline at end of file diff --git a/src/buildifier/buildifier.ts b/src/buildifier/buildifier.ts index 99746a8c..006a8070 100644 --- a/src/buildifier/buildifier.ts +++ b/src/buildifier/buildifier.ts @@ -174,6 +174,18 @@ export function getDefaultBuildifierExecutablePath(): string { return buildifierExecutable; } +/** + * Gets the path to the buildifier json configuration file specified by the + * workspace configuration, if present. + * + * @returns The path to the buildifier json configuration file specified in the + * workspace configuration, or an empty string if not present. + */ +export function getDefaultBuildifierJsonConfigPath(): string { + const bazelConfig = vscode.workspace.getConfiguration("bazel"); + return bazelConfig.get("buildifierConfigJsonPath", ""); +} + /** * Executes buildifier with the given file content and arguments. * @@ -191,6 +203,10 @@ export function executeBuildifier( return new Promise((resolve, reject) => { // Determine the executable let executable = getDefaultBuildifierExecutablePath(); + const buildifierConfigJsonPath = getDefaultBuildifierJsonConfigPath(); + if (buildifierConfigJsonPath.length !== 0) { + args = ["--config", buildifierConfigJsonPath, ...args]; + } // Paths starting with an `@` are referring to Bazel targets if (executable.startsWith("@")) { const targetName = executable;