INRIA / INRIA/spoon

[Bug] Ambiguous package name detected

Open
#4,633 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2k
Forks
392
Avg merge
11h 24m
Merged PRs (30d)
36

Description

**Describe the bug**
An exception is raised when analysing two files within the same package but in two different folders in the file system, like for example source code files and test files.
This only seems to happen when the project under analysis uses modules.

It seems that this error was "fixed" by another issue (#4051) but it seems that's not the case, as I still get the error even though I'm using Spoon 10.0.0.

**To Reproduce**
I encountered the error while analysing JetUML, so I'm going to us that to describe how to reproduce the error.

First clone the repo:
```bash shell
git clone --depth 1 https://github.com/prmr/JetUML.git /tmp/JetUML
```
Then run the following test
```java
@Test
void testAmbiguousName() {
Launcher launcher = new Launcher();
Environment environment = launcher.getEnvironment();
environment.setEncoding(StandardCharsets.UTF_8);
environment.setComplianceLevel(16);
environment.setIgnoreDuplicateDeclarations(true);
environment.setCommentEnabled(false);
environment.disableConsistencyChecks();
environment.setNoClasspath(true);
environment.setShouldCompile(false);

launcher.addInputResource("/tmp/JetUML/src/module-info.java"); // Commenting this line of code out makes the test pass.
launcher.addInputResource("/tmp/JetUML/src/ca/mcgill/cs/jetuml/viewers/nodes/AbstractNodeViewer.java");
launcher.addInputResource("/tmp/JetUML/test/ca/mcgill/cs/jetuml/viewers/nodes/TestUseCaseNodeViewer.java");
Assertions.assertDoesNotThrow(() -> launcher.buildModel());
}
```

Output:
```java
spoon.SpoonException: Ambiguous package name detected. If you believe the code you analyzed is correct, please file an issue and reference https://github.com/INRIA/spoon/issues/4051. Error details: Found 2 non-empty packages with name 'ca.mcgill.cs.jetuml.viewers.nodes'
at spoon.reflect.factory.PackageFactory.get(PackageFactory.java:192)
at spoon.support.reflect.reference.CtPackageReferenceImpl.getDeclaration(CtPackageReferenceImpl.java:24)
at spoon.support.reflect.declaration.CtCompilationUnitImpl.getDeclaredPackage(CtCompilationUnitImpl.java:230)
at spoon.support.compiler.jdt.ReferenceBuilder.setPackageOrDeclaringType(ReferenceBuilder.java:1188)
at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:705)
at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:638)
at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:517)
at spoon.support.compiler.jdt.ReferenceBuilder.getExecutableReference(ReferenceBuilder.java:455)
at spoon.support.compiler.jdt.JDTTreeBuilder.visit(JDTTreeBuilder.java:879)
at org.eclipse.jdt.internal.compiler.ast.AllocationExpression.traverse(AllocationExpression.java:716)
at org.eclipse.jdt.internal.compiler.ast.FieldDeclaration.traverse(FieldDeclaration.java:381)
at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.traverse(TypeDeclaration.java:1692)
at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.traverse(CompilationUnitDeclaration.java:827)
at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.traverse(CompilationUnitDeclaration.java:788)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.traverseUnitDeclaration(JDTBasedSpoonCompiler.java:480)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.lambda$buildModel$0(JDTBasedSpoonCompiler.java:437)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.forEachCompilationUnit(JDTBasedSpoonCompiler.java:464)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildModel(JDTBasedSpoonCompiler.java:435)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnitsAndModel(JDTBasedSpoonCompiler.java:372)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildSources(JDTBasedSpoonCompiler.java:335)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:116)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:99)
at spoon.Launcher.buildModel(Launcher.java:782)spoon.SpoonException: Ambiguous package name detected. If you believe the code you analyzed is correct, please file an issue and reference https://github.com/INRIA/spoon/issues/4051. Error details: Found 2 non-empty packages with name 'ca.mcgill.cs.jetuml.viewers.nodes'
at spoon.reflect.factory.PackageFactory.get(PackageFactory.java:192)
at spoon.support.reflect.reference.CtPackageReferenceImpl.getDeclaration(CtPackageReferenceImpl.java:24)
at spoon.support.reflect.declaration.CtCompilationUnitImpl.getDeclaredPackage(CtCompilationUnitImpl.java:230)
at spoon.support.compiler.jdt.ReferenceBuilder.setPackageOrDeclaringType(ReferenceBuilder.java:1188)
at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:705)
at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:638)
at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:517)
at spoon.support.compiler.jdt.ReferenceBuilder.getExecutableReference(ReferenceBuilder.java:455)
at spoon.support.compiler.jdt.JDTTreeBuilder.visit(JDTTreeBuilder.java:879)
at org.eclipse.jdt.internal.compiler.ast.AllocationExpression.traverse(AllocationExpression.java:716)
at org.eclipse.jdt.internal.compiler.ast.FieldDeclaration.traverse(FieldDeclaration.java:381)
at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.traverse(TypeDeclaration.java:1692)
at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.traverse(CompilationUnitDeclaration.java:827)
at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.traverse(CompilationUnitDeclaration.java:788)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.traverseUnitDeclaration(JDTBasedSpoonCompiler.java:480)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.lambda$buildModel$0(JDTBasedSpoonCompiler.java:437)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.forEachCompilationUnit(JDTBasedSpoonCompiler.java:464)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildModel(JDTBasedSpoonCompiler.java:435)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnitsAndModel(JDTBasedSpoonCompiler.java:372)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildSources(JDTBasedSpoonCompiler.java:335)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:116)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:99)
at spoon.Launcher.buildModel(Launcher.java:782)
```

**Operating system, JDK and Spoon version used**

* OS: Linux 5.13.0-30-generic
* JDK: 17.0.2
* Spoon version: 10.0.0 and master branch as well (173c3c52ddb19a)

Any ideas on how to deal with the issue?

Thank you.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.