Conversation
Copilot
AI
changed the title
[WIP] read this isuue: @ariga/atlas-provider-sqlalchemy/issues/16
and try to fix it
Fix module caching issue causing intermittent empty output (issue #16)
Aug 20, 2025
Co-authored-by: ronenlu <63970571+ronenlu@users.noreply.github.com>
947431b to
9d0b395
Compare
ronenlu
approved these changes
Aug 20, 2025
|
This fix seems to work 👍 |
noamcattan
reviewed
Aug 20, 2025
noamcattan
reviewed
Aug 20, 2025
Co-authored-by: Noam Cattan <62568565+noamcattan@users.noreply.github.com>
noamcattan
approved these changes
Aug 20, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #16 - resolves the intermittent issue where
atlas-provider-sqlalchemywould sometimes output nothing, particularly after git branch switches.Problem
The issue was caused by Python's module caching mechanism in the
get_metadata()function. When importing SQLAlchemy model files usingimportlib.util.spec_from_file_location(), the function used non-unique module names (justfile_path.stem), which could lead to stale cached modules being returned instead of fresh imports of changed files.This manifested as:
PYTHONDONTWRITEBYTECODE=1fixing the issue (confirming bytecode caching was the culprit)Solution
The fix implements unique module naming and cache invalidation:
Unique module names: Instead of using
file_path.stem(e.g., "models"), now usesf"atlas_dynamic_module_{abs(hash(str(file_path.absolute())))}_{file_mtime}"to ensure each import gets a unique module name based on file path and modification time.Cache invalidation: Explicitly clears stale modules from
sys.modulesand callsimportlib.invalidate_caches()to force fresh loading.File change detection: Includes file modification time in the module name to detect when files have changed (e.g., after git branch switches).
Testing
Added comprehensive test suite (
tests/test_caching_fix.py) that validates:All existing tests continue to pass, ensuring no regression in functionality.
Example
Before the fix, this scenario could fail:
After the fix, both runs consistently produce the correct output.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.