JVM: restore incorrect DFS logic in JavaModuleGraph for compatibility

The problem is tracked in KT-66622. It should be fixed later, with a
proper deprecation, because otherwise it can potentially break a lot of
code.
This commit is contained in:
Alexander Udalov
2024-03-18 15:47:29 +01:00
committed by Space Team
parent 94e5cafb61
commit 621fe41b17
19 changed files with 64 additions and 0 deletions
@@ -78,6 +78,9 @@ class JavaModuleGraph(finder: JavaModuleFinder) {
for ((dependencyModuleName, isTransitive) in module.moduleInfo.requires) {
if (dependencyModuleName == dependencyName) return true
if (isTransitive && dfs(dependencyModuleName)) return true
// This is incorrect, but is left for compatibility, see KT-66622.
if (isTransitive && dfs(dependencyName)) return true
}
return false
}
@@ -0,0 +1,3 @@
package foo;
public class Foo {}
@@ -0,0 +1,3 @@
module main {
requires transitive kotlin.stdlib;
}
@@ -0,0 +1 @@
val a = foo.Foo()
@@ -0,0 +1,3 @@
package foo;
public class Foo {}
@@ -0,0 +1,5 @@
module main {
requires kotlin.stdlib;
requires transitive unrelated;
}
@@ -0,0 +1 @@
val a = foo.Foo()
@@ -0,0 +1,3 @@
module unrelated {
exports unrelated;
}
@@ -0,0 +1,3 @@
package unrelated;
public class Unrelated {}
@@ -0,0 +1,3 @@
package foo;
public class Foo {}
@@ -0,0 +1,4 @@
module main {
requires kotlin.stdlib;
requires unrelated;
}
@@ -0,0 +1 @@
val a = foo.Foo()
@@ -0,0 +1,3 @@
package unrelated;
public class Unrelated {}
@@ -387,4 +387,27 @@ abstract class AbstractJavaModulesIntegrationTest(
val lib = module("lib", destination = File(tmpdir, name))
module("main", additionalKotlinArguments = listOf("-classpath", lib.path))
}
fun testNamedDoesNotReadAutomaticWithUnrelatedNamed() {
// This test should result in an error because 'main' does not depend on 'lib' or any other automatic module.
// But currently it's OK for compatibility, see KT-66622.
val lib = module("lib")
val unrelated = module("unrelated")
module("main", listOf(lib, unrelated))
}
fun testNamedDoesNotReadAutomaticWithTransitiveStdlib() {
// This test should result in an error because 'main' does not depend on 'lib' or any other automatic module.
// But currently it's OK for compatibility, see KT-66622.
val lib = module("lib")
module("main", listOf(lib))
}
fun testNamedReadsAutomaticWithUnrelatedAutomatic() {
// Similarly to how it works in javac, if we depend on one automatic module, we depend on all of them. So even though 'main' does
// not have explicit "requires lib", in fact it depends on 'lib' because it has "requires unrelated". So "OK" is expected.
val lib = module("lib")
val unrelated = module("unrelated")
module("main", listOf(lib, unrelated))
}
}