diff --git a/aswb/src/com/google/idea/blaze/android/libraries/AndroidLintCollector.java b/aswb/src/com/google/idea/blaze/android/libraries/AndroidLintCollector.java index c328ec154a6..ecaeda82181 100644 --- a/aswb/src/com/google/idea/blaze/android/libraries/AndroidLintCollector.java +++ b/aswb/src/com/google/idea/blaze/android/libraries/AndroidLintCollector.java @@ -24,12 +24,15 @@ import com.google.idea.blaze.base.sync.libraries.BlazeLibraryCollector; import com.google.idea.blaze.base.sync.libraries.LintCollector; import com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder; +import com.google.idea.common.experiments.FeatureRolloutExperiment; import com.intellij.openapi.project.Project; import java.io.File; import java.util.Objects; /** {@inheritDoc} Collecting lint rule jars from {@code AarLibrary} */ public class AndroidLintCollector implements LintCollector { + public static final FeatureRolloutExperiment lintEnabled = + new FeatureRolloutExperiment("blaze.android.libraries.lint.enabled"); @Override public ImmutableList collectLintJars(Project project, BlazeProjectData blazeProjectData) { @@ -42,4 +45,9 @@ public ImmutableList collectLintJars(Project project, BlazeProjectData bla .filter(Objects::nonNull) .collect(toImmutableList()); } + + @Override + public boolean isEnabled() { + return lintEnabled.isEnabled(); + } } diff --git a/base/src/com/google/idea/blaze/base/sync/libraries/LintCollector.java b/base/src/com/google/idea/blaze/base/sync/libraries/LintCollector.java index 9e4f06e94ec..64290466e04 100644 --- a/base/src/com/google/idea/blaze/base/sync/libraries/LintCollector.java +++ b/base/src/com/google/idea/blaze/base/sync/libraries/LintCollector.java @@ -31,9 +31,12 @@ public interface LintCollector { ImmutableList collectLintJars(Project project, BlazeProjectData blazeProjectData); + boolean isEnabled(); + static ImmutableList getLintJars(Project project, BlazeProjectData blazeProjectData) { return EP_NAME .extensions() + .filter(ep -> ep.isEnabled()) .map(ep -> ep.collectLintJars(project, blazeProjectData)) .flatMap(List::stream) .collect(toImmutableList()); diff --git a/common/experiments/src/com/google/idea/common/experiments/experiment.properties b/common/experiments/src/com/google/idea/common/experiments/experiment.properties index f28ea86bce7..1cf8db91bdb 100644 --- a/common/experiments/src/com/google/idea/common/experiments/experiment.properties +++ b/common/experiments/src/com/google/idea/common/experiments/experiment.properties @@ -5,4 +5,8 @@ # Flags used by ASwB # Use Studio's deployer: b/197761450 -aswb.use.studio.deployer=0 \ No newline at end of file +aswb.use.studio.deployer=0 + +# Enable lint check +blaze.java.libraries.lint.enabled=10 +blaze.android.libraries.lint.enabled=10 \ No newline at end of file diff --git a/java/src/com/google/idea/blaze/java/libraries/JavaLintCollector.java b/java/src/com/google/idea/blaze/java/libraries/JavaLintCollector.java deleted file mode 100644 index b864a26edee..00000000000 --- a/java/src/com/google/idea/blaze/java/libraries/JavaLintCollector.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright 2022 The Bazel Authors. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.idea.blaze.java.libraries; - -import static com.google.common.collect.ImmutableList.toImmutableList; -import static java.nio.charset.StandardCharsets.UTF_8; -import static java.util.stream.Collectors.joining; - -import com.google.common.collect.ImmutableList; -import com.google.idea.blaze.base.command.buildresult.BlazeArtifact; -import com.google.idea.blaze.base.io.FileOperationProvider; -import com.google.idea.blaze.base.model.BlazeProjectData; -import com.google.idea.blaze.base.sync.libraries.LintCollector; -import com.google.idea.blaze.base.sync.workspace.ArtifactLocationDecoder; -import com.google.idea.blaze.java.sync.model.BlazeJavaSyncData; -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.project.Project; -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.util.List; -import java.util.jar.Attributes; -import java.util.jar.JarOutputStream; -import java.util.jar.Manifest; -import java.util.zip.ZipEntry; -import javax.annotation.Nullable; - -/** {@inheritDoc} Collecting lint rule jars from {@code BlazeJavaSyncData} */ -public class JavaLintCollector implements LintCollector { - private static final Logger logger = Logger.getInstance(JarCache.class); - - private static final String ISSUE_REGISTRY_LOADER_RESOURCE_NAME = - "META-INF/services/com.android.tools.lint.client.api.IssueRegistry"; - private static final String PLUGIN_ISSUE_REGISTRY = - "com.google.android.tools.lint.registration.ServiceLoadingIssueRegistry"; - - public static ImmutableList collectLintJarsArtifacts( - BlazeProjectData blazeProjectData) { - BlazeJavaSyncData syncData = blazeProjectData.getSyncState().get(BlazeJavaSyncData.class); - ArtifactLocationDecoder artifactLocationDecoder = blazeProjectData.getArtifactLocationDecoder(); - - if (syncData == null) { - return ImmutableList.of(); - } - return syncData.getImportResult().pluginProcessorJars.stream() - .map(artifactLocationDecoder::resolveOutput) - .collect(toImmutableList()); - } - - @Override - public ImmutableList collectLintJars(Project project, BlazeProjectData blazeProjectData) { - JarCache jarCache = JarCache.getInstance(project); - FileOperationProvider fileOperationProvider = FileOperationProvider.getInstance(); - ImmutableList jars = - collectLintJarsArtifacts(blazeProjectData).stream() - .map(jarCache::getCachedJar) - .filter(jar -> (jar != null) && fileOperationProvider.exists(jar)) - .collect(toImmutableList()); - try { - File lintChecksJar = - makeLintChecksJar(JarCacheFolderProvider.getInstance(project).getJarCacheFolder(), jars); - return lintChecksJar == null ? ImmutableList.of() : ImmutableList.of(lintChecksJar); - } catch (IOException e) { - logger.warn("Fail to zip lint jars", e); - return jars; - } - } - - /** - * Create a lint jar that can be used to load all plugin jars. This is a temp workaround until - * b/203563664 get fixed. This piece of code follows {@code - * com.google.devtools.jvmtools.lint.runner.AndroidLintRunner#makeLintChecksJar} - */ - @Nullable - static File makeLintChecksJar(File dir, List pluginJars) throws IOException { - if (pluginJars.isEmpty()) { - return null; - } - - // To avoid merging plugin Jars we make a Jar with "Class-Path" manifest entry. IntelliJ's - // classloader expects that Jar to be named "classpath...jar" and contain absolute file URIs. - Path result = dir.toPath().resolve("classpath.jar"); - Manifest manifest = new Manifest(); - // Must set Manifest-Version, otherwise empty manifest is written :( - manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0"); - manifest - .getMainAttributes() - .put( - Attributes.Name.CLASS_PATH, - pluginJars.stream() - .map(jar -> "file://" + jar.getAbsolutePath()) - .collect(joining(" "))); - - // Make sure Lint tries to load our Lint rule registry. IntelliJ's UrlClassLoader doesn't find - // META-INF services files in Class-Path Jars, so reproduce in main Jar. - // TODO(b/143231996): skip creating Jar when IssueRegistry not in classpath - try (JarOutputStream jar = new JarOutputStream(Files.newOutputStream(result), manifest)) { - jar.putNextEntry(new ZipEntry(ISSUE_REGISTRY_LOADER_RESOURCE_NAME)); - jar.write((PLUGIN_ISSUE_REGISTRY + "\n").getBytes(UTF_8)); - jar.closeEntry(); - } - return result.toFile(); - } -}