Skip to content

Run selection indentation error #14048

Closed
Closed

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.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 (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

  1. DougPlumley commented on Sep 23, 2020

    @DougPlumley
    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.

  2. ghost removed
    triage-neededNeeds assignment to the proper sub-team
    on Sep 23, 2020
  3. added
    triage-neededNeeds assignment to the proper sub-team
    regressionBug didn't exist in a previous release
    and removed
    triage-neededNeeds assignment to the proper sub-team
    on Sep 23, 2020
  4. 1 remaining item

  5. brettcannon commented on Sep 23, 2020

    @brettcannon
    Member

    I can replicate the issue with:

    if a:
        b
    elif c:
        d
    else:
        e
  6. DougPlumley commented on Sep 23, 2020

    @DougPlumley
    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

  7. DougPlumley commented on Sep 23, 2020

    @DougPlumley
    Author

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

  8. DougPlumley commented on Sep 23, 2020

    @DougPlumley
    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)
    
  9. karrtikr commented on Sep 25, 2020

    @karrtikr

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

  10. karrtikr commented on Sep 30, 2020

    @karrtikr

    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.

  11. removed their assignment
    on Sep 30, 2020
  12. FangXS-github commented on Oct 6, 2020

    @FangXS-github

    Nice. It helps a lot.

  13. luabud commented on Jan 22, 2021

    @luabud
    Member

    This should be fixed on our newest release, version 2021.1.502429796, but if you're still seeing this let us now 😊

  14. locked as resolved and limited conversation to collaborators on Oct 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-terminalbugIssue identified by VS Code Team member as probable bugimportantIssue identified as high-priorityregressionBug didn't exist in a previous release

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions