For many year my go-to editor on Ubuntu was Geany but in recent time I have come to prefer and rely on Visual Studio Code for development.

However there has been one persistently annoying context menu bug I have suffered for a long time now. At random times the right-click context menu would open then close immediately, often selecting a menu item that happened to be under the mouse cursor… There is an open issue about this problem that has yet to be fixed since there was no consensus on replicating or cause.

When the issue cropped up again, I thoroughly read through the issue and replicated locally the issue to better understand it. As described in one of the first comments it is a result of the mouse up action clicking on a menu item, most often seen with a workspace that has a non-zero zoom level. From these details I figured out a way to workaround the problem locally with the custom-styled menu.

First by using VSCode developer tools (under Help menu) to inspect the menu item CSS and find that adding a few pixels of left margin toaction-item ensures the mouse up action doesn’t activate the menu item when the menu opens.

An extra layer of challenge on Ubuntu is that VSCode is installed via Snap. These packages are read-only for security so cannot easily be modified in-place. After a bit of searching the answer is to extract the package locally, make the required file changes and install the modified package using the debug tool snap try:

	  mkdir ~/snap-repo && cd ~/snap-repo
	  sudo snap download code

	  # Unpack snap to 'code' directory
	  unsquashfs -d code code*.snap

Edit the file located under ~/snap-repo/code/usr/share/code/resources/app/out/vs/workbench/workbench.desktop.main.js and adding a left margin as shown in this diff:

	   .monaco-menu .monaco-action-bar.vertical .action-item {
	   	display: block;
	  +	margin-left: 2px;
	   }

With the change made install this modified snap using snap try:

	  # Install unsquash'd directory
	  sudo snap try code/ --classic

	  snap list code

	  Name  Version   Rev  Tracking  Publisher  Notes
	  code  b3e4e68a  x1   -         -          classic,try

This installed VSCode will now remain pinned to that local package and won’t be updated.