<#1265 feat: use ruff to bundle lazy/conditional i...
# flyte-github
a
#1265 feat: use ruff to bundle lazy/conditional imports for loaded_modules Pull request opened by paullongtan 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: • Function-level imports: from helper import compute inside a task body never runs at packaging time, so helper is never in sys.modules. • if TYPE_CHECKING: imports: never executed at runtime, but needed when annotations are evaluated on the cluster. 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) • Update `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). • Add `_build_import_graph()`: runs
ruff 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. • Add `_collect_transitive_dependencies()`: does a transitivie search over the graph from the seeds (
all_files
) to collect all dependencies. • Extracted the user-file filter into
_is_user_file()
/
_build_invalid_directories()
Test Added two tests: • `test_ls_files_loaded_modules_includes_function_level_import()`: function-level imports • `test_ls_files_loaded_modules_includes_type_checking_import()`: TYPE_CHECKING imports
Copy code
uv run pytest tests/flyte/code_bundle/test_code_bundle.py -q
Result: 23 passed (the 2 new tests plus the existing 21). flyteorg/flyte-sdk