How to properly handle file paths with spaces when calling task using ShellExecution

Viewed 74

I've been banging my head trying to fix an issue with an VSCode extension I have created. This extension obtains the file path of the currently selected item in the VScode explorer tree. It then passes the path to a task for execution. The extension works fine for items that do not contain spaces in its file path but fails miserably when a path with a space is encountered.

Here is a simplified snippet:

import * as vscode from 'vscode';

export async function activate(context: vscode.ExtensionContext) {
  let disposable = vscode.commands.registerCommand('playwright-vscode-trace-viewer.show', async (...args) => {
    let path = args[0].fsPath;

    const p: vscode.ShellQuotedString = {
      value: path,
      quoting: vscode.ShellQuoting.Escape
    }

    const shellExec = new vscode.ShellExecution('npx', ['playwright', 'show-trace', p]);
    let tsk = new vscode.Task({ type: `trace-viewer${Math.random()}` },
      vscode.TaskScope.Workspace,
      'trace-viewer',
      'trace-viewer',
      shellExec,
      'npx');
    vscode.tasks.executeTask(tsk);
  });

  context.subscriptions.push(disposable);
}

export function deactivate() { }

When I execute the extension against a filepath:

c:\Users\ryanr\_repo\pw-tyscript\test-results\tests-start_server-bar-cw\trace (1).zip

It garbles up the path and fails ...

> Executing task: npx playwright show-trace c:\Users\ryanr\_repo\pw-tyscript\test-results\tests-start_server-bar-cw\trace` `(1`).zip <

No such file or directory: c:\Users\ryanr\_repo\pw-tyscript\test-results\tests-start_server-bar-cw\trace

I can execute the command manually using powershell like so:

npx playwright show-trace c:\Users\ryanr\_repo\pw-tyscript\test-results\tests-start_server-bar-cw\trace` `(1`).zip

I suspect I'm not calling vscode.ShellExecution correctly?

0 Answers
Related