Skip to content
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

Use path to workspace root as pwd for buildifier #6158

Merged
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
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
Loading