8000 functions-framework --debug exits if you save with a syntax error · Issue #213 · GoogleCloudPlatform/functions-framework-python · GitHub
[go: up one dir, main page]

Skip to content

functions-framework --debug exits if you save with a syntax error #213

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

Closed
alsuren opened this issue Dec 21, 2022 · 0 comments · Fixed by #214
Closed

functions-framework --debug exits if you save with a syntax error #213

alsuren opened this issue Dec 21, 2022 · 0 comments · Fixed by #214
Labels
enhancement New feature or request P3

Comments

@alsuren
Copy link
Contributor
alsuren commented Dec 21, 2022

Steps to reproduce:

  • Start things up normally
functions-framework \
    --source=cloud_function/main.py \
    --target=hello_get \
    --host=localhost \
    --port=18000 \
    --debug
This starts up as expected: ``` * Serving Flask app 'hello_get' (lazy loading) * Environment: production WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Debug mode: on WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://localhost:18000 Press CTRL+C to quit * Restarting with watchdog (fsevents) * Debugger is active! * Debugger PIN: 104-682-644 ```

Make a change that introduces a syntax error, and you get:

 * Detected change in '/Users/david/src/opvia/app/scripts_v3/cloud_function/main.py', reloading
 * Restarting with watchdog (fsevents)
Traceback (most recent call last):
  File "/Users/david/src/opvia/app/.venv/bin/functions-framework", line 8, in <module>
    sys.exit(_cli())
  File "/Users/david/src/opvia/app/.venv/lib/python3.10/site-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/Users/david/src/opvia/app/.venv/lib/python3.10/site-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/Users/david/src/opvia/app/.venv/lib/python3.10/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/david/src/opvia/app/.venv/lib/python3.10/site-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/Users/david/src/opvia/app/.venv/lib/python3.10/site-packages/functions_framework/_cli.py", line 37, in _cli
    app = create_app(target, source, signature_type)
  File "/Users/david/src/opvia/app/.venv/lib/python3.10/site-packages/functions_framework/__init__.py", line 289, in create_app
    spec.loader.exec_module(source_module)
  File "<frozen importlib._bootstrap_external>", line 879, in exec_module
  File "<frozen importlib._bootstrap_external>", line 1017, in get_code
  File "<frozen importlib._bootstrap_external>", line 947, in source_to_code
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/david/src/opvia/app/scripts_v3/cloud_function/main.py", line 11
    ///
    ^^
SyntaxError: invalid syntax

and the process exits

Potential solution:

Apparently this used to be an issue in Flask, but isn't anymore (see https://stackoverflow.com/a/68445450 ). I did some digging to work out how they fixed it, and I think I have a solution:

I think that functions-framework basically needs to do something like this:

https://github.com/pallets/flask/pull/4718/files#diff-fed8939d1905b99bba605ad91e9ccdf5ede6223e03ccbc3f0121853035051e62R935-R950

in functions_framework/init.py", line 289, in create_app where it does:

    with _app.app_context():
        spec.loader.exec_module(source_module)
  • catch the error
  • check werkzeug.serving.is_running_from_reloader().
    • if true, print the error and create a dummy app that explodes when called
    • else re-raise the error
alsuren pushed a commit to alsuren/functions-framework-python that referenced this issue Dec 21, 2022
@josephlewis42 josephlewis42 added enhancement New feature or request P3 labels Mar 3, 2023
alsuren pushed a commit to alsuren/functions-framework-python that referenced this issue Sep 6, 2023
HKWinterhalter pushed a commit that referenced this issue Sep 6, 2023
* fix: don't exit on reload if there is a syntax error (#213)

* style: reformat using black

* test: robustness to syntax error in debug mode startup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request P3
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants
0