question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Extract variable/method" consistently throws "Error in refactoring:<New line> Canceled" in Output despite actually succeeding unless variable/method name is left at default

See original GitHub issue

Environment data

  • VS Code version: 1.31.0
  • Extension version (available under the Extensions sidebar): 2019.1.0
  • OS and version: Microsoft Windows [Version 10.0.17134.523]
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.7.2
  • Type of virtual environment used (N/A | venv | virtualenv | conda | …): N/A
  • Relevant/affected Python packages and their versions: rope 0.11.0 installed from pip

Note

This issue applied to both “Extract variable” and “Extract method” feature of this extension. Therefore, variables or methods will be referred to as symbols throughout the description

Expected behaviour

No errors should be seen in outputs after extracting a symbol and choosing an appropriate name for the symbol. (since the extraction process actually succeeded)

Actual behaviour

Despite the refactoring being done correctly, an error message is seen in the Outputs tab by selecting “Python” from the drop down menu in that tab:

##########Refactor Output##########
Error in refactoring:
Canceled

after the naming process (VSCode asking you for a name for the new symbol) is complete.

More annoyingly, due to the implementation of an error notification feature (See #3268), an error notification will appear on the corner saying the same thing as in the output:

Cannot perform refactoring using selected element(s). (Canceled)

which is how I’m notified of this issue

Steps to reproduce:

  1. Create a new Python file in a folder using VSCode
  2. Type something that can be correctly “extracted”. For example, type
print(1 + 1)
  • Note: make sure there are no errors (red squiggly lines) in the Python file. To be sure, you can install and use pylint or any other linter to check
  1. Select the code that can be extracted to a symbol. In the example above, it is 1 + 1
  2. Right click (Ctrl-click on macOS) and click “Extract variable” or “Extract method” in the menu, either one will reproduce the issue
  3. Choose a different and new name for the symbol
  4. The refactoring should be performed correctly (since there are no syntax errors in this file), yet a misleading failure notification will pop up (See Actual Behavior)

Logs

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

##########Refactor Output##########
Error in refactoring:
Canceled

That’s the error I described in Actual Behavior. I believed that the error come from the language server that do the actual refactoring after the extraction rather than rope itself because that message only appears after choosing a name for the symbol, and doesn’t appear at all (neither does the error notification) if you accept the default name, which almost nobody will do.

Output from Console under the Developer Tools panel (toggle Developer Tools on under Help; turn on source maps to make any tracebacks be useful by running Enable source map support for extension debugging)

Cannot perform refactoring using selected element(s). (Canceled)
t.onDidNotificationChange @ notificationsAlerts.ts:40

That’s the only line that I think is useful

Edit: The outputs seems misleading as it implies that the refactoring “fails”, but just to clarify that the refactoring is actually done correctly (with the appropriate symbol extracted) I posted this issue solely because the error notification is too annoying.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:8 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
d3r3kkcommented, Feb 21, 2019

This is indeed a reproduceable bug, and I have trapped it in the debugger. Note that I reproduced this using Python 3.7 and rope==0.11.0 and rope==0.12.0.

See file src\client\refactor\proxy.ts (called from src\client\providers\simpleRefactorProvider.ts) on line 124. At first glance, the handleStdError method seems to be expecting JSON values (line 136) but we are receiving text lines instead.

My Repro

  1. Create a workspace with a single Python file in it, and copy this code into that file:
import sys

def go_show_stuff():
    print(f"This system is {sys.executable}")

def go_do_stuff():
    print("hello")
    m = 1 + 1  # <-- Refactor this statement, and extract to method.
    print(f"good-bye: {m}")

if __name__ == "__main__":
    go_do_stuff()
  1. Ensure your workspace has Python 3.7 accessible, with Rope installed.
  2. Highlight the 1 + 1 expression and right-click on the highlighted block, choose 'Extract Method`.

Expected results

A method is created and contains return 1 + 1.

Observed results

The command is issued on line 105 of src\client\refactor\proxy.ts:

"{"lookup":"extract_method","file":"d:\\dev\\4341_refactor_raising_exceptions\\test_file.py","start":"128","end":"133","id":"1","name":"newmethod869","indent_size":4}"

But results in stderr string on line 124 of src\client\refactor\proxy.ts this.handleStdError(output.out);:

"d:\dev\4341_refactor_raising_exceptions\.venv\lib\site-packages\rope\refactor\patchedast.py:85: RuntimeWarning: Unknown node type <JoinedStr>; please report!
  % node.__class__.__name__, RuntimeWarning)
"

Which ultimately raises the error:

message:"Unexpected token d in JSON at position 0"
stack:"SyntaxError: Unexpected token d in JSON at position 0\n\tat JSON.parse (<anonymous>)\n\tat handleStdError.dataStr.split.filter.map.resp (D:\dev\vscode-python\out\client\refactor\proxy.js:137:102)\n\tat Array.map (<anonymous>)\n\tat RefactorProxy.handleStdError (D:\dev\vscode-python\out\client\refactor\proxy.js:137:85)\n\tat SafeSubscriber.initialize.result.out.subscribe.output [as _next] (D:\dev\vscode-python\out\client\refactor\proxy.js:125:26)\n\tat SafeSubscriber.__tryOrUnsub (D:\dev\vscode-python\node_modules\rxjs\Subscriber.js:242:16)\n\tat SafeSubscriber.next (D:\dev\vscode-python\node_modules\rxjs\Subscriber.js:189:22)\n\tat Subscriber._next (D:\dev\vscode-python\node_modules\rxjs\Subscriber.js:129:26)\n\tat Subscriber.next (D:\dev\vscode-python\node_modules\rxjs\Subscriber.js:93:18)\n\tat sendOutput (D:\dev\vscode-python\out\client\common\process\proc.js:61:32)\n\tat Socket.execObservable.Observable_1.Observable...
0reactions
brettcannoncommented, Sep 21, 2021

This works in Jedi behind LSP and rope is being removed in our 2021.10 release.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Set Nvidia Video as the Default with Computers ... - Dell
The following steps can be used to always use the Nvidia video card. Open the "Nvidia Control Panel." Click "3D Settings" > "Manage...
Read more >
java - Why such output is coming? - Stack Overflow
Your function f() throws InterruptedException , which is caught by the first catch block (hence it prints 1), but this catch block cannot ......
Read more >
Nvidia: Fix "Audio not plugged in" error | Simple fixes | Full Guide
Want to use your monitor's audio or just audio output from your Nvidia graphics card, but can't get it to work properly in...
Read more >
An example of how suppressed exceptions in Java work
The sequence of events that triggers a suppressed exception is as follows: An AutoCloseable object is initialized in a try with resources ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found