Skip to content

Bug: runtime_sync() uses Path(__file__) from inside mobuild package instead of caller's notebook #4

Description

@fr1ll

Summary

When mobuild.runtime_sync() runs in a notebook that is outside of mobuild, it attempts to _write_file where the file parameter (i.e. input file) resolves to the path of mobuild/init.py rather than to the current notebook.

This causes a crash because mobuild's ._app is a Typer app rather than a Marimo notebook.

Reproduction

Open in molab

Expected behavior

The notebook's exported cells (those marked with ## EXPORT) are written to src/package_name.

Actual behavior

File ".../mobuild/__init__.py", line 132, in runtime_sync
    _write_file(Path(__file__), output_path)
File ".../mobuild/__init__.py", line 75, in _write_file
    order = internal.execution_order
File ".../marimo/_ast/app.py", line 971, in execution_order
    self._app._maybe_initialize()
AttributeError: 'Typer' object has no attribute '_maybe_initialize'

Root cause

In mobuild/__init__.py:132, Path(__file__) resolves to mobuild's own __init__.py (because __file__ in any function refers to the module that defines the function, not the caller). _write_file then:

  1. Imports mobuild/__init__.py as a "notebook".
  2. Grabs its module-level app — which is typer.Typer(no_args_is_help=True) from line 12.
  3. Wraps it in InternalApp and accesses .execution_order, which calls _maybe_initialize() on the Typer object → AttributeError.

Suggested fix

runtime_sync needs to discover the caller's file, e.g. via inspect.stack()[1].filename or by accepting the notebook path as an argument, then pass that to _write_file.

Attribution

Diagnosis and reproducible example by Claude Opus 4.7 with minimal edits to the explanation by me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions