are there ways to use VS code plugins in google cloud shell?

Viewed 525

I have a few quick navigation plugins such as "block travel" I use all the time. Is there a way to use these in cloud shell?

I imagine there are some restrictions, but even some simple editor plugins can be huge timesavers.

While I'm at it - alt-D to duplicate a line, or transpose lines - some of those seem to be missing and hard to use key remapping to get working, at least within the shell. In general maybe keyboard shortcuts seem to get trapped by the browser or PWA wrapper. I'm using cloudshell as a webapp on a chromebook FWIW, for various secure projects.

3 Answers

I have come up with a solution that covers both aspects of your question

To get Unlimited Persistent Disk:

You can use Google Cloud Storage FUSE

Google Cloud Storage FUSE lets you mount a GCS bucket as a folder to your linux instance. By doing that you get an “unlimited '' persistent disk and it is super simple to set up since gcsfuse is already installed in cloud shell.

1.  Create a GCS bucket (you only need to run this once) -- replace BUCKET_NAME with any name:
gsutil mb "gs://BUCKET_NAME/"

2.  Create a local directory for mounting -- replace FOLDER_NAME with the chosen directory name:
mkdir /home/[USER]/[FOLDER_NAME]
chmod 777 /home/[USER]/[FOLDER_NAME]

3.  Mount the bucket onto the local filesystem (note: you need to re-run this every time Cloud Shell starts)
gcsfuse -o nonempty -file-mode=777 -dir-mode=777 --uid=1000 --debug_gcs  [BUCKET_NAME]  /home/[USER]/[FOLDER_NAME]

To use third party plugins in cloud shell:

You can use an environment customization script (.customization_environment) as mentioned in the public documentation. It allows you to install additional packages into your Cloud Shell environment when it starts.

For reference, below are the steps to install VS Code plug-in.

Step 1:

To install the VSCode server, run the script named visual_studio_code.sh as below, in the root directory workspace of Cloud Shell Editor.

visual_studio_code.sh file:

export VERSION=`curl -s https://api.github.com/repos/cdr/code-server/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")'`
wget https://github.com/cdr/code-server/releases/download/$VERSION/code-server-3.10.2-linux-amd64.tar.gz
tar -xvzf code-server-3.10.2-linux-amd64.tar.gz
cd code-server-3.10.2-linux-amd64

Run the script using the below command in shell,

./visual_studio_code.sh


if getting permission denied error then run this following command in shell,

chmod +x visual_studio_code.sh
./visual_studio_code.sh

Step 2:

Make a customization script in the root directory workspace in Cloud Shell Editor to start VS Code Server on boot with the below commands :

.customization_environment file :

#!/bin/sh
#.customize_environmnet run in background as root, wait for your user to initialize
sleep 20
sudo -u [USER]  /home/[USER]/code-server-3.10.2-linux-amd64/code-server --auth none --port 9090

Step 3:

To view Visual Studio Code server on port 9090 :

Click on Web Preview > Change Port > 9090

If getting a 404 error, remove ‘?authuser=0’ from the url. Visual Studio Code Server will now be running on the browser!!!

Block travel navigation plugin:

To have the block travel navigation plugin in cloud shell,follow the below commands and run them in shell in root directory:

wget https://github.com/efatsi/block-travel/archive/refs/tags/v1.0.0.tar.gz
tar xzvf  v1.0.0.tar.gz
ls
#You will see block-travel-1.0.0  

block-travel-1.0.0/keymaps/block-travel.cson --auth none --port 9090
#You might get Permission denied if yes, then follow the next two commands else go to webport view in 9090

chmod +x block-travel-1.0.0/keymaps/block-travel.cson
block-travel-1.0.0/keymaps/block-travel.cson --auth none --port 9090

Open the webport view in 9090, you will be able to navigate through the vs code files using :

Alt+up for block-travel.jumpUp
Alt+shift+up for block-travel.selectUp
Alt+down for block-travel.jumpDown
Alt+shift+down for block-travel.selectDown

WARNING: This should not be considered a long term solution, just a stop gap until this is supported in an easier fashion.

This might not be the greatest idea but it does seem to work for the vim extension I tried in my environment. Probably best to make a request through the in product feedback to get it officially added but until then you can follow these steps.

  1. Upload the .vsix package to your $HOME directory.
  2. Unzip the package into the /google/devshell/editor/theia/plugins directory. This action will not persist so you'll want to add the command to the .customize_environment script actions.

e.g.

sudo unzip vscodevim.vsix -d /google/devshell/editor/theia/plugins/vscode-vim

Now for the questionable part. You'll want to install the pslist package to make life easy so you have access to the rkill command. You probably also want to add this to the .customize_environment file as well since it also will not persist.

sudo apt install pslist

Now we need to get the process id for the editor. Currently this seems to be spawned by a supervisord command, which also spawns the tmux section so we're going to grab the process id that is from the runuser command it spawns (and filter for the theia one just in case).

ps ax | grep runuser | grep "theia start"

Then we can use rkill to kill the process and all of the its children, which will cause supervisord to restart it for us and the plugin should be available.

sudo rkill PID_OF_GREP_OUTPUT

I'm not sure the best way to script the rkill command yet, since I don't know the timing of when it's up vs the .customize_environment execution, so right now I run this each time I start up a new VM.

If anything goes horribly wrong, you should be able to request a restart of the VM from the menu options and get a fresh one.

Cloud Shell offers VS Code editor experience through Theia. Did you try cloud Code editor in the cloud shell? that is exposed through "Open Editor" button on the top right, this will open cloud code editor that gives you VSCode experience. You have all the navigation keys that are available in the editor.

enter image description here

Related