'VSCode using Remote SSH - Run Tasks on Host Machine

I am connected to a remote server via SSH using the Remote - SSH plugin by VSCode. I would like to run a task with the path to the remote file on the local machine.

Specifically, I have a HTML file on the remote machine that I would like to preview/interact with on my local windows machine using an URL.

My task.json should look something like this:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Open Remote HTML",
            "type": "shell",
            "command": "start https://url_to_remote.com/${file}"
        }
    ]
}

Edit: Additionally, I have tasks that should continue run on the remote machine and I would like the option to run on the local or host machine on a per task basis.



Solution 1:[1]

Unfortunately running a task locally while using the Remote SSH extension is not possible, though it looks like there's sufficient demand and it has been added to their backlog:

https://github.com/microsoft/vscode-remote-release/issues/168

If you're determined to have this, though, a very hacky way to go about this would be to:

  1. Set up a basic webserver on your local machine at localhostIP

  2. Expose an endpoint that reads a URL parameter named file (http://localhostIP:3000/?file=blah.html) and opens a new tab in firefox/your browser of choice like so:

    firefox --new-tab https://remotehostIP:3000/blah.html
    

    Here's a one line web server runnable from bash through a local terminal (ctrl+shift+P, search integrated to start a local terminal while connected to remote host). This web server accomplishes steps 1 and 2 mentioned here (ctrl+c to stop web server):

    echo "const http = require('http');const {exec} = require('child_process');http.createServer(function (req, res) {h='http://remotehostIP:3000/';exec(\`firefox --new-tab \${h+req.url.slice('/?file='.length)}\`);res.end();}).listen(3000);" | node
    

    Indented, easier to read code:

    const http = require('http');
    const {exec} = require('child_process');
    
    http.createServer(function (req, res) {
        h="http://remotehostIP:3000/";
        exec(`firefox --new-tab ${h+req.url.slice("/?file=".length)}`);
        res.end();
    }).listen(3000);
    
  3. Lastly, create a task that curls and hits that localhostIP webserver and passes along ${relativeFile}. This task runs on the remote machine, but will trigger your localhostIP's webserver to open firefox. Here's what that might look like

     "tasks": [
         {
             "label": "show current html file",
             "type": "shell",
             "command": "curl",
             "args": ["http://localhostIP:3000/?file=${relativeFile}"],           
             "group": {
                 "kind": "build",
                 "isDefault": true
             }
         }
     ]
    

I think for now this is about as good as you're going to get with a best-effort solution while not investing a bunch of time. Let me know if you run into any issues!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 abelito