From 23e08ae30ce76d2338c7a015e551956995213614 Mon Sep 17 00:00:00 2001 From: Mihai Toader Date: Wed, 4 Sep 2024 17:54:34 -0700 Subject: [PATCH 1/2] [#6664]: Invoke `blaze mod` correctly --- .../base/command/mod/BlazeModRunnerImpl.java | 19 ++++++++++++++++++- .../model/ExternalWorkspaceDataProvider.java | 12 ++++++------ .../blaze/base/sync/ProjectStateSyncTask.java | 4 ++-- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/base/src/com/google/idea/blaze/base/command/mod/BlazeModRunnerImpl.java b/base/src/com/google/idea/blaze/base/command/mod/BlazeModRunnerImpl.java index f85fa9ce9fb..66685bc6ce0 100644 --- a/base/src/com/google/idea/blaze/base/command/mod/BlazeModRunnerImpl.java +++ b/base/src/com/google/idea/blaze/base/command/mod/BlazeModRunnerImpl.java @@ -38,6 +38,19 @@ public class BlazeModRunnerImpl extends BlazeModRunner { + private static final String DUMP_REPO_MAPPING = "dump_repo_mapping"; + private static final String ROOT_WORKSPACE = ""; + + /** + * {@code bazel mod dump_repo_mapping} takes a canonical repository name and will dump a map + * from repoName -> canonicalName of all the external repositories available to that repository + * The name {@code ""} is special and considered to be the main workspace so in order to dump the main + * repository map we would invoke it like {@code bazel mod dump_repo_mapping ""}. + *

+ * Additionally the flag {@code --enable_workspace} needs to be off for this to work. The flag is default + * off in bazel 8.0.0 but it is on between 7.1.0 and 8.0.0. So we need to also pass this along in + * between those versions for the command to work well. + */ @Override public ListenableFuture dumpRepoMapping( Project project, @@ -45,8 +58,12 @@ public ListenableFuture dumpRepoMapping( BlazeContext context, BuildSystemName buildSystemName, List flags) { + + // TODO: when 8.0.0 is released add this only if it's disabled explicitly for the repo + flags.add("--noenable_workspace"); + return Futures.transform( - runBlazeModGetBytes(project, invoker, context, ImmutableList.of( "dump_repo_mapping", "workspace"), flags), + runBlazeModGetBytes(project, invoker, context, ImmutableList.of(DUMP_REPO_MAPPING, ROOT_WORKSPACE), flags), bytes -> { JsonObject json = JsonParser.parseString(new String(bytes, StandardCharsets.UTF_8).trim()).getAsJsonObject(); diff --git a/base/src/com/google/idea/blaze/base/model/ExternalWorkspaceDataProvider.java b/base/src/com/google/idea/blaze/base/model/ExternalWorkspaceDataProvider.java index 98f4f03c3fe..7a06611243c 100644 --- a/base/src/com/google/idea/blaze/base/model/ExternalWorkspaceDataProvider.java +++ b/base/src/com/google/idea/blaze/base/model/ExternalWorkspaceDataProvider.java @@ -57,12 +57,12 @@ public static ExternalWorkspaceDataProvider getInstance(Project project) { } static Boolean isEnabled(BlazeVersionData blazeVersionData) { - // disable this until a more reliable opt-in mechanism is chosen. - // - // bg: some blaze workspaces with blaze > MINIMUM_BLAZE_VERSION - // have explicitly disabled this bzlmod support and this causes - // `blaze mod` to fail. - return blazeVersionData.bazelIsAtLeastVersion(MINIMUM_BLAZE_VERSION) && Registry.is("bazel.read.external.workspace.data"); + if (!Registry.is("bazel.read.external.workspace.data")) { + logger.info("disabled by registry"); + return false; + } + + return blazeVersionData.bazelIsAtLeastVersion(MINIMUM_BLAZE_VERSION); } public ListenableFuture getExternalWorkspaceData( diff --git a/base/src/com/google/idea/blaze/base/sync/ProjectStateSyncTask.java b/base/src/com/google/idea/blaze/base/sync/ProjectStateSyncTask.java index 8bbdcb1a480..8696f18bfd2 100644 --- a/base/src/com/google/idea/blaze/base/sync/ProjectStateSyncTask.java +++ b/base/src/com/google/idea/blaze/base/sync/ProjectStateSyncTask.java @@ -233,7 +233,7 @@ private ExternalWorkspaceData getExternalWorkspaceData( SyncMode syncMode) throws SyncCanceledException, SyncFailedException { - List syncFlags = + List blazeModFlags = BlazeFlags.blazeFlags( project, projectViewSet, @@ -243,7 +243,7 @@ private ExternalWorkspaceData getExternalWorkspaceData( ListenableFuture externalWorkspaceDataFuture = ExternalWorkspaceDataProvider.getInstance(project) - .getExternalWorkspaceData(context, syncFlags, blazeVersionData, blazeInfo); + .getExternalWorkspaceData(context, blazeModFlags, blazeVersionData, blazeInfo); FutureResult externalWorkspaceDataResult = FutureUtil.waitForFuture(context, externalWorkspaceDataFuture) From 22b9a4238e93148372fb1349af58d9a10bfabee3 Mon Sep 17 00:00:00 2001 From: Mihai Toader Date: Thu, 5 Sep 2024 07:28:40 -0700 Subject: [PATCH 2/2] Reformat with google format --- .../base/command/mod/BlazeModRunnerImpl.java | 52 ++++++++++--------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/base/src/com/google/idea/blaze/base/command/mod/BlazeModRunnerImpl.java b/base/src/com/google/idea/blaze/base/command/mod/BlazeModRunnerImpl.java index 66685bc6ce0..3d543d3c160 100644 --- a/base/src/com/google/idea/blaze/base/command/mod/BlazeModRunnerImpl.java +++ b/base/src/com/google/idea/blaze/base/command/mod/BlazeModRunnerImpl.java @@ -42,14 +42,14 @@ public class BlazeModRunnerImpl extends BlazeModRunner { private static final String ROOT_WORKSPACE = ""; /** - * {@code bazel mod dump_repo_mapping} takes a canonical repository name and will dump a map - * from repoName -> canonicalName of all the external repositories available to that repository - * The name {@code ""} is special and considered to be the main workspace so in order to dump the main - * repository map we would invoke it like {@code bazel mod dump_repo_mapping ""}. - *

- * Additionally the flag {@code --enable_workspace} needs to be off for this to work. The flag is default - * off in bazel 8.0.0 but it is on between 7.1.0 and 8.0.0. So we need to also pass this along in - * between those versions for the command to work well. + * {@code bazel mod dump_repo_mapping} takes a canonical repository name and will dump a map from + * repoName -> canonicalName of all the external repositories available to that repository The + * name {@code ""} is special and considered to be the main workspace so in order to dump + * the main repository map we would invoke it like {@code bazel mod dump_repo_mapping ""}. + * + *

Additionally the flag {@code --enable_workspace} needs to be off for this to work. The flag + * is default off in bazel 8.0.0 but it is on between 7.1.0 and 8.0.0. So we need to also pass + * this along in between those versions for the command to work well. */ @Override public ListenableFuture dumpRepoMapping( @@ -63,9 +63,12 @@ public ListenableFuture dumpRepoMapping( flags.add("--noenable_workspace"); return Futures.transform( - runBlazeModGetBytes(project, invoker, context, ImmutableList.of(DUMP_REPO_MAPPING, ROOT_WORKSPACE), flags), + runBlazeModGetBytes( + project, invoker, context, ImmutableList.of(DUMP_REPO_MAPPING, ROOT_WORKSPACE), flags), bytes -> { - JsonObject json = JsonParser.parseString(new String(bytes, StandardCharsets.UTF_8).trim()).getAsJsonObject(); + JsonObject json = + JsonParser.parseString(new String(bytes, StandardCharsets.UTF_8).trim()) + .getAsJsonObject(); ImmutableList externalWorkspaces = json.entrySet().stream() @@ -87,21 +90,22 @@ protected ListenableFuture runBlazeModGetBytes( List args, List flags) { return BlazeExecutor.getInstance() - .submit(() -> { - BlazeCommand.Builder builder = - BlazeCommand.builder(invoker, BlazeCommandName.MOD) - .addBlazeFlags(flags); + .submit( + () -> { + BlazeCommand.Builder builder = + BlazeCommand.builder(invoker, BlazeCommandName.MOD).addBlazeFlags(flags); - if (args != null) { - builder.addBlazeFlags(args); - } + if (args != null) { + builder.addBlazeFlags(args); + } - try (BuildResultHelper buildResultHelper = invoker.createBuildResultHelper()) { - BlazeCommandRunner runner = invoker.getCommandRunner(); - try (InputStream stream = runner.runBlazeMod(project, builder, buildResultHelper, context)) { - return stream.readAllBytes(); - } - } - }); + try (BuildResultHelper buildResultHelper = invoker.createBuildResultHelper()) { + BlazeCommandRunner runner = invoker.getCommandRunner(); + try (InputStream stream = + runner.runBlazeMod(project, builder, buildResultHelper, context)) { + return stream.readAllBytes(); + } + } + }); } }