fix: validate WebUI dist before reuse#8785
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/test_main.py" line_range="161-162" />
<code_context>
"""Tests that dashboard is not downloaded when it exists and version matches."""
# Mock os.path.exists to return True
monkeypatch.setattr(os.path, "exists", lambda x: True)
+ monkeypatch.setattr(os.path, "isfile", lambda x: True)
+ monkeypatch.setattr(os.path, "isdir", lambda x: True)
# Mock get_dashboard_version to return the current version
</code_context>
<issue_to_address>
**suggestion (testing):** Avoid globally returning True for os.path.isfile/isdir in these tests to better reflect real filesystem state
In `test_check_dashboard_files_exists_and_version_match` and `test_check_dashboard_files_exists_but_version_mismatch`, `os.path.isfile`/`os.path.isdir` are patched to always return `True`, which can hide issues if more path checks are added later. Instead, either:
1. Build a minimal `dist` structure under `tmp_path` (as in `test_check_dashboard_files_uses_bundled_dist_when_data_dist_is_stale`) and avoid patching `isfile`/`isdir`, or
2. Patch them to return `True` only for the specific `data/dist/index.html`, `data/dist/assets`, and `data/dist/assets/version` paths, delegating to the real implementation otherwise.
This will keep the tests aligned with the actual filesystem checks and reduce false positives as the implementation evolves.
Suggested implementation:
```python
orig_isfile = os.path.isfile
def _test_isfile(path):
path_str = os.fspath(path)
if path_str.endswith(os.path.join("data", "dist", "index.html")):
return True
if path_str.endswith(os.path.join("data", "dist", "assets", "version")):
return True
return orig_isfile(path)
monkeypatch.setattr(os.path, "isfile", _test_isfile)
```
```python
orig_isdir = os.path.isdir
def _test_isdir(path):
path_str = os.fspath(path)
if path_str.endswith(os.path.join("data", "dist", "assets")):
return True
return orig_isdir(path)
monkeypatch.setattr(os.path, "isdir", _test_isdir)
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
| monkeypatch.setattr(os.path, "isfile", lambda x: True) | ||
| monkeypatch.setattr(os.path, "isdir", lambda x: True) |
There was a problem hiding this comment.
suggestion (testing): Avoid globally returning True for os.path.isfile/isdir in these tests to better reflect real filesystem state
In test_check_dashboard_files_exists_and_version_match and test_check_dashboard_files_exists_but_version_mismatch, os.path.isfile/os.path.isdir are patched to always return True, which can hide issues if more path checks are added later. Instead, either:
- Build a minimal
diststructure undertmp_path(as intest_check_dashboard_files_uses_bundled_dist_when_data_dist_is_stale) and avoid patchingisfile/isdir, or - Patch them to return
Trueonly for the specificdata/dist/index.html,data/dist/assets, anddata/dist/assets/versionpaths, delegating to the real implementation otherwise.
This will keep the tests aligned with the actual filesystem checks and reduce false positives as the implementation evolves.
Suggested implementation:
orig_isfile = os.path.isfile
def _test_isfile(path):
path_str = os.fspath(path)
if path_str.endswith(os.path.join("data", "dist", "index.html")):
return True
if path_str.endswith(os.path.join("data", "dist", "assets", "version")):
return True
return orig_isfile(path)
monkeypatch.setattr(os.path, "isfile", _test_isfile) orig_isdir = os.path.isdir
def _test_isdir(path):
path_str = os.fspath(path)
if path_str.endswith(os.path.join("data", "dist", "assets")):
return True
return orig_isdir(path)
monkeypatch.setattr(os.path, "isdir", _test_isdir)There was a problem hiding this comment.
Code Review
This pull request updates the WebUI directory check in main.py to ensure that the data/dist directory is complete (containing index.html, assets, and version) before using it, falling back to downloading a fresh copy if any files are missing. Corresponding tests in tests/test_main.py have been updated and a new test has been added. Feedback suggests cleaning up the incomplete data/dist path before downloading to prevent extraction conflicts, and restricting the global mocks of os.path.isfile and os.path.isdir in tests to target only the distribution paths to avoid side effects.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| logger.warning( | ||
| "WebUI directory is incomplete: %s. Downloading a fresh copy.", | ||
| data_dist_path, | ||
| ) |
There was a problem hiding this comment.
If the WebUI directory is incomplete or corrupted, simply logging a warning and proceeding to download might lead to extraction conflicts (e.g., if data/dist is actually a file instead of a directory, or if there are leftover orphaned files from a previous failed installation). Cleaning up the incomplete path before downloading ensures a clean and reliable extraction state.
| logger.warning( | |
| "WebUI directory is incomplete: %s. Downloading a fresh copy.", | |
| data_dist_path, | |
| ) | |
| logger.warning( | |
| "WebUI directory is incomplete: %s. Downloading a fresh copy.", | |
| data_dist_path, | |
| ) | |
| import shutil | |
| if os.path.isdir(data_dist_path): | |
| shutil.rmtree(data_dist_path, ignore_errors=True) | |
| else: | |
| try: | |
| os.remove(data_dist_path) | |
| except Exception: | |
| pass |
| monkeypatch.setattr(os.path, "isfile", lambda x: True) | ||
| monkeypatch.setattr(os.path, "isdir", lambda x: True) |
There was a problem hiding this comment.
Globally mocking os.path.isfile and os.path.isdir to always return True can cause unexpected side effects in other parts of the test suite or standard library calls. It is safer to restrict the mock to return True only for paths related to the dashboard distribution.
| monkeypatch.setattr(os.path, "isfile", lambda x: True) | |
| monkeypatch.setattr(os.path, "isdir", lambda x: True) | |
| monkeypatch.setattr(os.path, "isfile", lambda x: "dist" in str(x)) | |
| monkeypatch.setattr(os.path, "isdir", lambda x: "dist" in str(x)) |
| monkeypatch.setattr(os.path, "isfile", lambda x: True) | ||
| monkeypatch.setattr(os.path, "isdir", lambda x: True) |
There was a problem hiding this comment.
Globally mocking os.path.isfile and os.path.isdir to always return True can cause unexpected side effects in other parts of the test suite or standard library calls. It is safer to restrict the mock to return True only for paths related to the dashboard distribution.
| monkeypatch.setattr(os.path, "isfile", lambda x: True) | |
| monkeypatch.setattr(os.path, "isdir", lambda x: True) | |
| monkeypatch.setattr(os.path, "isfile", lambda x: "dist" in str(x)) | |
| monkeypatch.setattr(os.path, "isdir", lambda x: "dist" in str(x)) |
|
你好,依据你的pr,确实能够覆盖到index.html、assets/、assets/version缺失的三个问题,但是我想确认一下,是否存在以下情况: |
|
The failing Unit Tests job is hitting the same FastAPI 0.137 route-introspection compatibility issue that #8783 fixes:
This PR's local targeted checks are still clean ( |
zouyonghe
left a comment
There was a problem hiding this comment.
Reviewed. The fix is scoped to rejecting incomplete cached directories before reuse, while preserving explicit behavior. Tests cover the partial-dist case and stale bundled-dist path. The current full pytest failure matches the unrelated FastAPI 0.137 route-test issue fixed by #8783.
zouyonghe
left a comment
There was a problem hiding this comment.
Reviewed. The fix is scoped to rejecting incomplete cached data/dist directories before reuse, while preserving explicit webui_dir behavior. Tests cover the partial-dist case and stale bundled-dist path. The current full pytest failure matches the unrelated FastAPI 0.137 route-test issue fixed by #8783.
Fixes #8784.
Summary
data/dist/index.html,data/dist/assets, anddata/dist/assets/versionbefore reusing the cached WebUI directorywebui_dirbehavior unchangedTo verify
python -m py_compile main.py tests\test_main.pypython -m ruff check main.py tests\test_main.pypython -m ruff format --check main.py tests\test_main.pypython -m pytest tests\test_main.py -q -p no:cacheprovidergit diff --checkSummary by Sourcery
Ensure the WebUI cache is only reused when the local dist directory is complete and valid, otherwise trigger a fresh download.
Bug Fixes:
Tests: