From 797ccfd54c261dd3848b0b76a7ee8132cb1ef0bd Mon Sep 17 00:00:00 2001 From: Thevakumar-Luheerathan Date: Fri, 5 Apr 2024 12:44:48 +0530 Subject: [PATCH 1/2] Exit RunBuildToolsTask upon tool error --- .../java/io/ballerina/cli/task/CompileTask.java | 7 ------- .../io/ballerina/cli/task/RunBuildToolsTask.java | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java index 675705a3abc8..ba0bd8f7aeba 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CompileTask.java @@ -31,7 +31,6 @@ import io.ballerina.projects.ProjectException; import io.ballerina.projects.ProjectKind; import io.ballerina.projects.SemanticVersion; -import io.ballerina.projects.buildtools.ToolContext; import io.ballerina.projects.directory.SingleFileProject; import io.ballerina.projects.environment.ResolutionOptions; import io.ballerina.projects.internal.PackageDiagnostic; @@ -230,12 +229,6 @@ public void execute(Project project) { }); // Add tool resolution diagnostics to diagnostics diagnostics.addAll(project.currentPackage().getBuildToolResolution().getDiagnosticList()); - // Report build tool execution diagnostics - if (project.getToolContextMap() != null) { - for (ToolContext tool : project.getToolContextMap().values()) { - diagnostics.addAll(tool.diagnostics()); - } - } boolean hasErrors = false; for (Diagnostic d : diagnostics) { if (d.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) { diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/RunBuildToolsTask.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/RunBuildToolsTask.java index 4c3e21cff94e..cdd828044ac6 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/RunBuildToolsTask.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/RunBuildToolsTask.java @@ -57,6 +57,7 @@ import java.net.URLClassLoader; import java.nio.file.Files; import java.nio.file.Path; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; @@ -103,6 +104,7 @@ public void execute(Project project) { .diagnostics().stream().filter(diagnostic -> diagnostic.diagnosticInfo().code() .startsWith(TOOL_DIAGNOSTIC_CODE_PREFIX)).toList(); toolManifestDiagnostics.forEach(outStream::println); + List toolDiagnostics = new ArrayList<>(toolManifestDiagnostics); // Read the build tool entries specified the Ballerina.toml List toolEntries = project.currentPackage().manifest().tools(); @@ -126,6 +128,7 @@ public void execute(Project project) { return; } buildToolResolution.getDiagnosticList().forEach(outStream::println); + toolDiagnostics.addAll(buildToolResolution.getDiagnosticList()); List resolvedTools = buildToolResolution.getResolvedTools(); List centralDeliveredResolvedTools = resolvedTools.stream().filter(tool -> !DEFAULT_VERSION .equals(tool.version().toString())).toList(); @@ -196,6 +199,7 @@ public void execute(Project project) { try { this.outStream.printf("\t%s(%s)%n", toolEntry.type().value(), toolEntry.id().value()); targetTool.get().execute(toolContext); + toolDiagnostics.addAll(toolContext.diagnostics()); for (Diagnostic d : toolContext.diagnostics()) { if (d.toString().contains("(1:1,1:1)")) { outStream.println(new PackageDiagnostic(d.diagnosticInfo(), toolContext.toolId())); @@ -207,6 +211,17 @@ public void execute(Project project) { throw createLauncherException(e.getMessage()); } } + + // Exit if there is any error diagnostic + boolean hasErrors = false; + for (Diagnostic d : toolDiagnostics) { + if (d.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) { + hasErrors = true; + } + } + if (hasErrors) { + throw createLauncherException("compilation contains errors"); + } // Reload the project to load the generated code reloadProject(project); this.outStream.println(); From c956cef864d5760bb9b18424f929e7d144998470 Mon Sep 17 00:00:00 2001 From: Thevakumar-Luheerathan Date: Fri, 5 Apr 2024 14:06:15 +0530 Subject: [PATCH 2/2] Address the review --- .../java/io/ballerina/cli/task/RunBuildToolsTask.java | 10 +++------- .../java/io/ballerina/cli/cmd/BuildCommandTest.java | 8 ++++---- .../build-bal-project-with-build-tool-not-found.txt | 3 --- .../unix/build-tool-with-diagnostics.txt | 3 --- ...build-tool-with-invalid-missing-toml-properties.txt | 3 --- .../unix/build-tool-with-recurring-tool-properties.txt | 3 --- .../build-bal-project-with-build-tool-not-found.txt | 3 --- .../windows/build-tool-with-diagnostics.txt | 3 --- ...build-tool-with-invalid-missing-toml-properties.txt | 3 --- .../build-tool-with-recurring-tool-properties.txt | 3 --- 10 files changed, 7 insertions(+), 35 deletions(-) diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/RunBuildToolsTask.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/RunBuildToolsTask.java index cdd828044ac6..5abc581cd81c 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/RunBuildToolsTask.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/RunBuildToolsTask.java @@ -213,14 +213,10 @@ public void execute(Project project) { } // Exit if there is any error diagnostic - boolean hasErrors = false; - for (Diagnostic d : toolDiagnostics) { - if (d.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)) { - hasErrors = true; - } - } + boolean hasErrors = toolDiagnostics.stream() + .anyMatch(d -> d.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)); if (hasErrors) { - throw createLauncherException("compilation contains errors"); + throw createLauncherException("build tool execution contains errors"); } // Reload the project to load the generated code reloadProject(project); diff --git a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/BuildCommandTest.java b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/BuildCommandTest.java index c71edac9e3f1..ce663ee60b46 100644 --- a/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/BuildCommandTest.java +++ b/cli/ballerina-cli/src/test/java/io/ballerina/cli/cmd/BuildCommandTest.java @@ -1197,7 +1197,7 @@ public Object[][] provideToolPropertiesDiagnostics() { { "build-tool-with-invalid-missing-toml-properties", "build-tool-with-invalid-missing-toml-properties.txt", - "error: compilation contains errors" + "error: build tool execution contains errors" }, { "build-tool-with-invalid-missing-optional-toml-properties", @@ -1207,12 +1207,12 @@ public Object[][] provideToolPropertiesDiagnostics() { { "build-tool-with-diagnostics", "build-tool-with-diagnostics.txt", - "error: compilation contains errors" + "error: build tool execution contains errors" }, { "build-tool-with-recurring-tool-properties", "build-tool-with-recurring-tool-properties.txt", - "error: compilation contains errors" + "error: build tool execution contains errors" } }; } @@ -1259,7 +1259,7 @@ public void testBuildProjectWithBuildToolNotFound() throws IOException { String buildLog = readOutput(true); Assert.assertEquals(buildLog.replaceAll("\r", ""), getOutput("build-bal-project-with-build-tool-not-found.txt")); - Assert.assertEquals("error: compilation contains errors", e.getDetailedMessages().get(0)); + Assert.assertEquals("error: build tool execution contains errors", e.getDetailedMessages().get(0)); } } diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-bal-project-with-build-tool-not-found.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-bal-project-with-build-tool-not-found.txt index a7b66a43f10d..544b10c0ae9f 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-bal-project-with-build-tool-not-found.txt +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-bal-project-with-build-tool-not-found.txt @@ -1,6 +1,3 @@ Executing Build Tools ERROR [Ballerina.toml:(6:1,9:26)] Build tool 'grpc' cannot be resolved - -Compiling source - foo/winery:0.1.0 diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-with-diagnostics.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-with-diagnostics.txt index 266b56e1e066..5a8ccf25fdf2 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-with-diagnostics.txt +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-with-diagnostics.txt @@ -2,6 +2,3 @@ Executing Build Tools openapi(generate-delivery-client) ERROR [generate-delivery-client] The provided filePath does not exist - -Compiling source - foo/winery:0.1.0 diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-with-invalid-missing-toml-properties.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-with-invalid-missing-toml-properties.txt index 6c0504eafa0c..9a48727ec7ed 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-with-invalid-missing-toml-properties.txt +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-with-invalid-missing-toml-properties.txt @@ -4,6 +4,3 @@ ERROR [Ballerina.toml:(12:1,14:24)] missing key '[filePath]' in table '[tool.ope Executing Build Tools WARNING: Execution of 'openapi:generate-delivery-client' is skipped due to errors WARNING: Execution of 'openapi:generate-client' is skipped due to errors - -Compiling source - foo/winery:0.1.0 diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-with-recurring-tool-properties.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-with-recurring-tool-properties.txt index 014a3b00d56a..84e56a6bfc9d 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-with-recurring-tool-properties.txt +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/unix/build-tool-with-recurring-tool-properties.txt @@ -4,6 +4,3 @@ ERROR [Ballerina.toml:(12:1,15:24)] recurring tool id 'generate-delivery-client' Executing Build Tools openapi(generate-delivery-client) WARNING: Execution of 'openapi:generate-delivery-client' is skipped due to errors - -Compiling source - foo/winery:0.1.0 diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-bal-project-with-build-tool-not-found.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-bal-project-with-build-tool-not-found.txt index a7b66a43f10d..544b10c0ae9f 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-bal-project-with-build-tool-not-found.txt +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-bal-project-with-build-tool-not-found.txt @@ -1,6 +1,3 @@ Executing Build Tools ERROR [Ballerina.toml:(6:1,9:26)] Build tool 'grpc' cannot be resolved - -Compiling source - foo/winery:0.1.0 diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-with-diagnostics.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-with-diagnostics.txt index 266b56e1e066..5a8ccf25fdf2 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-with-diagnostics.txt +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-with-diagnostics.txt @@ -2,6 +2,3 @@ Executing Build Tools openapi(generate-delivery-client) ERROR [generate-delivery-client] The provided filePath does not exist - -Compiling source - foo/winery:0.1.0 diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-with-invalid-missing-toml-properties.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-with-invalid-missing-toml-properties.txt index 6c0504eafa0c..9a48727ec7ed 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-with-invalid-missing-toml-properties.txt +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-with-invalid-missing-toml-properties.txt @@ -4,6 +4,3 @@ ERROR [Ballerina.toml:(12:1,14:24)] missing key '[filePath]' in table '[tool.ope Executing Build Tools WARNING: Execution of 'openapi:generate-delivery-client' is skipped due to errors WARNING: Execution of 'openapi:generate-client' is skipped due to errors - -Compiling source - foo/winery:0.1.0 diff --git a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-with-recurring-tool-properties.txt b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-with-recurring-tool-properties.txt index 014a3b00d56a..84e56a6bfc9d 100644 --- a/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-with-recurring-tool-properties.txt +++ b/cli/ballerina-cli/src/test/resources/test-resources/command-outputs/windows/build-tool-with-recurring-tool-properties.txt @@ -4,6 +4,3 @@ ERROR [Ballerina.toml:(12:1,15:24)] recurring tool id 'generate-delivery-client' Executing Build Tools openapi(generate-delivery-client) WARNING: Execution of 'openapi:generate-delivery-client' is skipped due to errors - -Compiling source - foo/winery:0.1.0