"Directory index is not initialized yet for Project" at plugin startup

In the plugin I'm developing, I'm needing to get hold of the Vcs Roots when a ProjectComponent starts up. Unfortunately, both the projectOpened and initComponent methods seem to be called before the roots are loaded, because calls to ProjectLevelVcsManager.getInstance(project).getRootsUnderVcs raise an error ("Directory index is not initialized yet for Project") from com.intellij.openapi.roots.impl.DirectoryIndexImpl.checkAvailability.

Is there a better place to put this initialization code, or perhaps a message bus signal that indicates the right time that this kind of initialization can happen?

4 comments
Comment actions Permalink

is postStartupActivity EP's runActivity called too early?

0
Comment actions Permalink

Forgive my ignorance of your abbreviations, but I'm not quite sure what you meant by EP.  That said, you led me down a successful path.

I put the initialization code inside the initComponent() method, inside a registerPostStartupActivity:

    public void initComponent() {
        StartupManager.getInstance(myProject).registerPostStartupActivity(new Runnable() {
            public void run() { /* initialization code */ }
        });
    }

Thanks for the help.

0
Comment actions Permalink

> what you meant by EP

my apologies Matt,

EP: "Extension Point"

 
StartupActivityImpl implements StartupActivity {
 
@Override
public void runActivity(@NotNull Project project) {

     your code

}

}

I guess you did the same thing without yet another file ;)

0
Comment actions Permalink

Thank you for sharing your solution. I like it much better than another extension file. I just added some initialization code into my plugin and got the same errors.

0

Please sign in to leave a comment.