Skip to content

fix: use Code.ensure_compiled/1 for compile-time module checks - #8

Closed
Munksgaard wants to merge 1 commit into
lostbean:mainfrom
Munksgaard:fix/ensure-compiled-for-compile-time-checks
Closed

Munksgaard wants to merge 1 commit into
lostbean:mainfrom
Munksgaard:fix/ensure-compiled-for-compile-time-checks

Conversation

@Munksgaard

@Munksgaard Munksgaard commented Apr 7, 2026 •

Copy link
Copy Markdown
Contributor

Code.ensure_loaded?/1 only checks if a module is already loaded in the BEAM VM. When called at compile time (e.g. in module body expressions or via use Jido.Composer.Workflow), it returns false for modules that exist in the project but have not been compiled yet, causing spurious "not a valid Jido.Action module" errors.

For example, the following triggers the bug because MapNode.new/1 runs at compile time and ExtractDocumentAction may not have been compiled yet:

defmodule MyApp.ExtractionPipeline do
  {:ok, extract_node} =
    Jido.Composer.Node.MapNode.new(
      name: :extract,
      over: :documents,
      node: MyApp.ExtractDocumentAction
    )

use Jido.Composer.Workflow,
    name: "extraction_pipeline",
    nodes: %{extract: extract_node, merge: MyApp.MergeAction},
    transitions: %{{:extract, :ok} => :merge, {:merge, :ok} => :done},
    initial: :extract
end

Replace with Code.ensure_compiled/1 which triggers compilation of the target module if needed. Only changes call sites that can run at compile time; ensure_loaded? calls for optional runtime deps (AgentServer, OpenTelemetry) are left unchanged.

Code.ensure_loaded?/1 only checks if a module is already loaded in the
BEAM VM. When called at compile time (e.g. in module body expressions or
via use Jido.Composer.Workflow), it returns false for modules that exist
in the project but have not been compiled yet, causing spurious "not a
valid Jido.Action module" errors.

For example, the following triggers the bug because MapNode.new/1 runs
at compile time and ExtractDocumentAction may not have been compiled yet:

```elixir
defmodule MyApp.ExtractionPipeline do
  {:ok, extract_node} =
    Jido.Composer.Node.MapNode.new(
      name: :extract,
      over: :documents,
      node: MyApp.ExtractDocumentAction
    )

  use Jido.Composer.Workflow,
    name: "extraction_pipeline",
    nodes: %{extract: extract_node, merge: MyApp.MergeAction},
    transitions: %{{:extract, :ok} => :merge, {:merge, :ok} => :done},
    initial: :extract
end
```

Replace with Code.ensure_compiled/1 which triggers compilation of the
target module if needed. Only changes call sites that can run at compile
time; ensure_loaded? calls for optional runtime deps (AgentServer,
OpenTelemetry) are left unchanged.
@Munksgaard
Munksgaard force-pushed the fix/ensure-compiled-for-compile-time-checks branch from f42321b to 9b952b3 Compare April 7, 2026 08:57
@Munksgaard

Copy link
Copy Markdown
Contributor Author

Hm, this doesn't seem to work the way I expected. Sorry for the noise.

@Munksgaard Munksgaard closed this Apr 7, 2026
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.

1 participant