FileTypeIndex is unstable in tests

Answered

I have a file type that is identifiable by virtual file:

object TopLevelFormFileType : LanguageFileType(JsonLanguage.INSTANCE), FileTypeIdentifiableByVirtualFile {

override fun getDefaultExtension() = "json"

override fun getDisplayName() = name

override fun getName() = "Form"

override fun getDescription() = "Form"

override fun getIcon() = Icons.TOP_LEVEL_FORM_ICON

override fun isMyFileType(file: VirtualFile) =
file.path.contains("config/forms")

}

It is registered in plugin.xml:

<fileType name="Form"
language="JSON"
implementationClass="<long_path>.TopLevelFormFileType"
fieldName="INSTANCE" />

When i run a completion test, it invokes the following code:

FileTypeIndex.getFiles(TopLevelFormFileType, scope)
Previously, I used FilenameIndex.getAllFilesByExt() to retrieve all files and the test passed. Now switched to FileTypeIndex.getFiles() and it fails.
FileTypeIndex.getFiles() returns extra results that do not correspond to the condition in isMyFileType. I invoked it in debug:

As you can see, there is 1 extra result (reflect-config.json). It does not follow the condition file.path.contains("config/forms"), but it is still returned.
Please help me understand if this is a bug or I am doing something wrong.
0
5 comments

Update: found this thread. Looks very similar to my problem, but there is no clear solution to it.

0

Update2: it's dumb, but I've managed to fix it by switching from fixture.configureByText to fixture.addFileToProject and then opening it in the editor.

But I am not happy with this result. It seems quite random. Looking for an explanation. 

Interestingly enough, in the thread I mentioned in previous comment, the solution was kinda similar: link. It seems like there really are problems with indexes in tests.

0

Please never use `object` for code registered in `plugin.xml`, use `class` for your FileType. https://plugins.jetbrains.com/docs/intellij/using-kotlin.html#caution

Please also try removing test caches for definite clean run https://plugins.jetbrains.com/docs/intellij/ide-development-instance.html#development-instance-settings-caches-logs-and-plugins

 

0

Thank you for your answer.

I tried both options:
1. Changed file types from objects to classes - test fails with the same result
2. Deleted caches from IDEA sandbox - test fails with the same result

So, unfortunately, it did not help. Test fails if I use configureByText. The solution to create the file and then open it in the editor still works. There is nothing wrong about using this approach, but I'd like to know why it works this way.

---

It really seems that something is wrong with caches/indexes. Even when I changed the condition in isMyFileType to always be false, this particular test still returns 4 results. Can't figure why. Seems like this condition is ignored.

I ran the test using gradle and intellij - both give the same result. There must be exactly 3 completions, but actually there are 4. One of them is a mysterious "reflect-config.json" (can be seen on the second screenshot in the original post).

I think it's gonna be much easier if I link the code. Here is the test that fails (named `test form completion`). getVariants is invoked from here.

0

Sorry for delay due to holidays.

The FileType implementations in your plugin _must_ be changed to use _class_ instead of _object_, regardless of this issue and use INSTANCE field instead to use singleton.

Please try using JsonFileType CTOR and specify JsonLanguage.INSTANCE as Language and secondary=true.

Does it help?

-- repro notes:

- adjust idea.home.path in build.gradle.kts

- disable FormTestBase#withSolarDependency

0

Please sign in to leave a comment.