reflex-dev / reflex

🕸️ Web apps in pure Python 🐍
https://reflex.dev
Apache License 2.0
20.45k stars 1.18k forks source link

Reflex run failed then remove my whole code #3747

Open leehuwuj opened 3 months ago

leehuwuj commented 3 months ago

Describe the bug I ran into this issue twice and it looks a serious bug to me which reflex remove almost my whole code folder. Here is my project structure:

app/
  api/
  services/
  ui/ => reflex code here
  app.py
rxconfig.py

After ran reflex run it created a .web folder in the project root path. But it instantly got error then remove everything in the app folder.

Rxconfig code:

# rxconfig.py
import reflex as rx

config = rx.Config(
    app_name="app",
)

Here is the log:

❯ poetry run reflex run
The currently activated Python version 3.10.9 is not supported by the project (^3.11,<3.12).
Trying to find and use a compatible version. 
Using python3.11 (3.11.4)
────────────────────────────────────────── Initializing app ───────────────────────────────────────────
[15:38:57] Prior to reflex 0.4.0, rx.* components are based on Chakra UI. They are now   console.py:104
           based on Radix UI. To stick to Chakra UI, use rx.chakra.*.                                  
                                                                                         console.py:104
           Run `reflex script keep-chakra` to automatically update your app.             console.py:104
                                                                                         console.py:104
           For more details, please see                                                  console.py:104
           https://reflex.dev/blog/2024-02-16-reflex-v0.4.0/                                           
           Initializing the web directory.                                               console.py:104
Success: Initialized app
───────────────────────────────────────── Starting Reflex App ─────────────────────────────────────────
[15:39:04] Compiling: ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100% 13/13 0:00:00
INFO:httpx:HTTP Request: POST https://app.posthog.com/capture/ "HTTP/1.1 200 OK"
───────────────────────────────────────────── App Running ─────────────────────────────────────────────
App running at: http://localhost:3000
Backend running at: http://0.0.0.0:8000
Exception in thread Thread-5:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/threading.py", line 1038, in _bootstrap_inner
    self.run()
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/watchdog/observers/api.py", line 223, in run
    self.dispatch_events(self.event_queue)
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/watchdog/observers/api.py", line 402, in dispatch_events
    handler.dispatch(event)
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/watchdog/events.py", line 224, in dispatch
    {
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/reflex/utils/watch.py", line 64, in on_modified
    shutil.rmtree(dest_path)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/shutil.py", line 732, in rmtree
    _rmtree_safe_fd(fd, path, onerror)
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/shutil.py", line 666, in _rmtree_safe_fd
    onerror(os.rmdir, fullname, sys.exc_info())
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/shutil.py", line 664, in _rmtree_safe_fd
    os.rmdir(entry.name, dir_fd=topfd)
OSError: [Errno 66] Directory not empty: '.web'
INFO:watchfiles.main:13366 changes detected
Task exception was never retrieved
future: <Task finished name='Task-2' coro=<send.<locals>.async_send() done, defined at /Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/reflex/utils/telemetry.py:188> exception=FileNotFoundError(2, 'No such file or directory')>
Traceback (most recent call last):
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/reflex/utils/telemetry.py", line 189, in async_send
    return _send(event, telemetry_enabled, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/reflex/utils/telemetry.py", line 173, in _send
    event_data = _prepare_event(event, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/reflex/utils/telemetry.py", line 114, in _prepare_event
    project_hash = get_project_hash(raise_on_fail=_raise_on_missing_project_hash())
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/reflex/utils/prerequisites.py", line 538, in get_project_hash
    data = json.loads(json_file.read_text())
                      ^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 1058, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 1044, in open
    return io.open(self, mode, buffering, encoding, errors, newline)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '.web/reflex.json'
Task exception was never retrieved
future: <Task finished name='Task-3' coro=<send.<locals>.async_send() done, defined at /Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/reflex/utils/telemetry.py:188> exception=FileNotFoundError(2, 'No such file or directory')>
Traceback (most recent call last):
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/reflex/utils/telemetry.py", line 189, in async_send
    return _send(event, telemetry_enabled, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/reflex/utils/telemetry.py", line 173, in _send
    event_data = _prepare_event(event, **kwargs)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/reflex/utils/telemetry.py", line 114, in _prepare_event
    project_hash = get_project_hash(raise_on_fail=_raise_on_missing_project_hash())
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/reflex/utils/prerequisites.py", line 538, in get_project_hash
    data = json.loads(json_file.read_text())
                      ^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 1058, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/pathlib.py", line 1044, in open
    return io.open(self, mode, buffering, encoding, errors, newline)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: '.web/reflex.json'
Process SpawnProcess-2:
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/process.py", line 314, in _bootstrap
    self.run()
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/multiprocessing/process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/uvicorn/_subprocess.py", line 76, in subprocess_started
    target(sockets=sockets)
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/uvicorn/server.py", line 61, in run
    return asyncio.run(self.serve(sockets=sockets))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 190, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "uvloop/loop.pyx", line 1517, in uvloop.loop.Loop.run_until_complete
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/uvicorn/server.py", line 68, in serve
    config.load()
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/uvicorn/config.py", line 467, in load
    self.loaded_app = import_from_string(self.app)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/uvicorn/importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 940, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/reflex/app_module_for_backend.py", line 16, in <module>
    app_module = get_app(reload=False)
                 ^^^^^^^^^^^^^^^^^^^^^
  File "/Users/huu/Library/Caches/pypoetry/virtualenvs/app-IS96j_NT-py3.11/lib/python3.11/site-packages/reflex/utils/prerequisites.py", line 256, in get_app
    raise RuntimeError(
RuntimeError: Cannot get the app module because `app_name` is not set in rxconfig! If this error occurs in a reflex test case, ensure that `get_app` is mocked.
^C[15:47:05] Reflex app stopped.                                                console.py:104

To Reproduce Steps to reproduce the behavior:

Expected behavior A clear and concise description of what you expected to happen.

Screenshots If applicable, add screenshots to help explain your problem.

Specifics (please complete the following information):

Additional context Add any other context about the problem here.

leehuwuj commented 3 months ago

Additional note: Fortunately, my project has initialized Git so i just need to revert the deleted changes. Then i reran reflex run again and it worked nicely.

masenf commented 3 months ago

Is there anything unusual in your app, like changing reflex.constants values? or threads modifying the assets folder?

I'm trying to figure out how to reproduce this issue, which appears to be related to the assets folder monitoring. If you know what I can do to trigger this, please let me know, would like to fix it.

leehuwuj commented 3 months ago

@masenf thank for the response. It seems like it just happened randomly, and nothing affected the constant values or changing the assets either. But if i disabled telemetry then the issue doesn't come up anymore.

import reflex as rx

config = rx.Config(
    app_name="app",
    telemetry_enabled=False,
)

I'll share the code with you later so you can try to reproduce the issue. In the meantime, I hope you can spot a bug with the sign above! Feel free to discuss if you need more information.