Skip to content

Wrong interpreter is used in tasks.json, if other variables are used in task #1659

Closed as not planned
microsoft/vscode-python
#26085

Description

Environment data

  • Python Environments extension version: 1.36.0
  • Python extension version: 2026.4.0
  • VS Code version: 1.130.0 (user)
  • OS and version: Windows 11 Enterprise 25H2
  • Python version: 3.14.6 (installed using Python Install Manager)
  • Environment manager in use: uv 0.11.29
  • Shell: pwsh
  • Remote / container scenario: none
  • Workspace type: singe folder
  • Is this a regression? If yes, last known working extension version: unknown

Repro Steps

  1. Create an empty workspace
  2. Make sure that python.useEnvironmentsExtension is true
  3. Create an virtual environment in it (e.g. uv init; uv sync or python -mvenv .venv)
  4. Select the virtual environment as Python interpreter for the workspace (Ctrl-Shift-P => Python: Select interpreter)
  5. Add this .vscode/tasks.json
{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Test (args): OK",
      "type": "shell",
      "command": "${command:python.interpreterPath}",
      "args": ["-c", "import sys; print(sys.executable)", "Dummy"],
      "group": {"kind": "build"}
    }, {
      "label": "Test (args): ERROR (input)",
      "type": "shell",
      "command": "${command:python.interpreterPath}",
      "args": ["-c", "import sys; print(sys.executable)", "${input:Dummy}"],
      "group": {"kind": "build"}
    }, {
      "label": "Test (args): ERROR (env)",
      "type": "shell",
      "command": "${command:python.interpreterPath}",
      "args": ["-c", "import sys; print(sys.executable)", "${env:DUMMY}"],
      "group": {"kind": "build"}
    }, {
      "label": "Test (cmd): OK",
      "type": "shell",
      "command": "${command:python.interpreterPath} -c 'import sys; print(sys.executable)'",
      "group": {"kind": "build"}
    }, {
      "label": "Test (cmd): ERROR (env)",
      "type": "shell",
      "command": "${command:python.interpreterPath} -c 'import sys; print(sys.executable)' '${env:DUMMY}'",
      "group": {"kind": "build"}
    }
  ],
  "inputs": [
    {
      "id": "Dummy",
      "type": "promptString",
      "description": "Dummy string for demonstration",
      "default": "Dummy"
    }
  ]
}
  1. Run the different build tasks

Expected behaviour

All tasks should use (and output) Python binary from the virtual environment (e.g. D:\src\vscode-bug\.venv\Scripts\python.exe)

Actual behavior

All tasks (labelled … OK) without additional variables in the command or args run as expected using the Python interpreter from the .venv for ${command:python.interpreterPath}.

All tasks (labelled … ERROR) with additional variables in the command or args run using the GLOBAL Python interpreter for ${command:python.interpreterPath}.
Neither the user nor the workspace setting for python.defaultInterpreterPath are used.
Image

Logs

Error case

Running task "Test (args): ERROR (env)"

Python Environments Trace

2026-07-24 16:34:47.720 [debug] Checking venv (ms-python.python:venv) for d:\src\vscode-bug\.venv\Scripts\python.exe
2026-07-24 16:34:47.721 [debug] Using venv (ms-python.python:venv) to handle d:\src\vscode-bug\.venv\Scripts\python.exe
2026-07-24 16:34:50.060 [debug] inlineScriptLazyDetector: event received (open) output:ms-python.vscode-python-envs.Python%20Environments.log
2026-07-24 16:34:50.060 [debug] inlineScriptLazyDetector: skipped (open) output:ms-python.vscode-python-envs.Python%20Environments.log (scheme='output', extname='.log', inWorkspace=false)

Python Trace

2026-07-24 16:34:47.800 [debug] Extension unknown accessed getActiveEnvironmentPath with args: undefined
2026-07-24 16:34:47.800 [debug] Extension unknown accessed resolveEnvironment with args: {"id":"D:\\SRC\\VSCODE-BUG\\.VENV\\SCRIPTS\\PYTHON.EXE","path":"d:\\src\\vscode-bug\\.venv\\Scripts\\python.exe"}

Task output

 *  Executing task: C:\Users\MyUserName\AppData\Local\Python\pythoncore-3.14-64\python.exe -c 'import sys; print(sys.executable)'  

C:\Users\MyUserName\AppData\Local\Python\pythoncore-3.14-64\python.exe
 *  Terminal will be reused by tasks, press any key to close it. 

Good case

Running task "Test (args): OK"

Python Environments Trace

2026-07-24 16:33:56.467 [debug] Checking venv (ms-python.python:venv) for d:\src\vscode-bug\.venv\Scripts\python.exe
2026-07-24 16:33:56.467 [debug] Using venv (ms-python.python:venv) to handle d:\src\vscode-bug\.venv\Scripts\python.exe
2026-07-24 16:33:58.000 [debug] inlineScriptLazyDetector: event received (open) output:ms-python.vscode-python-envs.Python%20Environments.log
2026-07-24 16:33:58.000 [debug] inlineScriptLazyDetector: skipped (open) output:ms-python.vscode-python-envs.Python%20Environments.log (scheme='output', extname='.log', inWorkspace=false)

Python Trace

2026-07-24 16:33:56.613 [debug] Extension unknown accessed getActiveEnvironmentPath with args: undefined
2026-07-24 16:33:56.613 [debug] Extension unknown accessed resolveEnvironment with args: {"id":"D:\\SRC\\VSCODE-BUG\\.VENV\\SCRIPTS\\PYTHON.EXE","path":"d:\\src\\vscode-bug\\.venv\\Scripts\\python.exe"}

Task output

 *  Executing task: d:\src\vscode-bug\.venv\Scripts\python.exe -c 'import sys; print(sys.executable)' Dummy 

D:\src\vscode-bug\.venv\Scripts\python.exe
 *  Terminal will be reused by tasks, press any key to close it.

Additional context

Python Environments Trace during VS Code startup

2026-07-24 16:20:47.807 [info] Python-envs extension version: 1.36.0
2026-07-24 16:20:47.808 [info] 
=== Python Envs Configuration Levels ===
2026-07-24 16:20:47.808 [info] {
  "section": "Python Envs Configuration Levels",
  "defaultEnvManager": {
    "workspaceFolderValue": "undefined",
    "workspaceValue": "undefined",
    "globalValue": "undefined",
    "defaultValue": "ms-python.python:venv"
  },
  "defaultPackageManager": {
    "workspaceFolderValue": "undefined",
    "workspaceValue": "undefined",
    "globalValue": "undefined",
    "defaultValue": "ms-python.python:pip"
  }
}
2026-07-24 16:20:47.808 [info] [pet] Starting Python Locator c:\Users\MyUserName\.vscode\extensions\ms-python.vscode-python-envs-1.36.0-win32-x64\python-env-tools\bin\pet.exe server
2026-07-24 16:20:47.808 [info] Registering pyenv manager (environments will be discovered lazily)
2026-07-24 16:20:47.808 [info] Registering pipenv manager (environments will be discovered lazily)
2026-07-24 16:20:47.808 [info] Registering poetry manager (environments will be discovered lazily)
2026-07-24 16:20:47.808 [info] [pet] configure: Sending configuration update: {"workspaceDirectories":["d:\\src\\vscode-bug"],"environmentDirectories":["d:/src/vscode-bug/.venv","d:/src/vscode-bug/*/.venv"],"pipenvExecutable":"pipenv","poetryExecutable":"poetry","cacheDirectory":"c:\\Users\\MyUserName\\AppData\\Roaming\\Code\\User\\globalStorage\\ms-python.vscode-python-envs\\pythonLocator"}
2026-07-24 16:20:47.808 [info] Extension for manager ms-python.python:venv is not active: Activating...
2026-07-24 16:20:47.813 [error] [pet] �[2m   0.077059700s�[0m �[33m WARN�[0m �[2mpet_windows_registry::environments�[0m�[2m:�[0m Failed to open HKLM\Software\Python, Os { code: 2, kind: NotFound, message: "The system cannot find the file specified." }

2026-07-24 16:20:47.816 [info] Discovered env: C:\Users\MyUserName\AppData\Local\Python\pythoncore-3.13-64\python.exe
2026-07-24 16:20:47.816 [info] Discovered env: C:\Users\MyUserName\AppData\Local\Python\pythoncore-3.14-64\python.exe
2026-07-24 16:20:47.834 [info] Discovered env: d:\src\vscode-bug\.venv\Scripts\python.exe
2026-07-24 16:20:47.866 [error] [pet] �[2m   0.131520500s�[0m �[31mERROR�[0m �[2mpet_python_utils::env�[0m�[2m:�[0m Failed to spawn Python to resolve info "C:\\Users\\MyUserName\\AppData\\Local\\Python\\bin\\python.exe.__target__": %1 is not a valid Win32 application. (os error 193)
�[2m   0.131563500s�[0m �[33m WARN�[0m �[2mpet::find�[0m�[2m:�[0m Unknown Python Env "C:\\Users\\MyUserName\\AppData\\Local\\Python\\bin\\python.exe.__target__"

2026-07-24 16:20:47.869 [error] [pet] �[2m   0.137766400s�[0m �[31mERROR�[0m �[2mpet_python_utils::env�[0m�[2m:�[0m Failed to spawn Python to resolve info "C:\\Users\\MyUserName\\AppData\\Local\\Python\\bin\\python3.13.exe.__target__": %1 is not a valid Win32 application. (os error 193)
�[2m   0.137799300s�[0m �[33m WARN�[0m �[2mpet::find�[0m�[2m:�[0m Unknown Python Env "C:\\Users\\MyUserName\\AppData\\Local\\Python\\bin\\python3.13.exe.__target__"

2026-07-24 16:20:47.876 [error] [pet] �[2m   0.145041700s�[0m �[31mERROR�[0m �[2mpet_python_utils::env�[0m�[2m:�[0m Failed to spawn Python to resolve info "C:\\Users\\MyUserName\\AppData\\Local\\Python\\bin\\python3.14.exe.__target__": %1 is not a valid Win32 application. (os error 193)
�[2m   0.145083400s�[0m �[33m WARN�[0m �[2mpet::find�[0m�[2m:�[0m Unknown Python Env "C:\\Users\\MyUserName\\AppData\\Local\\Python\\bin\\python3.14.exe.__target__"

2026-07-24 16:20:47.897 [info] Extension for manager ms-python.python:venv is now active.
2026-07-24 16:20:47.899 [error] [pet] �[2m   0.153364300s�[0m �[31mERROR�[0m �[2mpet_python_utils::env�[0m�[2m:�[0m Failed to spawn Python to resolve info "C:\\Users\\MyUserName\\AppData\\Local\\Python\\bin\\python3.exe.__target__": %1 is not a valid Win32 application. (os error 193)
�[2m   0.153397100s�[0m �[33m WARN�[0m �[2mpet::find�[0m�[2m:�[0m Unknown Python Env "C:\\Users\\MyUserName\\AppData\\Local\\Python\\bin\\python3.exe.__target__"

2026-07-24 16:20:49.363 [info] Resolved environment during PET refresh: d:\src\vscode-bug\.venv\Scripts\python.exe
2026-07-24 16:20:49.425 [info] Found venv environment: .venv (3.14.6.final.0)
2026-07-24 16:20:49.474 [info] Resolved Python Environment C:\Users\MyUserName\AppData\Local\Python\pythoncore-3.13-64\python.exe
2026-07-24 16:20:50.544 [info] Conda not found, turning off conda features. [Error: Conda not found
	at L (c:\Users\MyUserName\.vscode\extensions\ms-python.vscode-python-envs-1.36.0-win32-x64\dist\extension.js:2:629081)
	at async V (c:\Users\MyUserName\.vscode\extensions\ms-python.vscode-python-envs-1.36.0-win32-x64\dist\extension.js:2:629206)
	at async t.registerCondaFeatures (c:\Users\MyUserName\.vscode\extensions\ms-python.vscode-python-envs-1.36.0-win32-x64\dist\extension.js:2:648591)
	at async t.safeRegister (c:\Users\MyUserName\.vscode\extensions\ms-python.vscode-python-envs-1.36.0-win32-x64\dist\extension.js:2:290365)
	at async Promise.all (index 1)
	at async Immediate.<anonymous> (c:\Users\MyUserName\.vscode\extensions\ms-python.vscode-python-envs-1.36.0-win32-x64\dist\extension.js:2:782813)]
2026-07-24 16:20:50.545 [info] [interpreterSelection] Applying initial environment selection for 1 workspace folder(s)
2026-07-24 16:20:50.554 [info] Python API: Changed environment from undefined to .venv (3.14.6.final.0) for: d:\src\vscode-bug
2026-07-24 16:20:50.554 [info] Internal: Changed environment from undefined to .venv (3.14.6.final.0) for: d:\src\vscode-bug
2026-07-24 16:20:50.555 [info] [interpreterSelection] vscode-bug: .venv (3.14.6.final.0) (source: autoDiscovery)
2026-07-24 16:20:50.555 [info] [interpreterSelection] Workspace env resolved, deferring global scope to background
2026-07-24 16:20:50.560 [info] Python API: Changed environment from undefined to Python 3.13.14 for: global
2026-07-24 16:20:50.561 [info] Internal: Changed environment from undefined to Python 3.13.14 for: global
2026-07-24 16:20:50.561 [info] [interpreterSelection] global: Python 3.13.14 (source: autoDiscovery)
2026-07-24 16:20:51.141 [info] Refreshing pyenv environments
2026-07-24 16:20:51.474 [info] Pipenv not found via settings, cache, or PATH
2026-07-24 16:20:51.475 [info] Refreshing pipenv environments
2026-07-24 16:20:51.539 [info] Pipenv not found via settings, cache, or PATH
2026-07-24 16:20:51.539 [info] Found 0 pipenv environments
2026-07-24 16:20:51.592 [info] Pipenv not found via settings, cache, or PATH
2026-07-24 16:20:51.648 [info] Refreshing poetry environments
2026-07-24 16:20:51.724 [info] Poetry executable not found
2026-07-24 16:20:51.774 [info] Environment discovery complete: 4 environments found (Global: 3, venv: 1)

Activity

  1. added
    bugIssue identified by VS Code Team member as probable bug
    on Jul 24, 2026
  2. supervirus commented on Jul 24, 2026

    @supervirus
    Author

    One additional fact: Using the variable ${workspaceFolder} does not cause the error!
    It seems to matter when/how variables are replaced.

    Simple tasks.json

    {
      "version": "2.0.0",
      "tasks": [
        {
          "label": "Test (args): Also OK",
          "type": "shell",
          "command": "${command:python.interpreterPath}",
          "options": {
            "cwd": "${workspaceFolder}"
          },
          "args": ["-c", "import sys; print(sys.executable)", "${workspaceFolder}"],
          "group": {"kind": "build"}
        }
      ]
    }

    Logs

    Output of the sample tasks:

     *  Executing task: d:\src\vscode-bug\.venv\Scripts\python.exe -c 'import sys; print(sys.executable)' D:\src\vscode-bug 
    
    D:\src\vscode-bug\.venv\Scripts\python.exe
     *  Terminal will be reused by tasks, press any key to close it.
    
  3. supervirus commented on Jul 24, 2026

    @supervirus
    Author

    I did some more testing and discovered another possibly related issue.

    Using "type": "process" instead results in "command": "${command:python.interpreterPath}" being resolved to the Python executable with duplicated path (visible in the task output below).

    Simple tasks.json

    {
      "version": "2.0.0",
      "tasks": [
        {
          "label": "Test (process): ERROR",
          "type": "process",
          "command": "${command:python.interpreterPath}",
          "group": {"kind": "build"}
        }
      ]
    }

    Logs

    Python Environments Trace

    2026-07-24 16:51:26.223 [debug] inlineScriptLazyDetector: event received (open) output:ms-python.vscode-python-envs.Python%20Environments.log
    2026-07-24 16:51:26.223 [debug] inlineScriptLazyDetector: skipped (open) output:ms-python.vscode-python-envs.Python%20Environments.log (scheme='output', extname='.log', inWorkspace=false)
    

    Python Trace

    No output

    Task output

    *  Executing task: D:\src\vscode-bug\d:\src\vscode-bug\.venv\Scripts\python.exe  
    
    
     *  The terminal process failed to launch: Path to shell executable "D:\src\vscode-bug\d:\src\vscode-bug\.venv\Scripts\python.exe" does not exist.
    
  4. mohityadav8 commented on Jul 28, 2026

    @mohityadav8
    Contributor

    Karthik Nadig (@karthiknadig) i want to work on this issue

  5. karthiknadig commented on Jul 28, 2026

    @karthiknadig
    Member

    Go for it

  6. mohityadav8 commented on Jul 30, 2026

    @mohityadav8
    Contributor

    /working

  7. mohityadav8 commented on Aug 1, 2026

    @mohityadav8
    Contributor

    This isn't a vscode-python-environments bug. Traced both sides:

    • python-envs has no python.interpreterPath command (grepped src + package.json, zero hits). It only exposes python-envs.* commands + the internal resolveEnvironment/getEnvironment API.
    • The Python Environments Trace in the repro is identical in both the OK and ERROR runs — handlePythonPath (src/common/utils/pythonPath.ts) resolves the venv correctly every time, in both cases.

    Root cause is in ms-python.python, specifically src/client/interpreter/interpreterPathCommand.ts:

    if ('workspaceFolder' in args) {
        workspaceFolder = args.workspaceFolder;
    } else if (args[1]) {
        const [, second] = args;
        workspaceFolder = second;
    } else if (useEnvExtension() && 'type' in args && args.type === 'debugpy') {
        ...
    } else {
        workspaceFolder = undefined;
    }

    For tasks.json, it assumes VS Code always invokes the command variable with args[1] = workspace folder path. When extra variable kinds (${input:...}, ${env:...}) are resolved alongside ${command:python.interpreterPath} in the same task, that assumption breaks and workspaceFolder comes back undefined.

    That flows into getActiveInterpreter(undefined) in src/client/interpreter/interpreterService.ts:

    const key = workspaceService.getWorkspaceFolderIdentifier(resource);

    resource === undefined → different (global) cache key than the one populated for the real workspace → returns/caches the wrong interpreter, independent of whatever python-envs correctly resolved.

    i am raising a pr for this in vscode-python

  8. eleanorjboyd commented on Sep 25, 2026

    @eleanorjboyd
    Member

    🤖 Closing this issue here as tracked upstream in the Python extension. The python.interpreterPath task-variable behavior is owned by microsoft/vscode-python, and the proposed fix is microsoft/vscode-python#26085. That PR is still open and has not merged yet, so this is not a claim that the bug is fixed. Please follow the upstream PR for the implementation and verification; we can reopen here if a separate Python Environments issue is identified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugIssue identified by VS Code Team member as probable bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions