microsoft/ptvsd

Flask tests fail with "no such option: --wait" on windows py27

karthiknadig opened this issue · 6 comments

Environment data

  • PTVSD version: master
  • OS and version: windows
  • Python version (& distribution if applicable, e.g. Anaconda): 2.7
  • Using VS Code or Visual Studio:

Actual behavior

@01.013754: ptvsd#5692 ERR Usage: python -m flask [OPTIONS] COMMAND [ARGS]...

@01.013810: ptvsd#5692 ERR Try "python -m flask --help" for help.

@01.013822: ptvsd#5692 ERR 

@01.013971: ptvsd#5692 ERR Error: no such option: --wait

Expected behavior

Should not fail with that error message

Steps to reproduce:

run test_flask.py on Windows 2.7

This is the command line used:

@00.000951: Spawning ['d:\\a\\1\\s\\.tox\\py27\\scripts\\python.exe', 'd:\\a\\1\\s\\.tox\\py27\\lib\\site-packages\\ptvsd', '--wait', '--host', 'localhost', '--port', '5692', '-m', 'flask', 'run', '--no-debugger', '--no-reload', '--with-threads', '--port', '5014']

@fabioz this is a new issue with the tests that i am seeing. Do you suspect this is because of the new flask release or something in our argument parsing is now broken.

Figured out this issue. Looks like flask made some changes that now causes it to get the arguments via CommandLineToArgvW.

Previously (flask==v1.0.3) they were passing the argv to cli.main:

https://github.com/pallets/flask/blob/1.0.3/flask/cli.py#L889-L906

This is what it looks like now (flask==v1.1.1):

https://github.com/pallets/flask/blob/1.1.1/src/flask/cli.py#L965-L966

This change means that in click, it takes the path of get_os_args:

https://github.com/pallets/click/blob/7.0/click/core.py#L700-L704

Which eventually calls into here. The issue is that sys.argv is already rewritten by the time _initial_argv_hash is set when click._winconsole is loaded.

https://github.com/pallets/click/blob/7.0/click/_winconsole.py#L259-L280

This hash here will be same as when the flask reaches this point in click during initialization.

https://github.com/pallets/click/blob/7.0/click/utils.py#L346-L348

Looking at the comment there:

We can only extract the unicode argv if sys.argv has not been changed since the startup of the application.

Basically click should initialize _initial_argv_hash using original sys.argv. In the case of the debugger, this will always be equal to value returned by _hash_py_argv. Since click is loaded after the argv is re-written. This is going to break ptvsd, and pydevd.

/cc @Anapo14 @DonJayamanne

Workaround for debuggers:
One solution I see is if we can figure out debugger was launched for flask, we should pre-load click. I guess presence of FLASK_APP env var or -m flask argument could be used here as a work around.

This means that, we would be loading click when user may not have intended it to be loaded. Say for example user had defined FLASK_APP in the environment but was running some other script.

The fix for this will be in flask==1.1.2

@karthiknadig Maybe we should use an older version of flask in the ci in the meanwhile?