From 3bc2aa11fcf63bf6e60467db43fc6a1be078b9fe Mon Sep 17 00:00:00 2001 From: Vaidas Pilkauskas Date: Fri, 22 Dec 2023 09:31:27 +0200 Subject: [PATCH] Revert "Update plugin-building workflow to support remote builds" This reverts commit 8df620877669e1b89b15f5ffde3cb421cab2e073. --- .../run/BlazeIntellijPluginConfiguration.java | 5 +- .../run/BlazeIntellijPluginDeployer.java | 119 ++++++++---------- .../run/BuildPluginBeforeRunTaskProvider.java | 86 +++++++++---- 3 files changed, 116 insertions(+), 94 deletions(-) diff --git a/plugin_dev/src/com/google/idea/blaze/plugin/run/BlazeIntellijPluginConfiguration.java b/plugin_dev/src/com/google/idea/blaze/plugin/run/BlazeIntellijPluginConfiguration.java index 3905817b0c9..e8d4cd2df61 100644 --- a/plugin_dev/src/com/google/idea/blaze/plugin/run/BlazeIntellijPluginConfiguration.java +++ b/plugin_dev/src/com/google/idea/blaze/plugin/run/BlazeIntellijPluginConfiguration.java @@ -188,7 +188,8 @@ public RunProfileState getState(Executor executor, ExecutionEnvironment env) throw new ExecutionException("No sandbox specified for IntelliJ Platform Plugin SDK", e); } String buildNumber = IdeaJdkHelper.getBuildNumber(ideaJdk); - final BlazeIntellijPluginDeployer deployer = new BlazeIntellijPluginDeployer(sandboxHome); + final BlazeIntellijPluginDeployer deployer = + new BlazeIntellijPluginDeployer(sandboxHome, target); env.putUserData(BlazeIntellijPluginDeployer.USER_DATA_KEY, deployer); // copy license from running instance of idea @@ -197,7 +198,7 @@ public RunProfileState getState(Executor executor, ExecutionEnvironment env) return new JavaCommandLineState(env) { @Override protected JavaParameters createJavaParameters() throws ExecutionException { - DeployedPluginInfo deployedPluginInfo = deployer.deployNonBlocking(buildSystem); + DeployedPluginInfo deployedPluginInfo = deployer.deployNonBlocking(); final JavaParameters params = new JavaParameters(); diff --git a/plugin_dev/src/com/google/idea/blaze/plugin/run/BlazeIntellijPluginDeployer.java b/plugin_dev/src/com/google/idea/blaze/plugin/run/BlazeIntellijPluginDeployer.java index ebd96f174e8..e19678c3fac 100644 --- a/plugin_dev/src/com/google/idea/blaze/plugin/run/BlazeIntellijPluginDeployer.java +++ b/plugin_dev/src/com/google/idea/blaze/plugin/run/BlazeIntellijPluginDeployer.java @@ -15,7 +15,6 @@ */ package com.google.idea.blaze.plugin.run; -import static com.google.common.collect.ImmutableList.toImmutableList; import static java.nio.charset.StandardCharsets.UTF_8; import com.google.common.collect.ImmutableList; @@ -23,29 +22,30 @@ import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; import com.google.common.collect.Maps; -import com.google.common.io.Files; import com.google.devtools.intellij.plugin.IntellijPluginTargetDeployInfo.IntellijPluginDeployFile; import com.google.devtools.intellij.plugin.IntellijPluginTargetDeployInfo.IntellijPluginDeployInfo; import com.google.idea.blaze.base.async.executor.BlazeExecutor; +import com.google.idea.blaze.base.command.buildresult.BlazeArtifact; +import com.google.idea.blaze.base.command.buildresult.BuildResultHelper; import com.google.idea.blaze.base.command.buildresult.BuildResultHelper.GetArtifactsException; import com.google.idea.blaze.base.command.buildresult.OutputArtifact; -import com.google.idea.blaze.base.sync.aspects.BlazeBuildOutputs; +import com.google.idea.blaze.base.io.FileOperationProvider; +import com.google.idea.blaze.base.model.primitives.Label; import com.google.idea.common.experiments.BoolExperiment; import com.google.protobuf.TextFormat; import com.intellij.concurrency.AsyncUtil; import com.intellij.execution.ExecutionException; import com.intellij.openapi.util.Key; -import com.intellij.openapi.util.text.StringUtil; import java.io.BufferedInputStream; import java.io.File; +import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; +import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.List; -import java.util.Locale; import java.util.Map; import java.util.concurrent.Future; @@ -59,14 +59,16 @@ class BlazeIntellijPluginDeployer { Key.create(BlazeIntellijPluginDeployer.class.getName()); private final String sandboxHome; - private final Map buildArtifactsMap = new HashMap<>(); - private final List deployInfoArtifacts = new ArrayList<>(); - private final Map filesToDeploy = Maps.newHashMap(); + private final Label pluginTarget; + private final List deployInfoFiles = new ArrayList<>(); + private final Map filesToDeploy = Maps.newHashMap(); + private File executionRoot; private Future fileCopyingTask; - BlazeIntellijPluginDeployer(String sandboxHome) { + BlazeIntellijPluginDeployer(String sandboxHome, Label pluginTarget) { this.sandboxHome = sandboxHome; + this.pluginTarget = pluginTarget; } /** @@ -74,49 +76,49 @@ class BlazeIntellijPluginDeployer { * previously built plugin. */ void buildStarted() { - deployInfoArtifacts.clear(); - buildArtifactsMap.clear(); + executionRoot = null; + deployInfoFiles.clear(); } - void reportBuildComplete(BlazeBuildOutputs blazeBuildOutputs) throws GetArtifactsException { - ImmutableList buildArtifacts = - blazeBuildOutputs.artifacts.values().stream() - .map(a -> a.artifact) - .collect(toImmutableList()); - buildArtifactsMap.clear(); - buildArtifacts.forEach(a -> buildArtifactsMap.put(a.getRelativePath(), a)); - - deployInfoArtifacts.clear(); - deployInfoArtifacts.addAll( - buildArtifacts.stream() - .filter(a -> a.toString().endsWith(".intellij-plugin-debug-target-deploy-info")) - .collect(toImmutableList())); + void reportBuildComplete(File executionRoot, BuildResultHelper buildResultHelper) + throws GetArtifactsException { + this.executionRoot = executionRoot; + deployInfoFiles.clear(); + ImmutableList outputs = + buildResultHelper.getBuildArtifactsForTarget( + pluginTarget, file -> file.endsWith(".intellij-plugin-debug-target-deploy-info")); + deployInfoFiles.addAll(BlazeArtifact.getLocalFiles(outputs)); } /** * Returns information about which plugins will be deployed, and asynchronously copies the * corresponding files to the sandbox. */ - DeployedPluginInfo deployNonBlocking(String buildSystem) throws ExecutionException { - if (deployInfoArtifacts.isEmpty()) { + DeployedPluginInfo deployNonBlocking() throws ExecutionException { + if (deployInfoFiles.isEmpty()) { throw new ExecutionException("No plugin files found. Did the build fail?"); } List deployInfoList = Lists.newArrayList(); - for (OutputArtifact deployInfoFile : deployInfoArtifacts) { + for (File deployInfoFile : deployInfoFiles) { deployInfoList.addAll(readDeployInfoFromFile(deployInfoFile)); } - ImmutableMap filesToDeploy = - getFilesToDeploy(deployInfoList, buildSystem); + ImmutableMap filesToDeploy = getFilesToDeploy(executionRoot, deployInfoList); this.filesToDeploy.putAll(filesToDeploy); ImmutableSet javaAgentJars = deployJavaAgents.getValue() ? listJavaAgentFiles(deployInfoList) : ImmutableSet.of(); + for (File file : filesToDeploy.keySet()) { + if (!file.exists()) { + throw new ExecutionException( + String.format("Plugin file '%s' not found. Did the build fail?", file.getName())); + } + } // kick off file copying task asynchronously, so it doesn't block the EDT. fileCopyingTask = BlazeExecutor.getInstance() .submit( () -> { - for (Map.Entry entry : filesToDeploy.entrySet()) { + for (Map.Entry entry : filesToDeploy.entrySet()) { copyFileToSandbox(entry.getKey(), entry.getValue()); } return null; @@ -139,10 +141,10 @@ void deleteDeployment() { } } - private static ImmutableList readDeployInfoFromFile( - OutputArtifact deployInfoArtifact) throws ExecutionException { + private static ImmutableList readDeployInfoFromFile(File deployInfoFile) + throws ExecutionException { ImmutableList.Builder result = ImmutableList.builder(); - try (InputStream inputStream = deployInfoArtifact.getInputStream()) { + try (InputStream inputStream = new BufferedInputStream(new FileInputStream(deployInfoFile))) { IntellijPluginDeployInfo.Builder builder = IntellijPluginDeployInfo.newBuilder(); TextFormat.Parser parser = TextFormat.Parser.newBuilder().setAllowUnknownFields(true).build(); parser.merge(new InputStreamReader(inputStream, UTF_8), builder); @@ -154,45 +156,24 @@ private static ImmutableList readDeployInfoFromFile( return result.build(); } - private ImmutableMap getFilesToDeploy( - Collection deployInfos, String buildSystem) - throws ExecutionException { - ImmutableMap.Builder result = ImmutableMap.builder(); + private ImmutableMap getFilesToDeploy( + File executionRoot, Collection deployInfos) { + ImmutableMap.Builder result = ImmutableMap.builder(); for (IntellijPluginDeployInfo deployInfo : deployInfos) { for (IntellijPluginDeployFile deployFile : deployInfo.getDeployFilesList()) { - result.put( - getArtifactFromDeployFile(deployFile, buildSystem), - new File(sandboxPluginDirectory(sandboxHome), deployFile.getDeployLocation())); + File src = new File(executionRoot, deployFile.getExecutionPath()); + File dest = new File(sandboxPluginDirectory(sandboxHome), deployFile.getDeployLocation()); + result.put(src, dest); } for (IntellijPluginDeployFile deployFile : deployInfo.getJavaAgentDeployFilesList()) { - result.put( - getArtifactFromDeployFile(deployFile, buildSystem), - new File(sandboxPluginDirectory(sandboxHome), deployFile.getDeployLocation())); + File src = new File(executionRoot, deployFile.getExecutionPath()); + File dest = new File(sandboxPluginDirectory(sandboxHome), deployFile.getDeployLocation()); + result.put(src, dest); } } return result.build(); } - private OutputArtifact getArtifactFromDeployFile( - IntellijPluginDeployFile deployFile, String buildSystem) throws ExecutionException { - String relativePath = - buildArtifactsMap.keySet().stream() - .filter( - key -> - key.endsWith( - StringUtil.trimStart( - deployFile.getExecutionPath(), - String.format("%s-out/", buildSystem.toLowerCase(Locale.ROOT))))) - .findAny() - .orElseThrow( - () -> - new ExecutionException( - String.format( - "Plugin file '%s' not found. Did the build fail?", - deployFile.getExecutionPath()))); - return buildArtifactsMap.get(relativePath); - } - private ImmutableSet listJavaAgentFiles(Collection deployInfos) { ImmutableSet.Builder result = ImmutableSet.builder(); for (IntellijPluginDeployInfo deployInfo : deployInfos) { @@ -207,12 +188,10 @@ private static File sandboxPluginDirectory(String sandboxHome) { return new File(sandboxHome, "plugins"); } - private static void copyFileToSandbox(OutputArtifact deployArtifact, File dest) - throws ExecutionException { - try (BufferedInputStream stream = deployArtifact.getInputStream()) { - boolean unused = dest.getParentFile().mkdirs(); - Files.write(stream.readAllBytes(), dest); - unused = dest.setExecutable(true, true); + private static void copyFileToSandbox(File src, File dest) throws ExecutionException { + try { + dest.getParentFile().mkdirs(); + FileOperationProvider.getInstance().copy(src, dest, StandardCopyOption.REPLACE_EXISTING); dest.deleteOnExit(); } catch (IOException e) { throw new ExecutionException("Error copying plugin file to sandbox", e); diff --git a/plugin_dev/src/com/google/idea/blaze/plugin/run/BuildPluginBeforeRunTaskProvider.java b/plugin_dev/src/com/google/idea/blaze/plugin/run/BuildPluginBeforeRunTaskProvider.java index 9cdd9da66cd..0797b025ead 100644 --- a/plugin_dev/src/com/google/idea/blaze/plugin/run/BuildPluginBeforeRunTaskProvider.java +++ b/plugin_dev/src/com/google/idea/blaze/plugin/run/BuildPluginBeforeRunTaskProvider.java @@ -18,7 +18,8 @@ import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.google.idea.blaze.base.async.executor.ProgressiveTaskWithProgressIndicator; -import com.google.idea.blaze.base.bazel.BuildSystem.BuildInvoker; +import com.google.idea.blaze.base.async.process.ExternalTask; +import com.google.idea.blaze.base.async.process.LineProcessingOutputStream; import com.google.idea.blaze.base.command.BlazeCommand; import com.google.idea.blaze.base.command.BlazeCommandName; import com.google.idea.blaze.base.command.BlazeFlags; @@ -26,6 +27,10 @@ import com.google.idea.blaze.base.command.BlazeInvocationContext.ContextType; import com.google.idea.blaze.base.command.buildresult.BuildResultHelper; import com.google.idea.blaze.base.command.buildresult.BuildResultHelper.GetArtifactsException; +import com.google.idea.blaze.base.command.buildresult.BuildResultHelperProvider; +import com.google.idea.blaze.base.command.info.BlazeInfo; +import com.google.idea.blaze.base.command.info.BlazeInfoRunner; +import com.google.idea.blaze.base.console.BlazeConsoleLineProcessorProvider; import com.google.idea.blaze.base.experiments.ExperimentScope; import com.google.idea.blaze.base.filecache.FileCaches; import com.google.idea.blaze.base.issueparser.BlazeIssueParser; @@ -47,7 +52,6 @@ import com.google.idea.blaze.base.sync.aspects.BuildResult; import com.google.idea.blaze.base.sync.data.BlazeProjectDataManager; import com.google.idea.blaze.base.util.SaveUtil; -import com.google.idea.blaze.exception.BuildException; import com.intellij.execution.BeforeRunTask; import com.intellij.execution.BeforeRunTaskProvider; import com.intellij.execution.configurations.RunConfiguration; @@ -56,6 +60,7 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.util.Key; import icons.BlazeIcons; +import java.io.File; import java.util.concurrent.CancellationException; import java.util.concurrent.ExecutionException; import javax.annotation.Nullable; @@ -150,6 +155,7 @@ public final boolean executeTask( BlazeUserSettings userSettings = BlazeUserSettings.getInstance(); return Scope.root( context -> { + WorkspaceRoot workspaceRoot = WorkspaceRoot.fromProject(project); context .push(new ExperimentScope()) .push(new ProblemsViewScope(project, userSettings.getShowProblemsViewOnRun())) @@ -188,8 +194,37 @@ public final boolean executeTask( new ScopedTask(context) { @Override protected Void execute(BlazeContext context) { + String binaryPath = Blaze.getBuildSystemProvider(project).getBinaryPath(project); BlazeIntellijPluginConfiguration config = (BlazeIntellijPluginConfiguration) configuration; + + ListenableFuture executionRootFuture = + BlazeInfoRunner.getInstance() + .runBlazeInfo( + project, + Blaze.getBuildSystemProvider(project) + .getBuildSystem() + .getDefaultInvoker(project, context), + context, + config.getBlazeFlagsState().getFlagsForExternalProcesses(), + BlazeInfo.EXECUTION_ROOT_KEY); + + String executionRoot; + try { + executionRoot = executionRootFuture.get(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + context.setCancelled(); + return null; + } catch (ExecutionException e) { + IssueOutput.error(e.getMessage()).submit(context); + context.setHasError(); + return null; + } + if (executionRoot == null) { + IssueOutput.error("Could not determine execution root").submit(context); + return null; + } BlazeProjectData blazeProjectData = BlazeProjectDataManager.getInstance(project).getBlazeProjectData(); if (blazeProjectData == null) { @@ -197,13 +232,12 @@ protected Void execute(BlazeContext context) { return null; } - BuildInvoker invoker = - Blaze.getBuildSystemProvider(project) - .getBuildSystem() - .getBuildInvoker(project, context); - try (BuildResultHelper buildResultHelper = invoker.createBuildResultHelper()) { - BlazeCommand.Builder command = - BlazeCommand.builder(invoker, BlazeCommandName.BUILD) + // Explicitly create a local build helper because deployer.reportBuildComplete + // expects the outputs to be available locally + try (BuildResultHelper buildResultHelper = + BuildResultHelperProvider.createForLocalBuild(project)) { + BlazeCommand command = + BlazeCommand.builder(binaryPath, BlazeCommandName.BUILD) .addTargets(config.getTargets()) .addBlazeFlags( BlazeFlags.blazeFlags( @@ -218,33 +252,38 @@ protected Void execute(BlazeContext context) { .addBlazeFlags( config.getBlazeFlagsState().getFlagsForExternalProcesses()) .addExeFlags(config.getExeFlagsState().getFlagsForExternalProcesses()) - .addBlazeFlags(buildResultHelper.getBuildFlags()); - + .addBlazeFlags(buildResultHelper.getBuildFlags()) + .build(); if (command == null || context.hasErrors() || context.isCancelled()) { return null; } SaveUtil.saveAllFiles(); - BlazeBuildOutputs outputs = - invoker - .getCommandRunner() - .run(project, command, buildResultHelper, context); - if (!outputs.buildResult.equals(BuildResult.SUCCESS)) { + int retVal = + ExternalTask.builder(workspaceRoot) + .addBlazeCommand(command) + .context(context) + .stderr( + LineProcessingOutputStream.of( + BlazeConsoleLineProcessorProvider.getAllStderrLineProcessors( + context))) + .build() + .run(); + if (retVal != 0) { context.setHasError(); } ListenableFuture unusedFuture = FileCaches.refresh( - project, context, BlazeBuildOutputs.noOutputs(outputs.buildResult)); + project, + context, + BlazeBuildOutputs.noOutputs(BuildResult.fromExitCode(retVal))); try { - deployer.reportBuildComplete(outputs); + deployer.reportBuildComplete(new File(executionRoot), buildResultHelper); } catch (GetArtifactsException e) { IssueOutput.error("Failed to get build artifacts: " + e.getMessage()) .submit(context); return null; } return null; - } catch (BuildException e) { - context.handleException("Failed to build", e); - return null; } } }; @@ -262,7 +301,10 @@ protected Void execute(BlazeContext context) { context.setCancelled(); } - return !context.hasErrors() && !context.isCancelled(); + if (context.hasErrors() || context.isCancelled()) { + return false; + } + return true; }); } }