From 3e5d3144c142c15ffaa421d516b3ed624f0f7c50 Mon Sep 17 00:00:00 2001 From: Felix Schnabel Date: Thu, 26 Sep 2024 14:38:31 +0200 Subject: [PATCH 1/3] Fix bal pack crashes when [[dependency]] section is missing in the CompilerPlugin.toml file --- .../projects/JBallerinaBalaWriter.java | 42 +++++++++---------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java index 25aeaefb1914..a0b4fa6a8971 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java @@ -138,32 +138,28 @@ protected void addCompilerPlugin(ZipOutputStream balaOutputStream) throws IOExce List compilerPluginLibPaths = new ArrayList<>(); List compilerPluginDependencies = this.compilerPluginToml.get().getCompilerPluginDependencies(); - if (compilerPluginDependencies.get(0) == null) { + if (compilerPluginDependencies.isEmpty()) { throw new ProjectException("No dependencies found in CompilerPlugin.toml file"); } - if (!compilerPluginDependencies.isEmpty()) { - - // Iterate through compiler plugin dependencies and add them to bala - // organization would be - // -- Bala Root - // - compiler-plugin/ - // - libs - // - java-library1.jar - // - java-library2.jar - - - // Iterate jars and create directories for each target - for (String compilerPluginLib : compilerPluginDependencies) { - Path libPath = this.packageContext.project().sourceRoot().resolve(compilerPluginLib); - // null check is added for spot bug with the toml validation filename cannot be null - String fileName = Optional.ofNullable(libPath.getFileName()) - .map(Path::toString).orElse(ANNON); - Path entryPath = Paths.get("compiler-plugin").resolve("libs").resolve(fileName); - // create a zip entry for each file - putZipEntry(balaOutputStream, entryPath, new FileInputStream(libPath.toString())); - compilerPluginLibPaths.add(entryPath.toString()); - } + // Iterate through compiler plugin dependencies and add them to bala + // organization would be + // -- Bala Root + // - compiler-plugin/ + // - libs + // - java-library1.jar + // - java-library2.jar + + // Iterate jars and create directories for each target + for (String compilerPluginLib : compilerPluginDependencies) { + Path libPath = this.packageContext.project().sourceRoot().resolve(compilerPluginLib); + // null check is added for spot bug with the toml validation filename cannot be null + String fileName = Optional.ofNullable(libPath.getFileName()) + .map(Path::toString).orElse(ANNON); + Path entryPath = Paths.get("compiler-plugin").resolve("libs").resolve(fileName); + // create a zip entry for each file + putZipEntry(balaOutputStream, entryPath, new FileInputStream(libPath.toString())); + compilerPluginLibPaths.add(entryPath.toString()); } CompilerPluginJson compilerPluginJson = new CompilerPluginJson( From 39c8bba9bc2cdbf501ddc91f079671db271c295e Mon Sep 17 00:00:00 2001 From: gimantha Date: Fri, 4 Oct 2024 17:21:24 +0530 Subject: [PATCH 2/3] Use Path.of instead of Paths.get --- .../main/java/io/ballerina/projects/JBallerinaBalaWriter.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java index f6c557392fde..fac6f51d05c2 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JBallerinaBalaWriter.java @@ -155,7 +155,7 @@ protected void addCompilerPlugin(ZipOutputStream balaOutputStream) throws IOExce // null check is added for spot bug with the toml validation filename cannot be null String fileName = Optional.ofNullable(libPath.getFileName()) .map(Path::toString).orElse(ANNON); - Path entryPath = Paths.get("compiler-plugin").resolve("libs").resolve(fileName); + Path entryPath = Path.of("compiler-plugin").resolve("libs").resolve(fileName); // create a zip entry for each file putZipEntry(balaOutputStream, entryPath, new FileInputStream(libPath.toString())); compilerPluginLibPaths.add(entryPath.toString()); From 1d422667c73540f20b36213618866d6802922375 Mon Sep 17 00:00:00 2001 From: Felix Schnabel Date: Fri, 4 Oct 2024 18:58:21 +0200 Subject: [PATCH 3/3] Add negative testcase for compiler plugin without any dependencies --- .../projects/test/plugins/CompilerPluginNegativeTests.java | 6 ++++++ .../package_compiler_plugin_42/Ballerina.toml | 4 ++++ .../package_compiler_plugin_42/CompilerPlugin.toml | 3 +++ .../negative_cases/package_compiler_plugin_42/main.bal | 3 +++ 4 files changed, 16 insertions(+) create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/negative_cases/package_compiler_plugin_42/Ballerina.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/negative_cases/package_compiler_plugin_42/CompilerPlugin.toml create mode 100644 project-api/project-api-test/src/test/resources/compiler_plugin_tests/negative_cases/package_compiler_plugin_42/main.bal diff --git a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginNegativeTests.java b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginNegativeTests.java index 2c84f6fed0a5..ed6abf9b6cf2 100644 --- a/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginNegativeTests.java +++ b/project-api/project-api-test/src/test/java/io/ballerina/projects/test/plugins/CompilerPluginNegativeTests.java @@ -56,6 +56,7 @@ public class CompilerPluginNegativeTests { "'samjs:package_compiler_plugin_40:1.1.0' failed to complete. The value cannot be less than zero"; private static final String EXCEPTION_MESSAGE_41 = "The compiler extension in package " + "'samjs:package_compiler_plugin_41:1.1.0' failed to complete. The value cannot be less than zero"; + private static final String EXCEPTION_MESSAGE_42 = "No dependencies found in CompilerPlugin.toml file"; private static final Path RESOURCE_DIRECTORY = Path.of( "src/test/resources/compiler_plugin_tests/negative_cases").toAbsolutePath(); @@ -105,6 +106,11 @@ public void testLoadingCompilerPluginCase41() { loadCompilationPackage(41); } + @Test(expectedExceptions = ProjectException.class, expectedExceptionsMessageRegExp = EXCEPTION_MESSAGE_42) + public void testLoadingCompilerPluginCase42() { + loadCompilationPackage(42); + } + private void loadCompilationPackage(int testCase) { BCompileUtil.compileAndCacheBala( "compiler_plugin_tests/negative_cases/package_compiler_plugin_" + testCase); diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/negative_cases/package_compiler_plugin_42/Ballerina.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/negative_cases/package_compiler_plugin_42/Ballerina.toml new file mode 100644 index 000000000000..d9bf8ca9b157 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/negative_cases/package_compiler_plugin_42/Ballerina.toml @@ -0,0 +1,4 @@ +[package] +org = "samjs" +name = "package_compiler_plugin_42" +version = "1.1.0" diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/negative_cases/package_compiler_plugin_42/CompilerPlugin.toml b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/negative_cases/package_compiler_plugin_42/CompilerPlugin.toml new file mode 100644 index 000000000000..c5219280cbd7 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/negative_cases/package_compiler_plugin_42/CompilerPlugin.toml @@ -0,0 +1,3 @@ +[plugin] +class = "io.samjs.plugins.badsad.BadSadCompilerPlugin42" + diff --git a/project-api/project-api-test/src/test/resources/compiler_plugin_tests/negative_cases/package_compiler_plugin_42/main.bal b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/negative_cases/package_compiler_plugin_42/main.bal new file mode 100644 index 000000000000..41a101b1c439 --- /dev/null +++ b/project-api/project-api-test/src/test/resources/compiler_plugin_tests/negative_cases/package_compiler_plugin_42/main.bal @@ -0,0 +1,3 @@ +public function doSomething() { + // Do nothing +}