feat: use ruff to bundle lazy/conditional imports for loaded_modules#1265
Open
paullongtan wants to merge 6 commits into
Open
feat: use ruff to bundle lazy/conditional imports for loaded_modules#1265paullongtan wants to merge 6 commits into
paullongtan wants to merge 6 commits into
Conversation
a41157c to
86d2c1c
Compare
5 tasks
kumare3
reviewed
Jun 30, 2026
| `if TYPE_CHECKING:` blocks, so lazily/conditionally imported local modules are discovered even | ||
| when they were never executed at bundle time. | ||
| """ | ||
| if shutil.which("ruff") is None: |
Contributor
There was a problem hiding this comment.
should we add a debug log here to say that we are unable to find ruff so not using it
Author
There was a problem hiding this comment.
Definitely. I've added a debug message when ruff is not found and falls back to the current runtime sys.modules discovery method.
60663b0 to
48f4dbe
Compare
Signed-off-by: paullongtan <paullongtan@gmail.com>
Signed-off-by: paullongtan <paullongtan@gmail.com>
Signed-off-by: paullongtan <paullongtan@gmail.com>
Signed-off-by: paullongtan <paullongtan@gmail.com>
…tive_dependencies Signed-off-by: paullongtan <paullongtan@gmail.com>
Signed-off-by: paullongtan <paullongtan@gmail.com>
48f4dbe to
137e021
Compare
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.
Issue
With the default copy_style="loaded_modules", the SDK decides which local files to ship by inspecting sys.modules at runtime (list_imported_modules_as_files). Only modules already imported by the time bundling runs are captured, so lazily or conditionally imported local modules get silently dropped from the bundle and surface on the cluster as ModuleNotFoundError. This affects:
Closes flyteorg/flyte#7595.
Summary
Augments the current runtime sys.modules discovery method with a static first-party import graph from ruff analyze graph. (Falls back to the current method if ruff not available or when an error occurs)
ls_files(): the loaded_modules branch walks ruff's import graph starting from the runtime-discovered files, bundling everything reachable through import edges (function-level and TYPE_CHECKING imports included)._build_import_graph(): runsruff analyze graph, parses the JSON, and resolves ruff's cwd-relative paths to absolute. Returns None (with a log) when ruff is not on PATH or the analysis fails._collect_transitive_dependencies(): does a transitivie search over the graph from the seeds (all_files) to collect all dependencies._is_user_file()/_build_invalid_directories()Test
Added two tests:
test_ls_files_loaded_modules_includes_function_level_import(): function-level importstest_ls_files_loaded_modules_includes_type_checking_import(): TYPE_CHECKING importsResult: 23 passed (the 2 new tests plus the existing 21).