Skip to content

Incorrect Command Variable Resolution in tasks.json if task type is 'process' #160891

Closed

Description

@stuartthomson

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: microsoft/vscode-python#19265. They suggested that it's an upstream problem and to raise here.

  • 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:

  1. 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.
  2. Create a tasks.json file something like:
{
    "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}"
      }
    ]
  }
  1. Run the task
  2. 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.

Activity

  1. added
    tasks
    bug
    on Sep 14, 2022
  2. added
    help wanted
    and removed on Sep 22, 2022
  3. karrtikr commented on Jul 25, 2023

    @karrtikr

    Here're concise steps that can be used to reproduce the issue:

    Steps to reproduce:

    Please clone this repo https://github.com/pe224/vscode-python-venv-bad-path
    All preparatory steps are indicated in the README.

    interpreterPath fails with

     *  Executing task: C:\Users\<User>\projects\vscode-python-venv-bad-path\c:\Users\<User>\projects\vscode-python-venv-bad-path\.venv\Scripts\python.exe -m httpie 
    
     *  The terminal process failed to launch: Path to shell executable "C:\Users\<User>\projects\vscode-python-venv-bad-path\c:\Users\<User>\projects\vscode-python-venv-bad-path\.venv\Scripts\python.exe" does not exist.
    

    This is the bug - seems like the workspace folder is prefixed to the actual (correct) venv python path.

  4. barsikus007 commented on Jan 11, 2024

    @barsikus007

    any updates on that issue?

  5. nylnx commented on May 28, 2024

    @nylnx

    I would be very interested in a fix as well... Currently we have to duplicate all our tasks with a separate special "Windows version" because of this :(

    And using "shell" instead of "process" is not an option since the escaping of quotation marks is so horribly, horribly broken for tasks...

  6. Daddelhai commented on Dec 13, 2024

    @Daddelhai

    Any update yet? Running into the same issue

  7. tpajenkamp-dspace commented on Jan 29, 2025

    @tpajenkamp-dspace

    This issue also prevents the use of ${command:python.interpreterPath} if the selected Python interpreter is not in a virtual environment:

    The terminal process failed to launch: Path to shell executable "C:\Temp\python\WorkingFolder\C:\Python312\python.exe" does not exist.
    
  8. unawarez commented on Mar 27, 2025

    @unawarez

    Still broken with the same python.interpreterPath use case. I would really like to not use shell so that I don’t have to worry about quoting.

  9. anthonykim1 commented on May 27, 2025

    @anthonykim1
    Contributor

    Does using shell type like:

                "type": "shell",
                "command": "python3",
    

    work better for people?

    nylnx unawarez Yeah, it seems like there needs to be some work done to better handle escaping quotes for shell type tasks.
    Is it just the starting and the ending double quote that is giving you trouble or some things additional?

    Thomas Pajenkamp (@tpajenkamp-dspace) Does running C:\Temp\python\WorkingFolder\C:\Python312\python.exe + script your intending to run by itself in the terminal work for you?

  10. tpajenkamp-dspace commented on May 28, 2025

    @tpajenkamp-dspace

    Anthony Kim (@anthonykim1) Of course the command also does not work in the terminal, the two paths C:\Temp\python\WorkingFolder and C:\Python312\python.exe are not supposed to be combined.
    By the way, recent versions of VS Code constructs the path C:\Temp\python\WorkingFolder\"C:\Python312\python.exe", which also does not work.

    A task like

            {
                "label": "myscript",
                "type": "shell",
                "command": "&'${command:python.interpreterPath}'",
                "args": ["myscript.py"]
            }

    works for powershell (no space after the ampersand and make sure to surround the executable path with single quotes), even though I would prefer a solution that is independent of any specific shell syntax.

  11. DetachHead commented on Jun 1, 2025

    @DetachHead
    Contributor

    even though I would prefer a solution that is independent of any specific shell syntax.

    you can define OS-specific configs like this:

    {
        "label": "myscript",
        "type": "shell",
        "windows": {
            "command": "&'${command:python.interpreterPath}'"
        },
        "linux": {
            "command": "${command:python.interpreterPath}" // (or whatever works on linux, i've only tested this on windows)
        }
    }
  12. tpajenkamp-dspace commented on Jun 2, 2025

    @tpajenkamp-dspace

    even though I would prefer a solution that is independent of any specific shell syntax.

    you can define OS-specific configs like this:
    [...]

    That's a good start. However, I would recommend something like this which highlights the need for a more general solution:

    {
        "label": "myscript",
        "type": "shell",
        "command": "python",
        "windows": {
            "command": "&'${command:python.interpreterPath}'",
            "options": {
                "shell": {
                    "executable": "powershell"
                }
            }
        },
        "linux": {
            "command": "${command:python.interpreterPath}", // (or whatever works on linux, i've only tested this on windows),
            "options": {
                "shell": {
                    "executable": "bash" // or whatever
                }
            }
        },
        "osx": {
            // ... let us not forget these guys :)
        }
    }
    
  13. added
    unreleased
    on Nov 4, 2026
  14. added a commit that references this issue on Nov 4, 2026
    00735e5
  15. added
    insiders-released
    and removed
    unreleased
    on Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bughelp wantedinsiders-releasedtasksverified

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions