Skip to content

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

@alsuren

Description

@alsuren

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions