In this post I explain how I found the Run As menu item mysteriously disappear and how it can be fixed.

I ran into an unexpected issue while I was implementing a new launch configuration shortcut for our RCP application. The Run as menu only appeared the first time the Project Explorer's context menu was shown. (The Run as menu item was shown together with the Debug as and Profile as menu item, even though I did not declare any debug or profile shortcuts.) After it was shown once, it disappeared, regardless whether the action was run or not. Since I spend considerable time debugging this issue, I took the time to blog my solution to this strange behavior.

Run As menu item (first display): Run As menu item (first display)

Run As menu item (second display): Run As menu item (second display)

I initially thought I was misusing the org.eclipse.debug.ui.launchShortcuts extension point. But after re-reading the extension point description a few times, lots of trial and error experiments, I had to look further.

After some experimenting, I found out that the menu items disappeared a soon as the Debug plugin was loaded. If I activated the Debug plugin by clicking the Run menu, the Run as menu never occurred in the Project explorer's context menu. So I digged into the org.eclipse.debug.ui-plugin and found following extension point:

<extension  point="org.eclipse.ui.popupMenus">
  ...
  <objectContribution 
      objectClass="java.lang.Object"
      id="org.eclipse.debug.ui.contextualLaunch.run">
      <action 
         label="%RunContextMenu.label"
         style="pulldown"
         class="org.eclipse.debug.internal.ui.actions.RunContextualLaunchAction"
         menubarPath="additions"
         enablesFor="+"
         id="org.eclipse.debug.ui.contextualLaunch.run.submenu">
      </action>
      <enablement>
         <or>
            <and>
              <not><with variable="org.eclipse.core.runtime.Platform">
                 <test property="org.eclipse.core.runtime.bundleState" args="org.eclipse.debug.core" value="ACTIVE"/>
              </with></not>
              <adapt type="org.eclipse.core.resources.IResource"/>
            <and>
              <with variable="org.eclipse.core.runtime.Platform">
                 <test property="org.eclipse.core.runtime.bundleState" args="org.eclipse.debug.core" value="ACTIVE"/>
              </with>
              <test property="org.eclipse.debug.core.launchable" value="run"/>
            </and>
         </or>
      </enablement>
   </objectContribution>
   ...
</extension>

There I saw it, the Run as menu is enabled with different conditions whether the plugin is loaded or not. If the plugin is not loaded yet, the menu is shown for all objects that are adaptable to IResource. Once the plugin is loaded, the "org.eclipse.debug.core.launchable" test property is used.

The "org.eclipse.debug.core.launchable" is defined in org.eclipse.debug.core and runs a method, Test, in class org.eclipse.debug.internal.core.LaunchablePropertyTester. This method tests whether there is a "run" launch mode available and whether the selection has an org.eclipse.debug.ui.actions.ILaunchable adapter.

So it turned out that I only needed to add an ILaunchable adapter for the resources for which I can create a launch shortcut. I solved this by adding this extension point to my plugin.xml:

<extension point="org.eclipse.core.runtime.adapters">
  <factory class="org.eclipse.core.runtime.IAdaptable"
           adaptableType="org.eclipse.core.resources.IResource">
     <adapter type="org.eclipse.debug.ui.actions.ILaunchable"/>
  </factory>
</extension>

I finally I had the intended behavior. At least, as soon as the plugin is actually loaded.

P.S.: Different behavior when run in Eclipse

I also noted that this strange Run as menu item behavior did not occur when I ran my plugin inside the Eclipse IDE instead of inside our RCP application. The a posteriori explanation is that other plugins (JDT, JUnit,...?) must have implementations for the missing IResource adapter.