From b9b7f2c600f8b5c516893251c1133afe76dbb945 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Thu, 8 Feb 2024 23:04:41 +0530 Subject: [PATCH 1/6] Fix emit diagnostics to print lifecycle plugin diagnostics --- .../cli/task/CreateExecutableTask.java | 13 +++--------- .../ballerina/projects/JBallerinaBackend.java | 21 +++++++++++-------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateExecutableTask.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateExecutableTask.java index d4c9835a940e..107c88e4c7b3 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateExecutableTask.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateExecutableTask.java @@ -127,16 +127,9 @@ public void execute(Project project) { } } - List diagnostics = new ArrayList<>(emitResult.diagnostics().diagnostics()); - if (!diagnostics.isEmpty()) { - // TODO: When deprecating the lifecycle compiler plugin, we can remove this check for duplicates - // in JBallerinaBackend diagnostics and the diagnostics added to EmitResult. - diagnostics = diagnostics.stream() - .filter(diagnostic -> !jBallerinaBackend.diagnosticResult().diagnostics().contains(diagnostic)) - .collect(Collectors.toList()); - if (!diagnostics.isEmpty()) { - diagnostics.forEach(d -> out.println("\n" + d.toString())); - } + List emitDiagnostics = new ArrayList<>(emitResult.diagnostics().diagnostics()); + if (!emitDiagnostics.isEmpty()) { + emitDiagnostics.forEach(d -> out.println("\n" + d.toString())); } } catch (ProjectException e) { diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java index 2bba26323976..97ff8b0ba202 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java @@ -205,12 +205,11 @@ public DiagnosticResult diagnosticResult() { return diagnosticResult; } - // TODO EmitResult should not contain compilation diagnostics. public EmitResult emit(OutputType outputType, Path filePath) { Path generatedArtifact = null; if (diagnosticResult.hasErrors()) { - return new EmitResult(false, diagnosticResult, generatedArtifact); + return new EmitResult(false, new DefaultDiagnosticResult(new ArrayList<>()), generatedArtifact); } switch (outputType) { @@ -227,19 +226,23 @@ public EmitResult emit(OutputType outputType, Path filePath) { throw new RuntimeException("Unexpected output type: " + outputType); } - ArrayList diagnostics = new ArrayList<>(diagnosticResult.allDiagnostics); + ArrayList allDiagnostics = new ArrayList<>(diagnosticResult.allDiagnostics); + List emitResultDiagnostics = new ArrayList<>(); + // Add lifecycle plugin diagnostics. List pluginDiagnostics = packageCompilation.notifyCompilationCompletion(filePath); if (!pluginDiagnostics.isEmpty()) { - diagnostics.addAll(pluginDiagnostics); + emitResultDiagnostics.addAll(pluginDiagnostics); } - diagnosticResult = new DefaultDiagnosticResult(diagnostics); - - List allDiagnostics = new ArrayList<>(diagnostics); + // Add jar resolver diagnostics. jarResolver().diagnosticResult().diagnostics().stream().forEach( - diagnostic -> allDiagnostics.add(diagnostic)); + diagnostic -> emitResultDiagnostics.add(diagnostic)); + allDiagnostics.addAll(emitResultDiagnostics); + // JBallerinaBackend diagnostics contains all diagnostics. + // EmitResult will only contain diagnostics related to emitting the executable. + diagnosticResult = new DefaultDiagnosticResult(allDiagnostics); // TODO handle the EmitResult properly - return new EmitResult(true, new DefaultDiagnosticResult(allDiagnostics), generatedArtifact); + return new EmitResult(true, new DefaultDiagnosticResult(emitResultDiagnostics), generatedArtifact); } private Path emitBala(Path filePath) { From b893dc9fbe98965d1fe187f7d51a84db642bf708 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Thu, 8 Feb 2024 23:05:49 +0530 Subject: [PATCH 2/6] Revert "Fix emit diagnostics to print lifecycle plugin diagnostics" This reverts commit b9b7f2c600f8b5c516893251c1133afe76dbb945. --- .../cli/task/CreateExecutableTask.java | 13 +++++++++--- .../ballerina/projects/JBallerinaBackend.java | 21 ++++++++----------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateExecutableTask.java b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateExecutableTask.java index 107c88e4c7b3..d4c9835a940e 100644 --- a/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateExecutableTask.java +++ b/cli/ballerina-cli/src/main/java/io/ballerina/cli/task/CreateExecutableTask.java @@ -127,9 +127,16 @@ public void execute(Project project) { } } - List emitDiagnostics = new ArrayList<>(emitResult.diagnostics().diagnostics()); - if (!emitDiagnostics.isEmpty()) { - emitDiagnostics.forEach(d -> out.println("\n" + d.toString())); + List diagnostics = new ArrayList<>(emitResult.diagnostics().diagnostics()); + if (!diagnostics.isEmpty()) { + // TODO: When deprecating the lifecycle compiler plugin, we can remove this check for duplicates + // in JBallerinaBackend diagnostics and the diagnostics added to EmitResult. + diagnostics = diagnostics.stream() + .filter(diagnostic -> !jBallerinaBackend.diagnosticResult().diagnostics().contains(diagnostic)) + .collect(Collectors.toList()); + if (!diagnostics.isEmpty()) { + diagnostics.forEach(d -> out.println("\n" + d.toString())); + } } } catch (ProjectException e) { diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java index 97ff8b0ba202..2bba26323976 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBackend.java @@ -205,11 +205,12 @@ public DiagnosticResult diagnosticResult() { return diagnosticResult; } + // TODO EmitResult should not contain compilation diagnostics. public EmitResult emit(OutputType outputType, Path filePath) { Path generatedArtifact = null; if (diagnosticResult.hasErrors()) { - return new EmitResult(false, new DefaultDiagnosticResult(new ArrayList<>()), generatedArtifact); + return new EmitResult(false, diagnosticResult, generatedArtifact); } switch (outputType) { @@ -226,23 +227,19 @@ public EmitResult emit(OutputType outputType, Path filePath) { throw new RuntimeException("Unexpected output type: " + outputType); } - ArrayList allDiagnostics = new ArrayList<>(diagnosticResult.allDiagnostics); - List emitResultDiagnostics = new ArrayList<>(); - // Add lifecycle plugin diagnostics. + ArrayList diagnostics = new ArrayList<>(diagnosticResult.allDiagnostics); List pluginDiagnostics = packageCompilation.notifyCompilationCompletion(filePath); if (!pluginDiagnostics.isEmpty()) { - emitResultDiagnostics.addAll(pluginDiagnostics); + diagnostics.addAll(pluginDiagnostics); } - // Add jar resolver diagnostics. + diagnosticResult = new DefaultDiagnosticResult(diagnostics); + + List allDiagnostics = new ArrayList<>(diagnostics); jarResolver().diagnosticResult().diagnostics().stream().forEach( - diagnostic -> emitResultDiagnostics.add(diagnostic)); - allDiagnostics.addAll(emitResultDiagnostics); - // JBallerinaBackend diagnostics contains all diagnostics. - // EmitResult will only contain diagnostics related to emitting the executable. - diagnosticResult = new DefaultDiagnosticResult(allDiagnostics); + diagnostic -> allDiagnostics.add(diagnostic)); // TODO handle the EmitResult properly - return new EmitResult(true, new DefaultDiagnosticResult(emitResultDiagnostics), generatedArtifact); + return new EmitResult(true, new DefaultDiagnosticResult(allDiagnostics), generatedArtifact); } private Path emitBala(Path filePath) { From 91326970dee1c562d2d8b107f3951fc739840f99 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Fri, 9 Feb 2024 13:52:53 +0530 Subject: [PATCH 3/6] Improve code generated for bal add command --- .../src/main/resources/create_cmd_templates/lib/lib.bal | 6 +++--- .../resources/create_cmd_templates/lib/tests/lib_test.bal | 3 +-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cli/ballerina-cli/src/main/resources/create_cmd_templates/lib/lib.bal b/cli/ballerina-cli/src/main/resources/create_cmd_templates/lib/lib.bal index 7a0681e5344c..a5b1af96448c 100644 --- a/cli/ballerina-cli/src/main/resources/create_cmd_templates/lib/lib.bal +++ b/cli/ballerina-cli/src/main/resources/create_cmd_templates/lib/lib.bal @@ -1,9 +1,9 @@ # Returns the string `Hello` with the input string name. # -# + name - name as a string +# + name - name as a string or nil # + return - "Hello, " with the input string name -public function hello(string name) returns string { - if !(name is "") { +public function hello(string? name) returns string { + if name !is () { return "Hello, " + name; } return "Hello, World!"; diff --git a/cli/ballerina-cli/src/main/resources/create_cmd_templates/lib/tests/lib_test.bal b/cli/ballerina-cli/src/main/resources/create_cmd_templates/lib/tests/lib_test.bal index 31ce10dfbb64..4b725d91de93 100644 --- a/cli/ballerina-cli/src/main/resources/create_cmd_templates/lib/tests/lib_test.bal +++ b/cli/ballerina-cli/src/main/resources/create_cmd_templates/lib/tests/lib_test.bal @@ -21,8 +21,7 @@ function testFunction() { @test:Config {} function negativeTestFunction() { - string name = ""; - string welcomeMsg = hello(name); + string welcomeMsg = hello(()); test:assertEquals("Hello, World!", welcomeMsg); } From 1e7b5bcf681640d83034f58a23fc98315e800a5d Mon Sep 17 00:00:00 2001 From: ShammiL Date: Mon, 12 Feb 2024 10:41:51 +0530 Subject: [PATCH 4/6] Apply improvement to service template --- .../resources/create_cmd_templates/service/service.bal | 8 ++++---- .../create_cmd_templates/service/tests/service_test.bal | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cli/ballerina-cli/src/main/resources/create_cmd_templates/service/service.bal b/cli/ballerina-cli/src/main/resources/create_cmd_templates/service/service.bal index 90f6a0cc7979..293489862114 100644 --- a/cli/ballerina-cli/src/main/resources/create_cmd_templates/service/service.bal +++ b/cli/ballerina-cli/src/main/resources/create_cmd_templates/service/service.bal @@ -7,11 +7,11 @@ service / on new http:Listener(9090) { # A resource for generating greetings # + name - the input string name # + return - string name with hello message or error - resource function get greeting(string name) returns string|error { + resource function get greeting(string? name) returns string|error { // Send a response back to the caller. - if name is "" { + if name is () { return error("name should not be empty!"); } - return "Hello, " + name; - } + return string `Hello, ${name}`; + } } diff --git a/cli/ballerina-cli/src/main/resources/create_cmd_templates/service/tests/service_test.bal b/cli/ballerina-cli/src/main/resources/create_cmd_templates/service/tests/service_test.bal index 1e2c013c0280..0d85eb25166e 100644 --- a/cli/ballerina-cli/src/main/resources/create_cmd_templates/service/tests/service_test.bal +++ b/cli/ballerina-cli/src/main/resources/create_cmd_templates/service/tests/service_test.bal @@ -23,7 +23,7 @@ function testServiceWithProperName() { @test:Config {} function testServiceWithEmptyName() returns error? { - http:Response response = check testClient->get("/greeting/?name="); + http:Response response = check testClient->get("/greeting/"); test:assertEquals(response.statusCode, 500); json errorPayload = check response.getJsonPayload(); test:assertEquals(errorPayload.message, "name should not be empty!"); From 1dfeddb98cecd2323f5e97e55bf55da24d672175 Mon Sep 17 00:00:00 2001 From: ShammiL Date: Mon, 12 Feb 2024 10:42:12 +0530 Subject: [PATCH 5/6] Use string templates in templates --- .../src/main/resources/create_cmd_templates/lib/lib.bal | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/ballerina-cli/src/main/resources/create_cmd_templates/lib/lib.bal b/cli/ballerina-cli/src/main/resources/create_cmd_templates/lib/lib.bal index a5b1af96448c..ae6ddb6448cb 100644 --- a/cli/ballerina-cli/src/main/resources/create_cmd_templates/lib/lib.bal +++ b/cli/ballerina-cli/src/main/resources/create_cmd_templates/lib/lib.bal @@ -4,7 +4,7 @@ # + return - "Hello, " with the input string name public function hello(string? name) returns string { if name !is () { - return "Hello, " + name; + return string `Hello, ${name}`; } return "Hello, World!"; } From b9d24606cf56ac361328f0cfb9e36a0e65925ccb Mon Sep 17 00:00:00 2001 From: ShammiL Date: Thu, 15 Feb 2024 11:12:50 +0530 Subject: [PATCH 6/6] Address comments --- .../main/resources/create_cmd_templates/service/service.bal | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/ballerina-cli/src/main/resources/create_cmd_templates/service/service.bal b/cli/ballerina-cli/src/main/resources/create_cmd_templates/service/service.bal index 293489862114..597a58d2d1fa 100644 --- a/cli/ballerina-cli/src/main/resources/create_cmd_templates/service/service.bal +++ b/cli/ballerina-cli/src/main/resources/create_cmd_templates/service/service.bal @@ -5,7 +5,7 @@ import ballerina/http; service / on new http:Listener(9090) { # A resource for generating greetings - # + name - the input string name + # + name - name as a string or nil # + return - string name with hello message or error resource function get greeting(string? name) returns string|error { // Send a response back to the caller. @@ -13,5 +13,5 @@ service / on new http:Listener(9090) { return error("name should not be empty!"); } return string `Hello, ${name}`; - } + } }