Skip to content

Commit

Permalink
fix: Resolve query failure when there is a large number of files in t… (
Browse files Browse the repository at this point in the history
#6537)

* fix: Resolve query failure when there is a large number of files in the Working Set

closes #6478

* Use NIO where possible

* Apply review comments
  • Loading branch information
Tomasz Pasternak authored Jul 4, 2024
1 parent 8eff782 commit 6527cdd
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
4 changes: 3 additions & 1 deletion base/src/META-INF/blaze-base.xml
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@
<registryKey defaultValue="true"
description="By default Bazel plugin takes control and opens project if .ijwb folder is present. This property can be used to disable this behavior to allow to open .idea or other project models if they are exists."
key="bazel.project.auto.open.if.present"/>

<registryKey defaultValue="false"
description="The Bazel Plugin sometimes uses the --query_file option to pass the query via a file. By default, the file is deleted immediately after the query execution is finished. Set this flag to true to avoid file deletion"
key="bazel.sync.keep.query.files"/>
<editorNotificationProvider implementation="com.google.idea.blaze.base.wizard2.BazelNotificationProvider"/>
</extensions>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,25 @@
import com.google.idea.blaze.common.PrintOutput;
import com.google.idea.blaze.exception.BuildException;
import com.google.idea.common.experiments.BoolExperiment;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.Project;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.Future;
import javax.annotation.Nullable;

import com.intellij.openapi.util.registry.Registry;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.ide.PooledThreadExecutor;

/**
Expand Down Expand Up @@ -89,6 +97,8 @@ public BlazeQuerySourceToTargetException(String message, Throwable cause) {
}
}

static Logger logger = Logger.getInstance(BlazeQuerySourceToTargetProvider.class);

@Override
public Future<List<TargetInfo>> getTargetsBuildingSourceFile(
Project project, String workspaceRelativePath) {
Expand Down Expand Up @@ -185,10 +195,11 @@ private static ImmutableList<TargetInfo> runRecursiveRdepsQuery(
@Nullable
private static ImmutableList<TargetInfo> getTargetInfoList(
Project project, BlazeContext context, ContextType type, String rdepsQuery)
throws BlazeQuerySourceToTargetException {
throws BlazeQuerySourceToTargetException {
Path queryFile = prepareQueryFile(project, rdepsQuery);
BlazeCommand.Builder command =
getBlazeCommandBuilder(
project, type, rdepsQuery, ImmutableList.of("--output=label_kind"), context);
project, type, "--query_file=" + queryFile.toAbsolutePath(), ImmutableList.of("--output=label_kind"), context);
try (InputStream queryResultStream = runQuery(project, command, context)) {
BlazeQueryLabelKindParser blazeQueryLabelKindParser =
new BlazeQueryLabelKindParser(t -> true);
Expand All @@ -201,7 +212,35 @@ private static ImmutableList<TargetInfo> getTargetInfoList(
return blazeQueryLabelKindParser.getTargets();
} catch (IOException e) {
throw new BlazeQuerySourceToTargetException("Failed to get target info list", e);
} finally {
if (!Registry.is("bazel.sync.keep.query.files")) {
try {
Files.deleteIfExists(queryFile);
} catch (IOException e) {
logger.error("Failed to delete query file", e);
}
}
}
}

private static @NotNull Path prepareQueryFile(Project project, String rdepsQuery) throws BlazeQuerySourceToTargetException {
Path queryFile = null;
try {
Path queriesDir = Paths.get(project.getBasePath()).resolve("queries");
Files.createDirectories(queriesDir);
queryFile = Files.createTempFile(queriesDir, "query-", "");
Files.writeString(queryFile, rdepsQuery, StandardOpenOption.WRITE);
} catch (IOException e) {
if (queryFile != null) {
try {
Files.deleteIfExists(queryFile);
} catch (IOException ex) {
throw new BlazeQuerySourceToTargetException("Couldn't delete file after creation failure", e);
}
}
throw new BlazeQuerySourceToTargetException("Couldn't create query file", e);
}
return queryFile;
}

@Nullable
Expand Down

0 comments on commit 6527cdd

Please sign in to comment.