Repository: vscode
Issue: Incorrect Command Variable Resolution in tasks.json if task type is 'process'
Labels: bug,help wanted,tasks

<!-- ⚠️⚠️ Do Not Delete This! bug_report_template ⚠️⚠️ -->
<!-- Please read our Rules of Conduct: https://opensource.microsoft.com/codeofconduct/ ✔-->
<!-- 🕮 Read our guide about submitting issues: https://github.com/microsoft/vscode/wiki/Submitting-Bugs-and-Suggestions ✔-->
<!-- 🔎 Search existing issues to avoid creating duplicates. ✔ -->
<!-- 🧪 Test using the latest Insiders build to see if your issue has already been fixed: https://code.visualstudio.com/insiders/ ✔ Didn't find anything describing exactly this -->
<!-- 💡 Instead of creating your report here, use 'Report Issue' from the 'Help' menu in VS Code to pre-fill useful information. -->
<!-- 🔧 Launch with `code --disable-extensions` to check. -->
Does this issue occur when all extensions are disabled?: **I suspect so, can't figure out how to test.** I would need an in-built vscode command that returns a string and for some reason am struggling to find one. I first noticed the issue using vscode-python and it has already been raised there: https://github.com/microsoft/vscode-python/issues/19265. They suggested that it's an upstream problem and to raise here.

<!-- 🪓 If you answered No above, use 'Help: Start Extension Bisect' from Command Palette to try to identify the cause. Don't think this is applicable in this case -->
<!-- 📣 Issues caused by an extension need to be reported directly to the extension publisher. The 'Help > Report Issue' dialog can assist with this. -->

- VS Code Version:
```
Version: 1.71.0 (system setup)
Commit: 784b0177c56c607789f9638da7b6bf3230d47a8c
Date: 2022-09-01T07:36:10.600Z
Electron: 19.0.12
Chromium: 102.0.5005.167
Node.js: 16.14.2
V8: 10.2.154.15-electron.0
OS: Windows_NT x64 10.0.19044
Sandboxed: No
```
- OS Version: Windows_NT x64 10.0.19044

## Steps to Reproduce:

0. Either: install python extension or find a vscode command that returns a string and can be used as a command variable, as per [these docs](https://code.visualstudio.com/docs/editor/variables-reference#_command-variables).
1. Create a tasks.json file something like:
```jsonc
{
    "version": "2.0.0",
    "tasks": [
      {
        "label": "python: run",
        // I think the issue only arises for type 'process'
        "type": "process",  
        // This command could potentially be an in-built vscode one but I can't find one
        "command": "${command:python.interpreterPath}"
      }
    ]
  }
```
3. Run the task
4. Observe the output in the terminal, I get the following:
```The terminal process failed to launch: Path to shell executable "C:\projects\ots\testVscodeWorkspace\C:\Python310\python.exe" does not exist.```

## Suspected Cause
I've been looking into this a bit and I think I might know why it's happening. Apologies if this is hard to follow:

- There is a call in `ExtHostTask.$resolveVariables` to `win32.findExecutable`. The executable it is trying to find is the string `"${command:python.interpreterPath}"`.
- It doesn't think this looks like an absolute path, so it prepends it with the current working directory (see the very end of the `win32.findExecutable`) function. It doesn't check if this exists or not.
- Later on, there is further variable resolution done by the `VariableResolver` class in `terminalTaskSystem.ts`. This resolver has the details needed to expand the interpreter path correctly, but it has already been prepended with the cwd.

## Proposed Solutions

- Perhaps the `win32.findExecutable` function could check if the executable exists at the location, and if it doesn't, simple return the unaltered process name (in this case `"${command:python.interpreterPath}"`)?

- Alternatively, perhaps this call could be somehow removed from the setup done in `ExtHostTask`?

- Perhaps the path could be resolved before running it - if there are two absolute paths then the right-most one could be used.