Skip to content

Commit

Permalink
Use path to workspace root as pwd for buildifier (#6158)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkashyn-sfdc authored Feb 26, 2024
1 parent 57b5973 commit 2f4de64
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.TextRange;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Path;
Expand Down Expand Up @@ -125,7 +126,19 @@ private static Iterable<String> pathArg(@Nullable BuildFile buildFile) {
@Nullable
private static String formatText(
String buildifierBinaryPath, BuildFile buildFile, String inputText) throws IOException {
Process process = new ProcessBuilder(getCommandLineArgs(buildifierBinaryPath, buildFile)).start();
// We need to be in a proper directory to respect default buildifier config placement convention
// see https://github.com/bazelbuild/buildtools/blob/03bf520394afefdf48c558187b2d76b8b4b60ef1/buildifier/buildifier.go#L85
// `a file named '.buildifier.json' at the root of the workspace (e.g., in the same directory as the WORKSPACE file)`
File buildifierWorkingDir = null;

WorkspaceRoot workspaceRoot = BazelWorkspaceRootProvider.INSTANCE.findWorkspaceRoot(buildFile.getFile());
if (workspaceRoot != null) {
buildifierWorkingDir = workspaceRoot.directory();
}

Process process = new ProcessBuilder(getCommandLineArgs(buildifierBinaryPath, buildFile))
.directory(buildifierWorkingDir)
.start();
process.getOutputStream().write(inputText.getBytes(UTF_8));
process.getOutputStream().close();

Expand Down

0 comments on commit 2f4de64

Please sign in to comment.