Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run selection indentation error #14048

Closed
DougPlumley opened this issue Sep 23, 2020 · 9 comments
Closed

Run selection indentation error #14048

DougPlumley opened this issue Sep 23, 2020 · 9 comments
Labels
area-terminal bug Issue identified by VS Code Team member as probable bug important Issue identified as high-priority regression Bug didn't exist in a previous release

Comments

@DougPlumley
Copy link

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.languageServer setting:

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.

run_selection

Logs

Output for Python in the Output panel (ViewOutput, 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")"

@DougPlumley DougPlumley added triage-needed Needs assignment to the proper sub-team bug Issue identified by VS Code Team member as probable bug labels Sep 23, 2020
@DougPlumley
Copy link
Author

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.

@karthiknadig karthiknadig self-assigned this Sep 23, 2020
@karthiknadig karthiknadig added the important Issue identified as high-priority label Sep 23, 2020
@brettcannon brettcannon added needs PR and removed important Issue identified as high-priority labels Sep 23, 2020
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Sep 23, 2020
@brettcannon brettcannon added area-terminal triage-needed Needs assignment to the proper sub-team regression Bug didn't exist in a previous release and removed triage-needed Needs assignment to the proper sub-team labels Sep 23, 2020
@karthiknadig karthiknadig removed area-terminal regression Bug didn't exist in a previous release labels Sep 23, 2020
@brettcannon brettcannon added area-terminal regression Bug didn't exist in a previous release important Issue identified as high-priority labels Sep 23, 2020
@brettcannon
Copy link
Member

I can replicate the issue with:

if a:
    b
elif c:
    d
else:
    e

@DougPlumley
Copy link
Author

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
Copy link
Author

I just noticed 2020.9.111407 dropped, I can reproduce this on this version as well.

@DougPlumley
Copy link
Author

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.

if True:
    if False:
        print("if")
    elif True:
        print("elif")
    else:
        print("else")
else:
    print("else")
try:
    if False:
        print("if")
    elif True:
        print("elif")
    else:
        print("else")
except Exception as e:
    print(e)

@karrtikr
Copy link

karrtikr commented 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 karrtikr self-assigned this Sep 25, 2020
@karrtikr
Copy link

karrtikr commented 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.

if True:
    print('true')
else:
    print('')

print('')

So we insert an extra character between the two, hence it's run as two different statements
image

The bug is that starting from somewhere around Python 3.7.5, the following

if a:
    b
elif c:
    d

is being considered as two global statements and so sent separately,

if a:
    b

&

elif c:
    d

so sending the second elif block fails.

The ast library changed, and the older logic we use to detect global statements is no longer working correctly.

def _get_global_statement_blocks(source, lines):
"""Return a list of all global statement blocks.
The list comprises of 3-item tuples that contain the starting line number,
ending line number and whether the statement is a single line.
"""
tree = ast.parse(source)
visitor = Visitor(lines)
visitor.visit(tree)

We can either modify the logic or how we use ast library to compute global statements, or can use regexes instead.

@karrtikr karrtikr removed their assignment Sep 30, 2020
@FangXS-github
Copy link

Nice. It helps a lot.

@luabud
Copy link
Member

luabud commented 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 😊

@luabud luabud closed this as completed Jan 22, 2021
@ghost ghost removed the needs PR label Jan 22, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-terminal bug Issue identified by VS Code Team member as probable bug important Issue identified as high-priority regression Bug didn't exist in a previous release
Projects
None yet
Development

No branches or pull requests

6 participants