From 713eb087a0bb1737fa2a0f7f06c51923488b16a5 Mon Sep 17 00:00:00 2001 From: Sameer Sundresh Date: Fri, 7 Jun 2019 16:13:37 -0600 Subject: [PATCH] If there are a large number of target specs, pass them to pants via a file to avoid exceeding the OS command line length limit. (#404) If there are a large number of target specs, pass them to pants via a file to avoid exceeding the OS command line length limit. Specifically uses a threshold of 100 right now, which is directly in the code as opposed to being defined in a constants file. This seems to be consistent with the rest of the plugin code, which is littered with strings, although there is at least one shared constant `PantsCompileOptionsExecutor.PROJECT_NAME_LIMIT`. In practice, the threshold of 100 should not make a difference to the rest of the plugin code. Does not currently include any unit tests. In principle it seems like it should be possible to unit test `PantsCompileOptionsExecutor.getPantsExportCommand()`. --- .../service/PantsCompileOptionsExecutor.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/com/twitter/intellij/pants/service/PantsCompileOptionsExecutor.java b/src/com/twitter/intellij/pants/service/PantsCompileOptionsExecutor.java index 13fcf791f..5290b7d1c 100644 --- a/src/com/twitter/intellij/pants/service/PantsCompileOptionsExecutor.java +++ b/src/com/twitter/intellij/pants/service/PantsCompileOptionsExecutor.java @@ -28,6 +28,7 @@ import org.jetbrains.annotations.TestOnly; import java.io.File; +import java.io.FileWriter; import java.io.IOException; import java.util.Collections; import java.util.List; @@ -196,7 +197,7 @@ private ProcessOutput getProcessOutput( } @NotNull - private GeneralCommandLine getPantsExportCommand(final File outputFile, @NotNull Consumer statusConsumer) { + private GeneralCommandLine getPantsExportCommand(final File outputFile, @NotNull Consumer statusConsumer) throws IOException { final GeneralCommandLine commandLine = PantsUtil.defaultCommandLine(getProjectPath()); // Grab the import stage pants rc file for IntelliJ. @@ -210,7 +211,21 @@ private GeneralCommandLine getPantsExportCommand(final File outputFile, @NotNull commandLine.addParameter("--export-libraries-sources"); commandLine.addParameter("--export-libraries-javadocs"); } - commandLine.addParameters(getTargetSpecs()); + // If there are a large number of target specs, pass them to pants via a + // file to avoid exceeding the OS command line length limit. + final List targetSpecs = getTargetSpecs(); + if (targetSpecs.size() > 100) { + final File targetSpecsFile = FileUtil.createTempFile("pants_target_specs", ".in"); + try (FileWriter targetSpecsFileWriter = new FileWriter(targetSpecsFile)) { + for (String targetSpec : targetSpecs) { + targetSpecsFileWriter.write(targetSpec); + targetSpecsFileWriter.write('\n'); + } + } + commandLine.addParameter("--target-spec-file=" + targetSpecsFile.getPath()); + } else { + commandLine.addParameters(targetSpecs); + } commandLine.addParameter("--export-output-file=" + outputFile.getPath()); return commandLine; }