JUnit test not finding class added in library on 2017.3

This is an issue that only presented itself after upgrading from 2017.2 to 2017.3

I depend on a small library that I get from Gradle that I pass to the project at the `setUp()` method of my `LightCodeInsightFixtureTestCase`. This library is acquired using Gradle and passed to the `test` task of Gradle using system properties.

Here's how it's done in the `build.gradle` file, here and here.

And here's how the library is added in the test, here and here.

I have stepped through the code and verified the jar found using the test system property is correct and it does exist. This method also worked reliably up until this point before upgrading to 2017.3.

Nevertheless, any test which uses classes from this library (example) will fail:

https://gist.github.com/DemonWav/65d269c1c02d4e2c95fdfd90edb9a30c

 

10 comments

Hi Kyle,

please check that your module has library attached and that if you call library.getFiles(CLASSES) returns the jar you attached.

Anna

0

The module has the library attched, yes, and the jar is returned when I call that method.

0

It appears the issue is the library is added, and the files are accessible, but they aren't being indexed into the psi.

 

When testing if:

LibraryUtil.findLibrary(myModule, "mixin") === LibraryUtil.findLibraryByClass("org.spongepowered.asm.mixin.Mixin", project)

It returns true. But that same class can't be accessed by

JavaPsiFacade.getInstance(project).findClass("org.spongepowered.asm.mixin.Mixin", GlobalSearchScope.allScope(project))
0

I'd suggest to start with debugging com.intellij.psi.impl.file.impl.JavaFileManagerImpl#doFindClasses.

 

BTW I can try to help to debug but I am not sure what branch should I use as currently there is no such code.

0

That method doesn't seem to do much:

JavaFullClassNameIndex.getInstance().get(qName.hashCode(), myManager.getProject(), scope)

simply returns an empty list.

0

Could you please try reindexing (File | Invalidate Caches and restart, or deleting "system" if in tests)?

0

That did fix that issue, thanks. I was running the clean tasks but didn't think of the sandbox.

Now it's throwing a different psi error that maybe you could point me in the right direction on:

Element: class com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl because: different providers: SingleRootFileViewProvider{myVirtualFile=file:///tmp/unitTest__only_invokers/src/test/AccessorMixin.java, content=VirtualFileContent{size=223}}(72f5b0f6); SingleRootFileViewProvider{myVirtualFile=file:///tmp/unitTest__only_invokers/src/test/AccessorMixin.java, content=VirtualFileContent{size=223}}(75332ecc)
invalidated at: see attachment
com.intellij.psi.PsiInvalidElementAccessException: Element: class com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl because: different providers: SingleRootFileViewProvider{myVirtualFile=file:///tmp/unitTest__only_invokers/src/test/AccessorMixin.java, content=VirtualFileContent{size=223}}(72f5b0f6); SingleRootFileViewProvider{myVirtualFile=file:///tmp/unitTest__only_invokers/src/test/AccessorMixin.java, content=VirtualFileContent{size=223}}(75332ecc)
invalidated at: see attachment
at com.intellij.psi.util.PsiUtilCore.ensureValid(PsiUtilCore.java:502)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.getKind(PsiJavaCodeReferenceElementImpl.java:104)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.getCanonicalText(PsiJavaCodeReferenceElementImpl.java:296)
at com.intellij.psi.impl.source.PsiJavaCodeReferenceElementImpl.getCanonicalText(PsiJavaCodeReferenceElementImpl.java:285)
at com.intellij.psi.impl.source.tree.java.PsiAnnotationImpl.getQualifiedName(PsiAnnotationImpl.java:88)
at com.intellij.psi.impl.PsiImplUtil.findAnnotation(PsiImplUtil.java:309)
at com.intellij.psi.impl.source.PsiModifierListImpl.findAnnotation(PsiModifierListImpl.java:295)
at com.demonwav.mcdev.platform.mixin.util.MixinKt.isAccessorMixin(Mixin.kt:93)
at com.demonwav.mcdev.platform.mixin.AccessorMixinTest$test only invokers$1.invoke(AccessorMixinTest.kt:130)
at com.demonwav.mcdev.platform.mixin.AccessorMixinTest$test only invokers$1.invoke(AccessorMixinTest.kt:18)
at com.demonwav.mcdev.platform.mixin.AccessorMixinTest.doTest(AccessorMixinTest.kt:46)
at com.demonwav.mcdev.platform.mixin.AccessorMixinTest.test only invokers(AccessorMixinTest.kt:118)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at junit.framework.TestCase.runTest(TestCase.java:154)
at com.intellij.testFramework.UsefulTestCase.lambda$runTest$11(UsefulTestCase.java:339)
at com.intellij.testFramework.UsefulTestCase.lambda$invokeTestRunnable$12(UsefulTestCase.java:371)
at com.intellij.testFramework.EdtTestUtilKt.runInEdtAndWait(EdtTestUtil.kt:40)
at com.intellij.testFramework.UsefulTestCase.invokeTestRunnable(UsefulTestCase.java:370)
at com.intellij.testFramework.UsefulTestCase.runTest(UsefulTestCase.java:358)
at com.intellij.testFramework.UsefulTestCase.defaultRunBare(UsefulTestCase.java:384)
at com.intellij.testFramework.EdtTestUtil$Companion$runInEdtAndWait$1.invoke(EdtTestUtil.kt:29)
at com.intellij.testFramework.EdtTestUtil$Companion$runInEdtAndWait$1.invoke(EdtTestUtil.kt:27)
at com.intellij.testFramework.EdtTestUtilKt$runInEdtAndWait$1.run(EdtTestUtil.kt:46)
at com.intellij.openapi.application.TransactionGuardImpl$2.run(TransactionGuardImpl.java:315)
at com.intellij.openapi.application.impl.LaterInvocator$1.run(LaterInvocator.java:158)
at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.runNextEvent(LaterInvocator.java:424)
at com.intellij.openapi.application.impl.LaterInvocator$FlushQueue.run(LaterInvocator.java:407)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:822)
at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:650)
at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:366)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
0

This means your PSI was invalidated (most likely by project root change), but you still use it. If it's in tests, the best approach is to load PSI after setting up the project roots. In production, this probably means you need to use SmartPsiElementPointer somewhere.

0

Please sign in to leave a comment.