Support auto-rerun when config.toml is created#13625
Conversation
Watch parent .streamlit directories for config file creation when the file doesn't exist at startup. This allows Streamlit to automatically detect and apply newly created config files without requiring a server restart. Fixes #11296 Co-Authored-By: Claude <[email protected]>
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
✅ PR preview is ready!
|
config.toml is created
|
@cursor review |
SummaryThis PR adds support for auto-rerun when Changed files:
Code QualityThe implementation is clean, minimal, and follows existing patterns in the codebase: Strengths:
Minor Issue: In def exists_side_effect(path):
# Simulate: first config file exists, second doesn't
return "home" in path or path.endswith("0")This unused function should be removed to keep the test clean. Test CoverageThe test coverage is adequate with two new test cases:
The tests follow best practices:
Backwards CompatibilityThis change is fully backwards compatible:
Security & RiskLow risk:
Edge case consideration: Recommendations
VerdictAPPROVED: This is a well-implemented feature that addresses issue #11296 with minimal, clean code changes. The implementation correctly uses existing infrastructure, includes adequate test coverage, and poses no backwards compatibility or security concerns. The only minor issue is a small amount of dead code in the test file that can be addressed as a follow-up if desired. This is an automated AI review. Please verify the feedback and use your judgment. |
There was a problem hiding this comment.
Pull request overview
This pull request adds support for automatic app rerun when a config.toml file is created while Streamlit is running. Previously, creating a config file after server startup required a manual restart.
Changes:
- Modified
_install_config_watchers()to watch parent.streamlitdirectories when config files don't exist - Added
watch_dirimport to bootstrap.py - Added comprehensive unit tests for both scenarios: when config files don't exist and mixed scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| lib/streamlit/web/bootstrap.py | Added logic to watch parent directories with glob pattern when config files don't exist at startup |
| lib/tests/streamlit/web/bootstrap_test.py | Added two unit tests to verify directory watching for non-existent config files and mixed scenarios |
📉 Python coverage change detectedThe Python unit test coverage has decreased by 0.0007%
✅ Coverage change is within normal range. Coverage by files
|
Co-Authored-By: Claude <[email protected]>
lib/streamlit/web/bootstrap.py
Outdated
| else: | ||
| # Watch parent directory for file creation | ||
| parent_dir = os.path.dirname(filename) | ||
| watch_dir( |
There was a problem hiding this comment.
question: Will there be any unintended side-effects of this change? If I'm reading this correct, changing secrets.toml will now also trigger a re-run in this conditional branch, but not if config.toml already exists. That seems like unexpected behavior to me.
There was a problem hiding this comment.
The file watcher will receive events for all file changes in the directory, but I believe the glob_pattern="config.toml" here ensures that the callback itself is only triggered when it is touching the config.toml.
There was a problem hiding this comment.
I did a quick deep dive with Claude. And there seems to be a bug with the event-based file watcher (watchdog) not correctly respecting this parameter, while the poll watcher works fine. I will add a fix for this and do a manual verification
There was a problem hiding this comment.
Thanks for confirming and the quick follow-up fixes!
When watching a directory with a glob_pattern, file events inside the directory now correctly use the watched directory path for MD5 calculation instead of the individual file path. This ensures the glob_pattern filtering is applied correctly. Previously, changes to any file in a watched directory (e.g., secrets.toml) could trigger callbacks even when only specific files (e.g., config.toml) should be watched. Co-Authored-By: Claude <[email protected]>
SummaryThis PR adds directory-based config watchers so Code QualityChanges are localized and follow existing patterns; the Test CoverageUnit tests cover the new directory glob filtering and file-creation detection behavior in the event-based watcher, plus the new config watcher logic ( Backwards CompatibilityNo public API changes; behavior is internal to watcher plumbing. The callback path for directory watches now reflects the watched directory rather than the specific file event, but current internal uses don’t depend on the exact path. Security & RiskLow risk. The change is scoped to config file watching and watcher MD5 calculation logic, with tests in place. Recommendations
VerdictAPPROVED: Solid, scoped change with good unit coverage and minimal risk. This is an automated AI review using |
lib/streamlit/web/bootstrap.py
Outdated
| else: | ||
| # Watch parent directory for file creation | ||
| parent_dir = os.path.dirname(filename) | ||
| watch_dir( |
There was a problem hiding this comment.
Thanks for confirming and the quick follow-up fixes!
📈 Frontend coverage change detectedThe frontend unit test (vitest) coverage has increased by 0.0500%
🎉 Great job on improving test coverage! |
SummaryThis PR adds Code Quality
Test CoverageUnit tests were added for event-based and polling watchers and the bootstrap config watcher wiring. However, there is no test asserting that file content changes inside watched directories still trigger callbacks when Backwards CompatibilityThe callback argument change for directory watches and the loss of file-content change detection in watched directories can alter behavior in existing integrations (component watchers, folder watch list). This is a backwards compatibility risk and should be addressed before merge. Security & RiskNo direct security issues identified. The main risk is regressions in file watching that can prevent reruns or reloads when files are edited, or trigger reloads for ignored directories. Recommendations
VerdictCHANGES REQUESTED: The current watcher behavior can miss file content changes in watched directories and changes callback semantics, both of which risk regressions. This is an automated AI review using |
SummaryThis PR updates config file watching to detect newly created Code QualityThe changes are small, well-scoped, and follow existing watcher patterns. Docstrings now clarify file vs directory watcher behavior and callback semantics. No structural concerns in the Python code. Test CoveragePython unit tests were added to cover the new Backwards CompatibilityBackwards compatible. Security & RiskLow risk. The watcher changes are localized and do not affect security-sensitive paths. There is a minor operational edge case: if the parent Recommendations
VerdictAPPROVED: Solid, low-risk improvement with good unit test coverage; only minor follow-up recommendations. This is an automated AI review using |
|
I decided to do a bit more tweaking and enforce usage of the |
Describe your changes
When Streamlit starts without a
config.tomlfile, creating one now triggers an automatic app rerun to apply the configuration. This addresses the issue where newly created config files were not recognized without a manual server restart.The implementation watches parent
.streamlitdirectories for file creation usingwatch_dir()with a glob pattern when the config file doesn't exist at startup. The existing callback mechanism reloads configuration upon file creation.GitHub Issue Link
Fixes #11296
Testing Plan
test_install_config_watcher_for_nonexistent_files: Verifies directory watching when config files don't existtest_install_config_watcher_mixed_existing_and_nonexistent: Tests mixed scenarios with existing and non-existing filesContribution License Agreement
By submitting this pull request you agree that all contributions to this project are made under the Apache 2.0 license.