Skip to content

Commit

Permalink
Merge pull request #43426 from Shadow-Devil/fix-41418
Browse files Browse the repository at this point in the history
Fix bal pack crashes when [[dependency]] section is missing in the CompilerPlugin.toml file
  • Loading branch information
gimantha authored Oct 8, 2024
2 parents 4105ab0 + b2251b8 commit c2c47b3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,32 +137,28 @@ protected void addCompilerPlugin(ZipOutputStream balaOutputStream) throws IOExce
List<String> compilerPluginLibPaths = new ArrayList<>();
List<String> 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 = 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());
}
// 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 = 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());
}

CompilerPluginJson compilerPluginJson = new CompilerPluginJson(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "samjs"
name = "package_compiler_plugin_42"
version = "1.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[plugin]
class = "io.samjs.plugins.badsad.BadSadCompilerPlugin42"

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public function doSomething() {
// Do nothing
}

0 comments on commit c2c47b3

Please sign in to comment.