Skip to content

Run Python File in Terminal fails with & / ampersand in file path #8432

Closed as not planned

Description

The Command "Run Python File in Terminal" does not escape special chars in the file path and execution fails with an error. In my case the file path contains an ampersand and the errors states that you can not use the &-char in string literals:
2019-11-07_14h59_54

Environment data

  • VS Code version: 1.39.2
  • Extension version (available under the Extensions sidebar): 2019.10.44104
  • OS and version: WIndows 10 10.0.18362
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.9.0 32-bit
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): N/A
  • Relevant/affected Python packages and their versions: XXX
  • Jedi or Language Server? (i.e. what is "python.jediEnabled" set to; more info How to update the language server to the latest stable version #3977): XXX

Expected behaviour

The command should enclose the file path with double quotes.
2019-11-07_15h08_42

This way the script executes correctly.

Thanks

Activity

  1. added
    triage-neededNeeds assignment to the proper sub-team
    bugIssue identified by VS Code Team member as probable bug
    on Nov 7, 2019
  2. ghost removed
    triage-neededNeeds assignment to the proper sub-team
    on Nov 7, 2019
  3. removed their assignment
    on Nov 7, 2019
  4. karrtikr commented on Nov 7, 2019

    @karrtikr

    I am able to confirm this, thanks.

  5. christine-e-smit commented on May 21, 2020

    @christine-e-smit

    I am having this issue as well on Mac. When I run debug a script, I see:

    sh-3.2$  env /NOT_A_REAL_PATH/python3/envs/vscode/bin/python /NOT_A_REAL_PATH/.vscode/extensions/ms-python.python-2020.5.80290/pythonFiles/lib/python/debugpy/no_wheels/debugpy/launcher 53006 -- /NOT_A_REALPATH/bar.py https://some.url/?foo=1&bar=2
    

    the &bar=2 doesn't come through to my script.

    This issue appears to be exactly the same as microsoft/vscode#66738, although that one involved a node script. This makes me wonder if the issue is actually with the debugger rather than with the python extension.

  6. lisandromc commented on May 28, 2020

    @lisandromc

    This fails while using integrated terminal ("console": "integratedTerminal" ) or external terminal ("console": "externalTerminal" ). At launch.json:

    "args": [
      "--uri", "https://example.com?a=X&b=Y"
    ],
    "console": "integratedTerminal" 
    

    Console output:

    $  env /home/lisandro/example/venv/bin/python3.7 /home/lisandro/.vscode/extensions/ms-python.python-2020.5.80290/pythonFiles/lib/python/debugpy/wheels/debugpy/launcher 37491 -- -m main --uri https://example.com?a=X&b=Y
    

    and the b parameter never reaches the code.

    This happens because, in BASH, the & sign has a special meaning. The actual problem is that there seems to be no way to escape it. This should work:

    "args": [
      "--uri", "'https://example.com?a=X&b=Y'"
    ],
    

    but it doesn't, because the single quotes ' become \':

    $  env /home/lisandro/example/venv/bin/python3.7 /home/lisandro/.vscode/extensions/ms-python.python-2020.5.80290/pythonFiles/lib/python/debugpy/wheels/debugpy/launcher 42983 -- -m main --uri \'https://example.com?a=X&b=Y\'
    

    and so quoting makes no difference, other than further screwing the URL argument into "'https://example.com?a=X"

    As I understand it, the extension should not escape single quotes in the arguments.

    Note that this issue doesn't happen while using the internal console:

    "args": [
      "--uri", "https://example.com?a=X&b=Y"
    ],
    "console": "internalConsole" 
    

    The problem with internal console is that now we lose all debugging output, which more or less defeats the purpose.

    Version: 1.44.2
    Commit: ff915844119ce9485abfe8aa9076ec76b5300ddd
    Date: 2020-04-16T17:50:03.709Z
    Electron: 7.1.11
    Chrome: 78.0.3904.130
    Node.js: 12.8.1
    V8: 7.8.279.23-electron.0
    OS: Linux x64 5.3.0-46-generic

  7. alaa-ismail commented on Jun 3, 2020

    @alaa-ismail

    I'm having the same problem as well. There is no way to escape & so that bash doesn't misinterpret it. Adding a single quote ' gets converted to \' by the debugger. Also tried using \" instead of single quote but that doesn't work either.

    Version: 1.45.1
    Commit: 5763d909d5f12fe19f215cbfdd29a91c0fa9208a
    Date: 2020-05-14T08:27:22.494Z
    Electron: 7.2.4
    Chrome: 78.0.3904.130
    Node.js: 12.8.1
    V8: 7.8.279.23-electron.0
    OS: Linux x64 5.6.15-1-MANJARO

  8. fireattack commented on Aug 22, 2020

    @fireattack

    Just want to mention, in addition to Linux and Mac, this issue happens in Windows (with CMD) as well.

    I think the cause is pretty straightforward:

    When the args (or file path) have spaces, VS Code will automatically quote them, so there is no ambiguity:

    cmd /C "[...] \launcher 4538 -- f:\sync\code\python\_gists\_test.py "space in between" "
    

    So for ampersands, VS Code should do the same, since otherwise it would be interpreted as something else by most of terminals as mentioned above, but it doesn't:

    cmd /C "[...] \launcher 4591 -- f:\sync\code\python\_gists\_test.py ampersand&in&between "
    
  9. fireattack commented on Jan 2, 2021

    @fireattack

    Any chance to have someone looking into this?

    It looks to me that this isn't hard to fix (quote it when there are ampersands in args string(s), just like what we already do for space), and the impact of the bug is relatively large: you can't debug anything with ampersand in args.

    With filenames, I can somewhat get away by renaming the files myself, but I can't really avoid them in args for things like URLs with query strings.

    Thanks!

  10. Shrirang97 commented on May 9, 2021

    @Shrirang97

    Facing the same issue. I am beginner. Let me check if I can debug this issue.

  11. karrtikr commented on Dec 19, 2022

    @karrtikr

    Fixed by #18855.

  12. locked as resolved and limited conversation to collaborators on Jan 19, 2023
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 bugneeds PRReady to be worked on

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions