Skip to content

fix(loader): actionable error for module-name Python plugin refs - #1435

Merged
robcohen merged 5 commits into
mainfrom
fix/1432-module-plugin-error
Jun 19, 2026
Merged

robcohen merged 5 commits into
mainfrom
fix/1432-module-plugin-error

Conversation

@robcohen

Copy link
Copy Markdown
Member

Implements Gap 1 of #1432: a Python plugin referenced by module name (beancount's plugin "pkg.mod") now produces an actionable error pointing at the file-path requirement, instead of a misleading "module not found".

Problem

Module-name references are unsupported by design — only file paths load (discover_module_source). But a name like beancount.plugins.auto_accounts contains a dot, so it routes into the Python-plugin branch, spins up the runtime, and fails inside execute_module, surfacing as:

E8002: Python plugin 'beancount.plugins.auto_accounts' execution failed:
       Python plugin module not found: beancount.plugins.auto_accounts

Doubly confusing — "execution failed" and "module not found" — so users (reasonably) suspect a venv/PYTHONPATH problem rather than an unsupported declaration form. The existing suggest_module_path() + E8004 hint was unreachable for real module names, because they always contain a dot and get diverted into the Python branch before reaching it.

Fix

In the Python-plugin error path, detect the bare-module-name case (ext != "py" and no path separator) and emit an actionable E8004:

  • Resolvable (host Python finds it): Python plugin "pkg.mod" is not supported by module name; reference the file directly: plugin "/abs/path/pkg/mod.py"
  • Unresolvable: a generic variant that still names the file-path requirement and notes the sandbox can't see the host venv, so the plugin must be self-contained (stdlib + beancount compat shim).

This also unifies the error code with the parallel unknown-plugin branch (both E8004).

Tests

  • New test_python_module_plugin_error_is_actionable (gated on python-plugins): uses an unresolvable module name for a deterministic message; asserts it contains "not supported by module name" + "reference the file directly" and does not contain "execution failed".
  • Strengthened the existing test_python_module_plugin_reports_error.

Verification

cargo fmt --check clean · cargo clippy --features python-plugins -- -D warnings clean · full loader_test suite 64/64 pass.

Scope

This is Gap 1 only. Gaps 2-3 (document the file-path + self-contained-sandbox model in docs/reference/plugins.md and docs/migration/from-beancount.md) remain open under #1432.

A Python plugin declared by module name (beancount's `plugin "pkg.mod"`) is
unsupported by design — only file-path references load (see
`discover_module_source`). It routes into the Python-plugin branch (the name
contains a dot), runs, and fails inside `execute_module`, surfacing as
`E8002: Python plugin 'pkg.mod' execution failed: Python plugin module not
found: pkg.mod` — doubly misleading, reading as a venv/PYTHONPATH problem the
user cannot fix.

Detect the bare-module-name case in the error path and emit an actionable
`E8004` instead: name the unsupported form and point at the file path,
resolving it via system Python (`suggest_module_path`) when possible, else a
generic hint that also notes the sandbox cannot see the host venv (the plugin
must be self-contained). This also unifies the code with the parallel
unknown-plugin branch (both `E8004`).

The existing `suggest_module_path`/`E8004` hint was unreachable for real
module names because they always contain a dot and were diverted into the
Python branch first.

Closes #1432 (Gap 1).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@robcohen
robcohen requested a review from a team as a code owner June 19, 2026 10:53
Copilot AI review requested due to automatic review settings June 19, 2026 10:53
@codecov

codecov Bot commented Jun 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.75000% with 4 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
crates/rustledger-loader/src/process.rs 93.75% 4 Missing ⚠️

📢 Thoughts on this report? Let us know!

Copilot AI left a comment

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.

Pull request overview

Improves rustledger’s loader UX for Python plugins referenced by module name (plugin "pkg.mod"), which are unsupported by design, by emitting a clearer, actionable diagnostic instead of surfacing a misleading runtime “execution failed / module not found” error.

Changes:

  • Updates Python plugin failure handling to detect bare module-name references and emit an actionable E8004 suggesting file-path usage (optionally resolving a path via system Python).
  • Keeps the existing E8002 behavior for genuine Python plugin execution failures (non-module refs).
  • Adds a regression test ensuring module-name refs produce an actionable message and do not contain the confusing “execution failed” wrapper.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
crates/rustledger-loader/src/process.rs Adds module-name detection in the Python-plugin error path and emits E8004 with a file-path-based remediation message.
crates/rustledger-loader/tests/loader_test.rs Adds a regression test asserting the new actionable error message for unsupported module-name plugin references.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread crates/rustledger-loader/src/process.rs Resolved
@github-actions

github-actions Bot commented Jun 19, 2026 •

Copy link
Copy Markdown
Contributor

📊 Benchmark Results

Tool Time Memory
rustledger 33ms ± 0.4ms 24.5 MB
beancount 887.3ms ± 10.1ms 50.6 MB
Comparison 26.9x faster 2.1x less

vs main branch (2026-06-19)

Metric main this PR Change
Time 34.2ms 33ms -3.5% ✅
10K transactions · 5 runs, 2 warmup · hyperfine
What do these numbers mean?
  • Time: Wall-clock time to parse and validate the ledger (± std dev)
  • Memory: Peak resident set size (RSS)
  • vs main: Compared to latest nightly benchmark on main branch

Gaps 2-3 of #1432. Document that:

- Built-in names resolve to the native Rust plugin regardless of the
  `beancount.plugins.` prefix (so `plugin "beancount.plugins.auto_accounts"`
  is unchanged), but a custom Python plugin must be referenced by file path —
  the module-name form is rejected.
- Python plugins run in a CPython-WASI sandbox that never mounts the host
  venv / site-packages, so they must be self-contained (stdlib + the bundled
  beancount compat shim); anything heavier needs a native Rust plugin.

Adds a "Referencing a Python Plugin" + "Plugin Sandbox" section to
docs/reference/plugins.md and a file-path note to docs/migration/from-beancount.md
(clarifying that the existing auto_accounts example works because it resolves
to a native plugin, not Python).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@github-actions

github-actions Bot commented Jun 19, 2026 •

Copy link
Copy Markdown
Contributor

✅ Compatibility Test Results

Metric Result
Check Compatibility 100%
BQL Compatibility 100%
Full AST Match 100%
Total Files 792
What's tested
  • Check: Exit codes and error counts match bean-check
  • BQL: Query results match bean-query
  • Files: ~800 test files downloaded from 10+ beancount repositories

Full results available in workflow artifacts

robcohen and others added 3 commits June 19, 2026 11:43
Follow-up to the review on #1435. Instead of running the plugin and
relabeling the failure by name-shape, detect a bare module-name reference
*before* invoking the runtime:

- Correctness (#1): the old error-path relabel discarded the real error `e`
  for any dotted/non-.py name — masking genuine failures such as a corrupt
  runtime download (`PythonRuntime::new()` fails before module discovery). Now
  module names are rejected up front, so the `Err(e)` arm only fires for real
  plugin files and surfaces `e` verbatim (E8002).
- Altitude (#2): no runtime is downloaded/compiled (and no "10-100x slower"
  warning printed) for a request that is rejectable from the name alone.
- Reuse/consistency (#3): both E8004 sites now share `module_ref_message`,
  so the "not supported by module name; reference the file directly" guidance
  is identical everywhere.
- Discriminator (#4): one `is_python_module_name` helper using
  `eq_ignore_ascii_case`, not the pre-lowercased `ext` (fixes a latent
  case-sensitivity coupling).
- Docs (#5): document E8002/E8004/E8005 in docs/reference/errors.md.
- Cleanup (#6,#7): static example path (drops the dead `unwrap_or` and the
  `./.py` trailing-dot edge); drop the redundant `is_python_available()` guard
  (`suggest_module_path` already handles a missing interpreter).

fmt + clippy (-D warnings, python-plugins) clean; loader suite 64/64.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
…ests

Addresses the Copilot review note: is_python_module_name checked only
std::path::MAIN_SEPARATOR (`\` on Windows), so a forward-slash file ref like
"plugins/foo" could be misclassified as a module name there. Check both `/`
and the platform separator.

Adds direct unit tests for is_python_module_name (module vs .py vs
path-separated vs existing-file, case-insensitive ext) and module_ref_message
(both the resolved-path and fallback branches — also covering the line codecov
flagged).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@robcohen
robcohen merged commit 38f9610 into main Jun 19, 2026
42 checks passed
@robcohen
robcohen deleted the fix/1432-module-plugin-error branch June 19, 2026 12:17
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.

2 participants