This page specifies the behavior of the
moonxpackage and script runner.
moonx executes either one standalone .mbtx file or exactly one main package
from the Mooncakes registry without installing it into the user's binary
directory. Other local paths, Git URLs, and wildcard package selectors are out
of scope.
moonx is another entrance to the moon executable, not a separately compiled
binary. The process selects the moonx command-line parser when its invoked
executable name is moonx or moonx.exe.
moonx [OPTIONS] <MBTX|PACKAGE> [PROGRAM_ARGS]...
The supported options are:
--experimental-policy <PATH>
--daemon
-v, --verbose
-h, --help
-V, --version
Everything after the .mbtx path or package coordinate, including
hyphen-prefixed values, is forwarded to the executed program. An explicit --
separator is accepted but is not required. moonx options therefore precede
the input.
The child process inherits the caller's working directory, environment,
standard streams, and signal behavior. moonx returns the child's exit code.
--daemon enables a session IPC execution host for this invocation, for both
standalone scripts and registry packages. Native descendants that invoke moonrun
reuse that host; if a session is already inherited, the invocation joins it.
The session ends with its owning Run: no persistent or detached daemon starts.
The flag overrides process.ipc = false as an execution setting, without changing
selected or inherited permissions. It requires V8 and process.virtualize = false.
The same selection applies to Engine::run_moon artifact handoff. Options after
the input remain program arguments, so moonx script.mbtx --daemon does not
enable a session. See IPC sessions.
The selected policy's process.virtualize = true enables Moonrun's experimental
virtual supervision for the resolved Wasm and recursive moonx children. Guest
spawn currently accepts only moonx; other commands remain unsupported.
Virtualization defaults to false. The existing process.spawn / process.allow
rules authorize guest launches in either mode, including internal moonx. For a
moonx-only tree, use virtualize = true and allow = [{ program = "moonx" }].
Acquisition and building stay in Moon. See
moonrun.
By default, moonx emits no informational output of its own. With --verbose,
registry or script dependency acquisition, build progress, and execution
details are written to stderr. Stdout remains reserved for the executed
program, whose standard streams are inherited unchanged.
When a policy-bearing moonrun directly spawns moonx, the host publishes the
canonical policy representation through an inherited OS handle and replaces a
reserved environment entry with the handle's process-local identifier. At
process entry, moonx takes ownership of the handle, removes the entry from its
ambient environment before registry or script dependency work, and makes the
handle inheritable again only for the final moonrun command. That moonrun
reads and closes the handle before constructing the Run environment. Detached
moonx processes therefore do not depend on the parent Run's lifetime, while
registry and build subprocesses and processes spawned by the resolved program
receive neither the marker nor the handle. No pathname crosses the process
boundary: Unix removes the temporary name before spawning, while Windows keeps
the delete-on-close file exclusively open. The guest therefore has no
filesystem name it can replace or modify.
Policy publication and handle setup run as part of the direct moonx spawn
job. If either fails, the job reports the corresponding OS error and no child
process is created or registered.
An inherited policy and the selected child policy both constrain filesystem,
network, and process operations. An explicit --experimental-policy adds
restrictions; the child may not replace or widen the inherited policy. This
applies to registry packages and standalone .mbtx files. Missing or invalid
selected policies fail execution.
For host-supervised execution, the private prefix
--moonrun-artifact-handoff <directory> selects artifact delivery instead of
launching moonrun. Registry acquisition and standalone builds keep their normal
caches. Handoff carries only Wasm: the helper copies the module and optional
source map/policy, then returns arguments and cwd in a manifest. An explicit
supervisor policy file selects the child policy; inherited restrictions also apply.
See artifact delivery.
Moon and Moonrun share the moonx grammar in moonutil::cli_support. Moonrun
classifies the invocation before artifact preparation. Help/version and parse
errors complete inside the Engine using this embedded frontend, with plain-text
output on the bound stdout/stderr and Clap's exit status. No helper or handoff
directory is created for those responses. Guest spawn still resolves and
authorizes its executable before admission; direct embedding calls need no
helper executable to produce a frontend response.
Only valid execution requests enter artifact preparation, and successful helper exit still requires a delivered artifact. Options after the input remain opaque program arguments. A frontend-position private handoff prefix is rejected by the shared grammar; the host adds its own prefix only when starting preparation.
An input whose extension is .mbtx is treated as a persistent standalone
script path. Moonx reuses the same synthesized-package dependency resolution,
build planning, and incremental target directory as a standalone linear-memory
Wasm build through moon run. Imports declared by .mbtx front matter or the
legacy prelude are resolved and built before the synthesized script package.
Prelude is available implicitly, as in an ordinary package. Other packages
require explicit imports; an absent or empty import block imports no additional
packages.
The resulting Wasm artifact then uses the same moonx execution path as a
registry Wasm artifact.
Standalone .mbtx execution uses the linear-memory Wasm backend.
Program arguments are forwarded to the final
moonrun, and --experimental-policy has the same meaning as for a registry
Wasm artifact.
A script may declare imports and an embedded Moonrun Policy in front matter
between two lines containing at least three hyphens, such as ----. The front
matter uses the moon.pkg grammar, with import { ... } blocks and a single
permission(...) declaration:
----
import {
"moonbitlang/core/env",
}
permission(
env: { "from_host": ["PATH"] },
fs: { "read": ["."] },
)
----
fn main {
// ...
}Moon's header parser accepts an optional shebang, such as #!/usr/bin/env moonx,
on the first line before front matter or a legacy import prelude. It also
accepts a leading UTF-8 BOM and blank lines after the shebang. Without a
shebang, front matter starts at the beginning of the file, optionally after a
BOM and whitespace. The closing delimiter is required; its hyphen count need
not match the opening delimiter. Imports retain versioned paths, named aliases,
and * import-all syntax. Multiple import blocks are
combined in source order. The old import { ... } prelude outside front matter
remains supported, including after a permission header; these imports follow
any front matter imports. Conditional and test-only imports remain unsupported.
Unknown declarations and duplicate permission declarations are errors.
Embedded policies use permission(...) in front matter. The former
// policy: YAML format is no longer interpreted; those lines are ordinary
comments and do not select a policy.
Shebang handling here applies only to Moon's header parsing and Moonrun's policy loading. Moon passes the original script unchanged to compiler tools; accepting a header does not guarantee that the compiler accepts the script. The compiler rejects imports outside front matter.
The .mbtx path itself is passed to Moonrun as the policy source, so relative
filesystem roots are resolved from the script's directory. For moon run -
and moon run -e, Moon writes the source to a temporary .mbtx but retains the
invocation directory as the logical policy source directory. An explicit
--experimental-policy selects the child policy instead of embedded policy.
Inherited restrictions apply in addition to the selected child policy. Front matter syntax is
validated during dependency resolution. Permission fields are decoded only when
the declaration is the effective policy for Wasm execution; overridden,
non-Wasm, and build-only paths do not validate the permission schema. An explicit
policy can therefore override permission fields with an invalid schema.
Inheritance does not skip validation of the selected child policy.
Moonx consumes an inherited policy handle before resolving .mbtx
dependencies and relays it only to the final moonrun process. Registry and
build subprocesses therefore cannot observe the reserved marker or inherit the
handle.
An Executable Package Coordinate selects exactly one main package. A module-only
coordinate selects its root package; a package suffix selects that exact package.
The selected package must be declared as a main package. moonx does not infer a
sole main package elsewhere in a module.
Pinned coordinates accept both existing forms:
user/module/package@1.2.3
user/module@1.2.3/package
An explicit latest request accepts the corresponding forms:
user/module/package@latest
user/module@latest/package
Documentation should prefer the first form. @latest refreshes the registry
index before resolving the newest version and fails if the refresh fails.
Unpinned coordinates resolve the latest version already known to the local
registry index. The index is updated only when the module cannot be resolved
locally.
Pinned coordinates use the supplied version without querying the index for version selection. For unpinned coordinates, Moon retries local resolution after one registry refresh when no cached latest answer is available. Registry read errors remain errors; a missing module and a module with no version metadata produce distinct diagnostics. Package-path existence does not trigger a search for a different module version.
Registry packages run as linear-memory Wasm artifacts:
- Resolve the exact module version.
- Compute the published linear-Wasm asset URL and cache path.
- On a cache miss, download and verify the asset under a per-artifact lock.
- Publish it atomically and execute it with
moonrun.
A missing published Wasm asset is an error. Version 1 does not fall back to downloading source and building Wasm locally.
The registry client owns asset URLs, checksum verification, and publication
under $MOON_HOME/registry/cache/assets. Once a version is resolved, an artifact
cache hit does not read registry metadata, including when
MOON_REGISTRY_CACHE=off or after moon clean --registry-cache.
Private prebuilt downloads are not implemented. In HTTP mode, a cache miss checks the selected release's visibility and rejects private releases. Existing cached artifacts come from public downloads, and the artifact cache is not partitioned by visibility. The Git backend does not update its index on an asset cache miss.
Cargo continues to build only the moon binary. Distributors create a second
filesystem entry named moonx containing the same executable bytes, using a
hard link where practical and a copy otherwise. Updating the external MoonBit
installer is required follow-up work outside this repository.