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
…he Working Set

closes bazelbuild#6478
  • Loading branch information
Tomasz Pasternak committed Jun 28, 2024
1 parent 5cd93dc commit 3a56897
Show file tree
Hide file tree
Showing 2 changed files with 34 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="Bazel Plugin sometimes uses --query_file option to pass the query via a file. By default, the file is deleted right 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 @@ -41,18 +41,26 @@
import com.google.idea.blaze.base.sync.workspace.WorkspacePathResolverProvider;
import com.google.idea.blaze.common.PrintOutput;
import com.google.idea.blaze.exception.BuildException;
import com.google.idea.blaze.qsync.project.ProjectProtoTransform;
import com.google.idea.common.experiments.BoolExperiment;
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 @@ -185,10 +193,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 +210,27 @@ private static ImmutableList<TargetInfo> getTargetInfoList(
return blazeQueryLabelKindParser.getTargets();
} catch (IOException e) {
throw new BlazeQuerySourceToTargetException("Failed to get target info list", e);
} finally {
if (!Registry.get("bazel.sync.keep.query.files").asBoolean()) {
queryFile.toFile().delete();
}
}
}

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

@Nullable
Expand Down

0 comments on commit 3a56897

Please sign in to comment.