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

fix: Support bazel 8 and --noenable_workspace mode #6990

Merged
merged 5 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
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 @@ -22,7 +22,7 @@
import com.google.idea.blaze.base.sync.aspects.strategy.AspectStrategy;
import com.google.idea.blaze.base.sync.aspects.strategy.AspectStrategy.OutputGroup;
import com.google.idea.blaze.base.sync.aspects.strategy.AspectStrategyBazel;
import com.google.idea.blaze.base.sync.aspects.strategy.AspectRepositoryProvider;
import com.google.idea.blaze.base.sync.aspects.strategy.OverrideFlags;
import java.io.File;
import java.nio.file.Paths;
import java.util.Collection;
Expand Down Expand Up @@ -55,12 +55,12 @@ public static void main(String[] a) throws Exception {
aspectStrategyBazel.getAspectFlag().get(),
String.format(
"%s=%s/%s/aspect",
AspectRepositoryProvider.OVERRIDE_REPOSITORY_FLAG,
OverrideFlags.overrideRepositoryFlag(false),
System.getenv("TEST_SRCDIR"),
System.getenv("TEST_WORKSPACE")),
String.format(
"%s=%s/%s/aspect_template",
AspectRepositoryProvider.OVERRIDE_REPOSITORY_TEMPLATE_FLAG,
OverrideFlags.overrideRepositoryTemplateFlag(false),
System.getenv("TEST_SRCDIR"),
System.getenv("TEST_WORKSPACE"))
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.google.idea.blaze.base.sync.aspects.strategy;

import com.google.idea.blaze.base.model.BlazeProjectData;
import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager;
import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project;

Expand All @@ -11,8 +13,6 @@ public interface AspectRepositoryProvider {
ExtensionPointName<AspectRepositoryProvider> EP_NAME =
ExtensionPointName.create("com.google.idea.blaze.AspectRepositoryProvider");

String OVERRIDE_REPOSITORY_FLAG = "--override_repository=intellij_aspect";
String OVERRIDE_REPOSITORY_TEMPLATE_FLAG = "--override_repository=intellij_aspect_template";

Optional<File> aspectDirectory();

Expand Down Expand Up @@ -41,17 +41,24 @@ static Optional<File> findAspectTemplateDirectory() {
}

static Optional<String>[] getOverrideFlags(Project project) {
return new Optional[] {
getOverrideFlagForAspectDirectory(),
getOverrideFlagForProjectAspectDirectory(project),

Optional<BlazeProjectData> projectData =
Optional.ofNullable(BlazeProjectDataManager.getInstance(project))
.flatMap(it -> Optional.ofNullable(it.getBlazeProjectData()));
boolean useInjectedRepository = projectData
.map(it -> it.getBlazeVersionData().bazelIsAtLeastVersion(8, 0, 0))
.orElse(false); //fall back to false, as override_repository is available for all bazel versions
return new Optional[]{
getOverrideFlagForAspectDirectory(useInjectedRepository),
getOverrideFlagForProjectAspectDirectory(project, useInjectedRepository),
};
}

private static Optional<String> getOverrideFlagForAspectDirectory() {
return findAspectDirectory().map(it -> OVERRIDE_REPOSITORY_FLAG + "=" + it.getPath());
private static Optional<String> getOverrideFlagForAspectDirectory(boolean useInjectedRepository) {
return findAspectDirectory().map(it -> OverrideFlags.overrideRepositoryFlag(useInjectedRepository) + "=" + it.getPath());
}

private static Optional<String> getOverrideFlagForProjectAspectDirectory(Project project) {
return getProjectAspectDirectory(project).map(it -> OVERRIDE_REPOSITORY_TEMPLATE_FLAG + "=" + it.getPath());
private static Optional<String> getOverrideFlagForProjectAspectDirectory(Project project, boolean useInjectedRepository) {
return getProjectAspectDirectory(project).map(it -> OverrideFlags.overrideRepositoryTemplateFlag(useInjectedRepository) + "=" + it.getPath());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ public Optional<File> aspectTemplateDirectory() {
@VisibleForTesting
public AspectStrategyBazel(BlazeVersionData versionData) {
super(/* aspectSupportsDirectDepsTrimming= */ true);
if (versionData.bazelIsAtLeastVersion(6, 0, 0)) {
boolean useInjectedRepository = versionData.bazelIsAtLeastVersion(8, 0, 0);
if (useInjectedRepository) {
aspectFlag = "--aspects=@intellij_aspect//:intellij_info_bundled.bzl%intellij_info_aspect";
Copy link
Collaborator

@mai93 mai93 Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we have to bring this back to a single @?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else if (versionData.bazelIsAtLeastVersion(6, 0, 0)) {
aspectFlag = "--aspects=@@intellij_aspect//:intellij_info_bundled.bzl%intellij_info_aspect";
} else {
aspectFlag = "--aspects=@intellij_aspect//:intellij_info_bundled.bzl%intellij_info_aspect";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.google.idea.blaze.base.sync.aspects.strategy;

import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

public class OverrideFlags {
@Contract(pure = true)
static @NotNull String newRepositoryFlag(boolean useInjectedRepository) {
if (useInjectedRepository) {
return "--inject_repository";
} else {
return "--override_repository";
}
}

public static @NotNull String overrideRepositoryFlag(boolean useInjectedRepository) {
return String.format("%s=intellij_aspect", newRepositoryFlag(useInjectedRepository));
}

public static @NotNull String overrideRepositoryTemplateFlag(boolean useInjectedRepository) {
return String.format("%s=intellij_aspect_template", newRepositoryFlag(useInjectedRepository));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ final class BazelFastBuildAspectStrategy extends FastBuildAspectStrategy {
@Override
protected List<String> getAspectFlags(BlazeVersionData versionData, Project project) {
String intellijAspectFile;
if (versionData.bazelIsAtLeastVersion(6, 0, 0)) {
boolean useInjectedRepository = versionData.bazelIsAtLeastVersion(8, 0, 0);
if(useInjectedRepository) {
intellijAspectFile = "--aspects=@intellij_aspect//:fast_build_info_bundled.bzl%fast_build_info_aspect";
} else if (versionData.bazelIsAtLeastVersion(6, 0, 0)) {
intellijAspectFile = "--aspects=@@intellij_aspect//:fast_build_info_bundled.bzl%fast_build_info_aspect";
} else {
intellijAspectFile = "--aspects=@intellij_aspect//:fast_build_info_bundled.bzl%fast_build_info_aspect";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -61,9 +60,12 @@ public boolean isApplicable(BlazeVersionData versionData) {
@Override
public ImmutableList<String> getBuildFlags(BlazeVersionData versionData, Project project) {
String intellijAspect;
if (versionData.bazelIsAtLeastVersion(6, 0, 0)) {
boolean useInjectedRepository = versionData.bazelIsAtLeastVersion(8, 0, 0);
if (useInjectedRepository) {
intellijAspect = "--aspects=@intellij_aspect//:java_classpath.bzl%java_classpath_aspect";
} else if (versionData.bazelIsAtLeastVersion(6, 0, 0)) {
intellijAspect = "--aspects=@@intellij_aspect//:java_classpath.bzl%java_classpath_aspect";
} else {
} else { // #bazel5 we are going to drop bazel 5 support in Feb 2025
intellijAspect = "--aspects=@intellij_aspect//:java_classpath.bzl%java_classpath_aspect";
}

Expand Down
Loading