get_moonhome

    Download zip
    Author
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    17 days ago
    Downloads
    27

    No dependencies

    #get_moonhome

    A minimal MoonBit module demonstrating how to run build.mbtx with the module-level --moonbit-unstable-prebuild option. It uses only the standard library and requires no third-party dependencies.

    #Run

    cd /Users/yorkin/source/moonbit/bin_deps_test/get_moonhome moon build moon run .

    moon.mod sets the default target to native. Running the module prints the script's diagnostic message followed by the main program's output, for example:

    [prebuild] MOON_HOME=/Users/yorkin/.moon get_moonhome: main executed

    Moon may also print build progress and notices about experimental scripts.

    #Configuration and script

    The key configuration is in moon.mod:

    options(
    "preferred-target": "native",
    "--moonbit-unstable-prebuild": "build.mbtx",
    )

    The script path is relative to the module root. Moon automatically compiles the .mbtx file to linear-memory Wasm and executes it with moonrun. No separate moon run command is needed in the configuration.

    build.mbtx reads the inherited environment variable with @env.get_env_var("MOON_HOME"). If it is unset, the script falls back to HOME/.moon, or USERPROFILE/.moon when HOME is unavailable. This fallback is implemented by the example script; Moon does not automatically inject a default MOON_HOME. Set the environment variable explicitly for a nonstandard location.

    The script's stdout must contain exactly one build configuration JSON value, for example:

    {"vars":{"MOON_HOME":"/Users/yorkin/.moon"}}

    This example prints the path to stderr with eprintln and returns JSON on stdout with println(output.stringify()). The returned variable can be referenced as ${build.MOON_HOME} in build configuration. It does not automatically become an environment variable in the main program, which only demonstrates the normal build and run flow in this example.

    You can also run the script directly to inspect the returned JSON:

    moon run --target wasm build.mbtx

    #When the script runs

    • This is an experimental module-level configuration script option. Its full name is --moonbit-unstable-prebuild.
    • native and llvm builds execute it; wasm, wasm-gc, and js builds skip it.
    • moon check skips this module's configuration script. Use moon build or moon run . to verify the hook.
    • Native/LLVM builds with --dry-run also execute the script to obtain the build configuration.
    • Package-level pre-build or rule + dev_build entries in moon.pkg use a separate code generation mechanism.

    See Moon's prebuild documentation for implementation details.

    Source Files

    Powered by MoonBit

    Site sourceReport issueModulesSkillsCLIBuild queueStatistics

    Ā© 2026 mooncakes.io