Skip to content

Commit 8a8e6b4

Browse files
authored
Merge pull request #130 from krrish175-byte/fix/bloop-config-suffix
Fix bloop config suffix collision and update dependency resolution
2 parents cdcc31e + 72a95e7 commit 8a8e6b4

7 files changed

Lines changed: 176 additions & 4 deletions

File tree

src/main/scala/bloop/integrations/maven/MojoImplementation.scala

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ object MojoImplementation {
129129
}
130130
}
131131

132+
val reactorArtifactIds = session.getProjects().asScala.map(_.getArtifactId).toSet
133+
134+
def getBloopName(artifactId: String, configuration: String): String = {
135+
configuration match {
136+
case "compile" => artifactId
137+
case _ =>
138+
val defaultName = s"$artifactId-$configuration"
139+
if (reactorArtifactIds.contains(defaultName)) s"$artifactId-$configuration-scope"
140+
else defaultName
141+
}
142+
}
143+
132144
val reactorProjectsSet = mojo
133145
.getReactorProjects()
134146
.asScala
@@ -153,7 +165,7 @@ object MojoImplementation {
153165
log.info(s"Dependency $dep, $matchingArtifacts")
154166
matchingArtifacts
155167
.collect {
156-
case artifact if artifact.getType == "test-jar" => dep.getArtifactId + "-test"
168+
case artifact if artifact.getType == "test-jar" => getBloopName(dep.getArtifactId, "test")
157169
}
158170
.toList
159171
.appended(dep.getArtifactId)
@@ -201,8 +213,7 @@ object MojoImplementation {
201213
launcher: Option[AppLauncher],
202214
configuration: String
203215
): Unit = {
204-
val suffix = if (configuration == "compile") "" else s"-$configuration"
205-
val name = project.getArtifactId() + suffix
216+
val name = getBloopName(project.getArtifactId(), configuration)
206217
val build = project.getBuild()
207218
val baseDirectory = abs(project.getBasedir())
208219
val out = baseDirectory.resolve("target")
@@ -335,6 +346,12 @@ object MojoImplementation {
335346
log.info(s"Generated $finalTarget")
336347
log.debug(s"Configuration to be serialized:\n$config")
337348
bloop.config.write(config, configTarget.toPath)
349+
350+
log.info(s"Starting to write configuration for project: ${project.getArtifactId()} with configuration: $configuration")
351+
log.debug(s"Source directories: ${sourceDirs0.map(_.getAbsolutePath).mkString(", ")}")
352+
log.debug(s"Classpath: ${classpath0().asScala.mkString(", ")}")
353+
log.debug(s"Resources: ${resources0.asScala.mkString(", ")}")
354+
log.debug(s"Output directory: ${classesDir0.getAbsolutePath}")
338355
}
339356

340357
writeConfig(
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.example</groupId>
4+
<artifactId>module1-test</artifactId>
5+
<version>0.1</version>
6+
<name>module1-test</name>
7+
8+
<parent>
9+
<groupId>com.example</groupId>
10+
<artifactId>conflicting_modules</artifactId>
11+
<version>0.1</version>
12+
</parent>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.scala-lang</groupId>
17+
<artifactId>scala-library</artifactId>
18+
<version>2.13.6</version>
19+
</dependency>
20+
</dependencies>
21+
22+
<build>
23+
<sourceDirectory>src/main/scala</sourceDirectory>
24+
<testSourceDirectory>src/test/scala</testSourceDirectory>
25+
<plugins>
26+
<plugin>
27+
<groupId>net.alchim31.maven</groupId>
28+
<artifactId>scala-maven-plugin</artifactId>
29+
<version>3.3.2</version>
30+
</plugin>
31+
</plugins>
32+
</build>
33+
</project>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.example</groupId>
4+
<artifactId>module1</artifactId>
5+
<version>0.1</version>
6+
<name>module1</name>
7+
8+
<parent>
9+
<groupId>com.example</groupId>
10+
<artifactId>conflicting_modules</artifactId>
11+
<version>0.1</version>
12+
</parent>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>org.scala-lang</groupId>
17+
<artifactId>scala-library</artifactId>
18+
<version>2.13.6</version>
19+
</dependency>
20+
</dependencies>
21+
22+
<build>
23+
<sourceDirectory>src/main/scala</sourceDirectory>
24+
<testSourceDirectory>src/test/scala</testSourceDirectory>
25+
<plugins>
26+
<plugin>
27+
<groupId>net.alchim31.maven</groupId>
28+
<artifactId>scala-maven-plugin</artifactId>
29+
<version>3.3.2</version>
30+
</plugin>
31+
</plugins>
32+
</build>
33+
</project>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.example</groupId>
4+
<artifactId>conflicting_modules</artifactId>
5+
<version>0.1</version>
6+
<packaging>pom</packaging>
7+
<name>conflicting_modules</name>
8+
<description>Project with conflicting module names.</description>
9+
10+
<properties>
11+
<maven.compiler.source>1.8</maven.compiler.source>
12+
<maven.compiler.target>1.8</maven.compiler.target>
13+
<encoding>UTF-8</encoding>
14+
</properties>
15+
16+
<modules>
17+
<module>module1</module>
18+
<module>module1-test</module>
19+
</modules>
20+
21+
</project>

src/test/resources/multi_module_test_jar/bar/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<dependency>
1818
<groupId>org.scala-lang</groupId>
1919
<artifactId>scala-library</artifactId>
20+
<version>${scala.version}</version>
2021
</dependency>
2122
<dependency>
2223
<groupId>com.example</groupId>

src/test/resources/multi_module_test_jar/foo/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<dependency>
1818
<groupId>org.scala-lang</groupId>
1919
<artifactId>scala-library</artifactId>
20+
<version>${scala.version}</version>
2021
</dependency>
2122
</dependencies>
2223

src/test/scala/bloop/integrations/maven/MavenConfigGenerationTest.scala

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,45 @@ class MavenConfigGenerationTest extends BaseConfigSuite {
202202
}
203203
}
204204

205+
@Test
206+
def conflictingSubmodules() = {
207+
check(
208+
"conflicting_modules/pom.xml",
209+
submodules = List(
210+
"conflicting_modules/module1/pom.xml",
211+
"conflicting_modules/module1-test/pom.xml"
212+
)
213+
) {
214+
case (configFile, projectName, List(module1, module2)) =>
215+
// module1 is "module1"
216+
// module2 is "module1-test"
217+
218+
// module1's test configuration should be renamed to avoid conflict with module2
219+
// Default would be "module1-test", but "module1-test" exists as a reactor artifact (module2)
220+
// So it should be "module1-test-scope"
221+
assertEquals("module1", module1.project.name)
222+
223+
// We need to check the test configuration name for module1.
224+
// check() function loads the "compile" configuration (default expectation in this test suite assumption?)
225+
// Actually check() loads the config based on the project file name.
226+
// module1 comes from "conflicting_modules/module1/pom.xml" -> parent dir is "module1".
227+
// The bloop file loaded is "module1.json".
228+
229+
// Let's check the test config of module1
230+
val module1TestConfigPath = configFile.project.directory.resolve(".bloop").resolve("module1-test-scope.json")
231+
assertTrue(s"Test config for module1 should be renamed to module1-test-scope.json", Files.exists(module1TestConfigPath))
232+
233+
val module1TestConfig = readValidBloopConfig(module1TestConfigPath.toFile())
234+
assertEquals("module1-test-scope", module1TestConfig.project.name)
235+
236+
// module2 should start with module1-test
237+
assertEquals("module1-test", module2.project.name)
238+
239+
case _ =>
240+
fail("Conflicting modules test should have 2 submodules")
241+
}
242+
}
243+
205244
@Test
206245
def dependencyTestJars() = {
207246
check("test_jars/pom.xml") { (configFile, projectName, subprojects) =>
@@ -260,6 +299,29 @@ class MavenConfigGenerationTest extends BaseConfigSuite {
260299
}
261300
}
262301

302+
@Test
303+
def testFallbackNamingForTestScope() = {
304+
check(
305+
"multi_dependency/pom.xml",
306+
submodules = List("multi_dependency/module1/pom.xml", "multi_dependency/module2/pom.xml")
307+
) {
308+
case (configFile, projectName, submodulesList) =>
309+
val (module1: Config.File, module2: Config.File) = submodulesList match {
310+
case List(m1, m2) => (m1, m2)
311+
case _ => fail(s"Expected 2 submodules, but got ${submodulesList.size}")
312+
}
313+
314+
// Standard naming should be preserved when no collision exists
315+
assert(!configFile.project.name.contains("-compile"))
316+
assert(!module1.project.name.contains("-compile"))
317+
assert(!module2.project.name.contains("-compile"))
318+
319+
// Note: To test actual collision, we would need to construct a project structure
320+
// where a submodule name conflicts with the test suffix of another module.
321+
// For now, we verify that the default behavior is correct (no suffixes).
322+
}
323+
}
324+
263325
@Test
264326
def junitSupport() = {
265327
check("junit_project/pom.xml") { (configFile, projectName, subprojects) =>
@@ -328,6 +390,7 @@ class MavenConfigGenerationTest extends BaseConfigSuite {
328390
val projectName = projectPath.toFile().getName()
329391
val bloopDir = projectPath.resolve(".bloop")
330392
val projectFile = bloopDir.resolve(s"${projectName}.json")
393+
331394
val configFile = readValidBloopConfig(projectFile.toFile())
332395

333396
val subProjects = submodules.map { mod =>
@@ -362,7 +425,7 @@ class MavenConfigGenerationTest extends BaseConfigSuite {
362425
val processBuilder = new ProcessBuilder()
363426
val out = new StringBuilder()
364427
processBuilder.directory(cwd)
365-
processBuilder.command(cmd: _*);
428+
processBuilder.command(cmd: _*)
366429
var process = processBuilder.start()
367430

368431
val reader =
@@ -373,6 +436,9 @@ class MavenConfigGenerationTest extends BaseConfigSuite {
373436
out.append(line + "\n")
374437
line = reader.readLine()
375438
}
439+
440+
val exitCode = process.waitFor()
441+
376442
out.toString()
377443
}
378444
}

0 commit comments

Comments
 (0)