spring-modulith icon indicating copy to clipboard operation
spring-modulith copied to clipboard

Report duplicate application module identifiers with a descriptive error message

Open nikolamicic21 opened this issue 1 month ago • 1 comments

When two application modules have the same identifier, ApplicationModules.of(…) fails with the generic IllegalStateException that Collectors.toMap(…) throws:

java.lang.IllegalStateException: Duplicate key billing (attempted merging values # Billing
> Logical name: billing
> Base package: com.other.billing
> Excluded packages: none
…
 and # Billing
> Logical name: billing
> Base package: com.another.billing
> Excluded packages: none
…)

The exception comes from the constructor of ApplicationModules:

this.modules = sources.stream()
    .map(…)
    .collect(toMap(ApplicationModule::getIdentifier, Function.identity()));

Failing here is correct, because the identifiers must be unique. But the message has some problems:

  • It is the JDK default message, and the two "values" are the full multi-line ApplicationModule.toString() output. The base packages are in the message, but they are hard to find.
  • It does not tell the user why the identifiers collide or how to fix the problem.
  • The exception occurs before verify(), so users usually see it as a failing ApplicationModules.of(…) call, @ApplicationModuleTest or Documenter run, with a stream-internal stack trace.

How to reproduce

package com.example;

@SpringBootApplication
@Modulithic(additionalPackages = { "com.other", "com.another" })
class Application {}
com.other.billing.BillingService
com.another.billing.BillingService

Each additional package is its own root package, so both billing packages get the identifier billing. Two explicit identifiers that are the same, for example @ApplicationModule(id = "core") on two packages, give the same failure.

Proposal

Find the duplicate identifiers before the map is built, and throw an exception with a short message, for example:

Application module identifier 'billing' is not unique. The modules with the base packages
'com.other.billing' and 'com.another.billing' use it. Declare an explicit identifier with
@ApplicationModule(id = …), or set @Modulithic(useFullyQualifiedModuleNames = true).

nikolamicic21 avatar Oct 15 '26 08:10 nikolamicic21

I would like to work on this and prepare a PR with the descriptive error message and unit tests.

abhiramaab avatar Oct 15 '26 14:10 abhiramaab