Repository navigation
Run selection indentation error #14048
Description
Environment data
- VS Code version:
- OS and version:
Version: 1.49.1
Commit: 58bb7b2331731bf72587010e943852e13e6fd3cf
Date: 2020-09-16T23:21:17.533Z (6 days ago)
Electron: 9.2.1
Chrome: 83.0.4103.122
Node.js: 12.14.1
V8: 8.3.110.13-electron.0
OS: Darwin x64 19.6.0
- Extension version (available under the Extensions sidebar):
2020.8.1 (20 August 2020)
- Python version (& distribution if applicable, e.g. Anaconda):
Python 3.7.8, Python 3.8.4
- Type of virtual environment used (N/A | venv | virtualenv | conda | ...):
venv, and without a virtual environment
- Relevant/affected Python packages and their versions:
N/A
- Relevant/affected Python-related VS Code extensions and their versions:
N/A
- Value of the
python.languageServersetting:
Pylance
[NOTE: If you suspect that your issue is related to the Microsoft Python Language Server (python.languageServer: 'Microsoft'), please download our new language server Pylance from the VS Code marketplace to see if that fixes your issue]
Expected behaviour
When I highlight Python code and run selection I expect it to run if it is syntactically correct.
Actual behaviour
I'm receiving indentation errors in specific scenarios for code that is syntactically correct.
Steps to reproduce:
[NOTE: Self-contained, minimal reproducing code samples are extremely helpful and will expedite addressing your issue]
Run selection on the following code and the following error(s) will occur
if False:
print("if")
elif True:
print("elif")
else:
print("else")if False:
... print("if")
...
>>> elif True:
File "<stdin>", line 1
elif True:
^
SyntaxError: invalid syntax
>>> print("elif")
File "<stdin>", line 1
print("elif")
^
IndentationError: unexpected indent
>>> else:
File "<stdin>", line 1
else:
^
SyntaxError: invalid syntax
>>> print("else")
File "<stdin>", line 1
print("else")
^
IndentationError: unexpected indent
>>>
If I copy/paste the same code into the Python interactive window it will run without error.
Logs
Output for Python in the Output panel (View→Output, change the drop-down the upper-right of the Output panel to Python)
User belongs to experiment group 'AlwaysDisplayTestExplorer - control'
User belongs to experiment group 'ShowPlayIcon - start'
User belongs to experiment group 'DebugAdapterFactory - experiment'
User belongs to experiment group 'PtvsdWheels37 - experiment'
User belongs to experiment group 'UseTerminalToGetActivatedEnvVars - control'
User belongs to experiment group 'LocalZMQKernel - control'
User belongs to experiment group 'CollectLSRequestTiming - control'
User belongs to experiment group 'CollectNodeLSRequestTiming - experiment'
User belongs to experiment group 'EnableIPyWidgets - experiment'
User belongs to experiment group 'RunByLine - experiment'
User belongs to experiment group 'CustomEditorSupport - control'
User belongs to experiment group 'pythonaacf'
User belongs to experiment group 'pythonTipPromptWording'
> conda --version
> conda info --json
> pyenv root
> python3.7 ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python3.6 ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python2 ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> /usr/local/bin/python3 ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
> python3 ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/pyvsc-run-isolated.py -c "import sys;print(sys.executable)"
Error 2020-09-22 22:05:40: Failed to download and store experiments, Class name = b, completed in 528ms, has a falsy return value, , Return Value: undefined [Error: Failed with status 404, Not Found, Uri https://raw.githubusercontent.com/microsoft/vscode-python/master/experiments.json
at H._callback (/Users/dplumley/.vscode/extensions/ms-python.python-2020.9.106471-dev/out/client/extension.js:54:716477)
at H.a._callback.a.callback.a.callback (/Users/dplumley/.vscode/extensions/ms-python.python-2020.9.106471-dev/out/client/node_modules/request.js:189:5442)
at H.emit (events.js:223:5)
at H.<anonymous> (/Users/dplumley/.vscode/extensions/ms-python.python-2020.9.106471-dev/out/client/node_modules/request.js:189:18302)
at H.emit (events.js:223:5)
at IncomingMessage.<anonymous> (/Users/dplumley/.vscode/extensions/ms-python.python-2020.9.106471-dev/out/client/node_modules/request.js:189:17134)
at Object.onceWrapper (events.js:312:28)
at IncomingMessage.emit (events.js:228:7)
at endReadableNT (_stream_readable.js:1185:12)
at processTicksAndRejections (internal/process/task_queues.js:81:21)]
Error 2020-09-22 22:05:40: Failed to initialize experiments, Class name = b, completed in 535ms, has a falsy return value, , Return Value: undefined [Error: Failed with status 404, Not Found, Uri https://raw.githubusercontent.com/microsoft/vscode-python/master/experiments.json
at H._callback (/Users/dplumley/.vscode/extensions/ms-python.python-2020.9.106471-dev/out/client/extension.js:54:716477)
at H.a._callback.a.callback.a.callback (/Users/dplumley/.vscode/extensions/ms-python.python-2020.9.106471-dev/out/client/node_modules/request.js:189:5442)
at H.emit (events.js:223:5)
at H.<anonymous> (/Users/dplumley/.vscode/extensions/ms-python.python-2020.9.106471-dev/out/client/node_modules/request.js:189:18302)
at H.emit (events.js:223:5)
at IncomingMessage.<anonymous> (/Users/dplumley/.vscode/extensions/ms-python.python-2020.9.106471-dev/out/client/node_modules/request.js:189:17134)
at Object.onceWrapper (events.js:312:28)
at IncomingMessage.emit (events.js:228:7)
at endReadableNT (_stream_readable.js:1185:12)
at processTicksAndRejections (internal/process/task_queues.js:81:21)] {
vslsStack: [
CallSite {}, CallSite {},
CallSite {}, CallSite {},
CallSite {}, CallSite {},
CallSite {}, CallSite {},
CallSite {}, CallSite {}
]
}
Python interpreter path: /usr/local/bin/python3
Starting Pylance language server.
> /usr/local/bin/python3 ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/pyvsc-run-isolated.py -c "import jupyter"
> /usr/local/bin/python3 ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/pyvsc-run-isolated.py -c "import jupyter"
> /usr/local/bin/python3 ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/pyvsc-run-isolated.py -c "import notebook"
> /usr/local/bin/python3 ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/pyvsc-run-isolated.py -c "import notebook"
> conda --version
> /usr/local/bin/python3 ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/normalizeForInterpreter.py "if False:
print("if")
elif True:
print("elif")
else:
print("else")"
> /usr/local/bin/python3 ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/normalizeForInterpreter.py "if False:
print("if")
elif True:
print("elif")
else:
print("else")"
> /usr/local/bin/python3 ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/normalizeForInterpreter.py "if False:
print("if")
elif True:
print("elif")
else:
print("else")"
> /usr/local/bin/python3 ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.106471-dev/pythonFiles/normalizeForInterpreter.py "if False:
print("if")
elif True:
print("elif")
else:
print("else")"

Activity
DougPlumley commentedon Sep 23, 2020
I incorrectly listed my Python extension version, I was on v2020.9.106471-dev, reverting to v2020.8.109390 hasn't resolved the issue however.
1 remaining item
brettcannon commentedon Sep 23, 2020
I can replicate the issue with:
DougPlumley commentedon Sep 23, 2020
I've tried this using different extension versions and can reproduce it on:
2020.8.108011
2020.8.106424
2020.8.105369
2020.8.105045
2020.8.103604
DougPlumley commentedon Sep 23, 2020
I just noticed 2020.9.111407 dropped, I can reproduce this on this version as well.
DougPlumley commentedon Sep 23, 2020
Perhaps this hasn't been working for a long time and I just haven't encountered it, I'm noticing that these two scenarios work.
karrtikr commentedon Sep 25, 2020
I have noticed that I'm not able to repro this all on all Python versions. For eg. it works with Python 3.7.1-64 bit. Investigating further...
karrtikr commentedon Sep 30, 2020
Spike results
We insert blank lines between global statements, for eg. the following selection has two global statements: the if-else block and print.
So we insert an extra character between the two, hence it's run as two different statements

The bug is that starting from somewhere around Python 3.7.5, the following
is being considered as two global statements and so sent separately,
&
so sending the second elif block fails.
The
astlibrary changed, and the older logic we use to detect global statements is no longer working correctly.vscode-python/pythonFiles/normalizeForInterpreter.py
Lines 48 to 57 in a58eeff
We can either modify the logic or how we use
astlibrary to compute global statements, or can use regexes instead.FangXS-github commentedon Oct 6, 2020
Nice. It helps a lot.
luabud commentedon Jan 22, 2021
This should be fixed on our newest release, version 2021.1.502429796, but if you're still seeing this let us now 😊