Skip to content

feat: use ruff to bundle lazy/conditional imports for loaded_modules#1265

Open
paullongtan wants to merge 6 commits into
flyteorg:mainfrom
paullongtan:feat/ruff-analyze-graph-code-bundle
Open

feat: use ruff to bundle lazy/conditional imports for loaded_modules#1265
paullongtan wants to merge 6 commits into
flyteorg:mainfrom
paullongtan:feat/ruff-analyze-graph-code-bundle

Conversation

@paullongtan

@paullongtan paullongtan commented Jun 27, 2026

Copy link
Copy Markdown

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
uv run pytest tests/flyte/code_bundle/test_code_bundle.py -q

Result: 23 passed (the 2 new tests plus the existing 21).

`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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a debug log here to say that we are unable to find ruff so not using it

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely. I've added a debug message when ruff is not found and falls back to the current runtime sys.modules discovery method.

@paullongtan paullongtan force-pushed the feat/ruff-analyze-graph-code-bundle branch from 60663b0 to 48f4dbe Compare June 30, 2026 01:52
@paullongtan paullongtan requested a review from kumare3 June 30, 2026 01:57
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>
@paullongtan paullongtan force-pushed the feat/ruff-analyze-graph-code-bundle branch from 48f4dbe to 137e021 Compare June 30, 2026 02:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: use ruff analyze graph to detect files for the code bundle

2 participants