diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JarResolver.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JarResolver.java index 1bd463089935..6d792c4b61fe 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JarResolver.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/JarResolver.java @@ -161,12 +161,20 @@ private void addCodeGeneratedLibraryPaths(PackageContext packageContext, Platfor } ModuleContext moduleContext = packageContext.moduleContext(moduleId); PackageID pkgID = moduleContext.descriptor().moduleCompilationId(); + // DuplicatePkgs are pkgs which are imported by both testable and build projects. + // We need a different category for those to avoid false positive test passes. + boolean isDuplicatePkg = JvmCodeGenUtil.duplicatePkgsMap.containsKey(pkgID.orgName + + pkgID.getNameComps().toString()); // TODO: extract this condition out if (packageContext.project().buildOptions().eliminateDeadCode() && !this.rootPackageContext.project().buildOptions().skipTests() && this.jBalBackend.getOptimizedPackageIDs().contains(pkgID)) { - addOptimizedLibraryPaths(packageContext, scope, libraryPaths, moduleContext, pkgID); + addOptimizedLibraryPaths(packageContext, scope, libraryPaths, moduleContext, isDuplicatePkg); + if (!isDuplicatePkg) { + // If the pkg is not an duplicate pkg, we only need the optimized thin JAR. + continue; + } } PlatformLibrary generatedJarLibrary = jBalBackend.codeGeneratedLibrary( @@ -177,14 +185,12 @@ private void addCodeGeneratedLibraryPaths(PackageContext packageContext, Platfor private void addOptimizedLibraryPaths(PackageContext packageContext, PlatformLibraryScope scope, Set libraryPaths, ModuleContext moduleContext, - PackageID pkgID) { + boolean isDuplicatePkg) { PlatformLibrary generatedOptimizedLibrary = jBalBackend.codeGeneratedOptimizedLibrary( packageContext.packageId(), moduleContext.moduleName()); Path optimizedJarLibraryPath = generatedOptimizedLibrary.path().toAbsolutePath(); - if (JvmCodeGenUtil.duplicatePkgsMap.containsKey(pkgID.orgName + pkgID.getNameComps().toString())) { - // Package is an optimized duplicated pkg. - // This means the package is a common dependency of both testable and build projects. + if (isDuplicatePkg) { optimizedJarLibraryPath = Path.of(optimizedJarLibraryPath.toString().replace(ProjectConstants.BLANG_COMPILED_JAR_EXT, ProjectConstants.BYTECODE_OPTIMIZED_JAR_SUFFIX)); diff --git a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/repos/FileSystemCache.java b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/repos/FileSystemCache.java index dbc3503518b3..3cfc81b8c223 100644 --- a/compiler/ballerina-lang/src/main/java/io/ballerina/projects/repos/FileSystemCache.java +++ b/compiler/ballerina-lang/src/main/java/io/ballerina/projects/repos/FileSystemCache.java @@ -98,10 +98,22 @@ public void cacheBir(ModuleName moduleName, ByteArrayOutputStream birContent) { public Optional getPlatformSpecificLibrary(CompilerBackend compilerBackend, String libraryName, boolean isOptimizedLibrary) { String libraryFileName = libraryName + compilerBackend.libraryFileExtension(); - Path targetPlatformCacheDirPath = isOptimizedLibrary ? getOptimizedTargetPlatformCacheDirPath(compilerBackend) : - getTargetPlatformCacheDirPath(compilerBackend); - Path jarFilePath = targetPlatformCacheDirPath.resolve(libraryFileName); - return Files.exists(jarFilePath) ? Optional.of(jarFilePath) : Optional.empty(); + Path optimizedJarFilePath = getOptimizedTargetPlatformCacheDirPath(compilerBackend).resolve(libraryFileName); + if (isOptimizedLibrary) { + return Optional.of(optimizedJarFilePath); + } + + Path jarFilePath = getTargetPlatformCacheDirPath(compilerBackend).resolve(libraryFileName); + // getPlatformSpecificLibrary gets invoked from TestUtils class and there is no proper way to check whether a + // given module is `optimized`, `unoptimized` or `duplicated optimized`. Maybe we can lift the logic to + // JarResolver somehow. + // TODO Find a cleaner way to handle JarPaths for bal test with dead code elimination. + if (Files.exists(jarFilePath)) { + return Optional.of(jarFilePath); + } else if (Files.exists(optimizedJarFilePath)) { + return Optional.of(optimizedJarFilePath); + } + return Optional.empty(); } @Override