Skip to content

feat: support conditional imports in moon.pkg - #2220

Draft
Yu-zh wants to merge 5 commits into
mainfrom
Yu-zh/conditional-import
Draft

Yu-zh wants to merge 5 commits into
mainfrom
Yu-zh/conditional-import

Conversation

@Yu-zh

@Yu-zh Yu-zh commented Sep 16, 2026

Copy link
Copy Markdown
Contributor
  • Related issues: None
  • PR kind: Feature

Summary

Adds backend-conditional imports to moon.pkg for regular, blackbox-test, and whitebox-test imports. Conditions support target = "...", all(...), any(...), not(...), true, and false, with clearer diagnostics for misplaced #cfg attributes and unsupported conditions.

Package discovery happens before backend selection. Dependency resolution produces complete PackageRelations for each requested backend, which is shared by planning, lowering, metadata, package-graph export, and virtual-package handling. For example, moon check --target native ignores broken JS-only imports, while moon check --target js,native fails if either requested backend cannot resolve. Run planner tests use the production package and backend selection path.

Builds on the merged parser, normalization, direct DSL-to-MoonPkg conversion, and discovery/resolution split. Uses the merged DiscoveredProject and ResolvedProject naming, with coverage configuration retained by discovery. Repeated import blocks combine in declaration order. Repeated packages within one import kind remain valid and warn only when their conditions overlap. Legacy import options override the corresponding blocks before warnings; aliases and import-all settings retain their existing behavior. Legacy moon.pkg.json imports remain unconditional.

Includes package-model, dependency-graph, planner, and CLI coverage, plus English and Chinese documentation.

Validation

Rebased onto main at e46d2ed38 (including merged #2241, #2242, and #2243). The final update from 86c63700e changes only moonrun; git range-diff confirms all five PR patches are unchanged.

Validation before the final runtime-only base update:

  • cargo test --locked -p moonutil -p moonbuild-rupes-recta — 407 tests passed; one existing doctest ignored.
  • cargo test --locked -p moon --bin moon --test mod -- tests::planner filter::tests watch::prebuild_output::tests conditional_imports major_version_modules_coexist_in_projects_and_scripts test_wbtest_coverage — 59 unit tests and 9 CLI tests passed, including Wasm execution for versioned imports in projects and scripts, plus coverage execution.
  • cargo clippy --workspace --exclude moonrun --locked --all-targets --all-features -- -D warnings
  • cargo fmt --all -- --check
  • git diff --check
  • Generated-C test planning passed with MOONBIT_NEW_NATIVE=0 moon test app --target native --dry-run on the conditional-import fixture.

After the final base update, reran cargo test --locked -p moon --test mod -- major_version_modules_coexist_in_projects_and_scripts test_wbtest_coverage: both integration tests passed, covering ordinary project/script Wasm execution and coverage. Formatting and diff checks also passed again.

Native/C checks used dry-runs only; the full workspace test suite was not rerun.

Actual Wasm execution of the conditional-import fixture was attempted, but the installed compiler (moonc v0.10.14+7d59c7ec9, 2026-09-18) rejects #cfg in moon.pkg. Compiling and formatting conditional manifests still requires compiler/formatter support for that syntax. Ordinary project and script Wasm execution passed as described above.

Metadata

  • Tests added/updated for bug fixes or new features
  • Compatible with Windows/Linux/macOS (focused macOS validation; cross-platform validation is left to CI)

@semanticdiff-com

semanticdiff-com Bot commented Sep 16, 2026

Copy link
Copy Markdown

Review changes with  SemanticDiff

Changed Files
File Status
  crates/moonbuild-rupes-recta/src/metadata.rs  95% smaller
  crates/moon/src/filter.rs  92% smaller
  crates/moon/src/watch/prebuild_output.rs  89% smaller
  crates/moon/src/cli/cram.rs  69% smaller
  crates/moonbuild-rupes-recta/src/build_lower/mod.rs  59% smaller
  crates/moonbuild-rupes-recta/src/build_plan/builders.rs  56% smaller
  crates/moonbuild-rupes-recta/src/compile/mod.rs  56% smaller
  crates/moonbuild-rupes-recta/src/intent.rs  52% smaller
  crates/moon/src/cli/tree.rs  50% smaller
  crates/moon/src/cli/bundle.rs  39% smaller
  crates/moon/src/cli/doc.rs  35% smaller
  crates/moon/src/rr_build/mod.rs  33% smaller
  crates/moonbuild-rupes-recta/src/resolve/mod.rs  31% smaller
  crates/moon/src/cli/info.rs  29% smaller
  crates/moonbuild-rupes-recta/src/lib.rs  23% smaller
  crates/moon/src/cli/run.rs  23% smaller
  crates/moon/src/cli/prove.rs  22% smaller
  crates/moon/src/cli/tool/build_binary_dep.rs  20% smaller
  crates/moonbuild-rupes-recta/src/pkg_solve/solve.rs  20% smaller
  crates/moon/src/tests/planner/fixture.rs  6% smaller
  crates/moonutil/src/package.rs  5% smaller
  crates/moonutil/src/moon_pkg/parser.rs  4% smaller
  crates/moonbuild-rupes-recta/src/pkg_solve/mod.rs  2% smaller
  crates/moon/src/cli/bench.rs  0% smaller
  crates/moon/src/cli/build.rs  0% smaller
  crates/moon/src/cli/check.rs  0% smaller
  crates/moon/src/cli/install_binary.rs  0% smaller
  crates/moon/src/cli/test.rs  0% smaller
  crates/moon/src/tests/planner/target_backend_planning.rs  0% smaller
  crates/moon/tests/test_cases/conditional_imports/fixture/app/app_test.mbt Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/fixture/app/app_wbtest.mbt Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/fixture/app/lib.mbt Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/fixture/app/moon.pkg Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/fixture/main/main.mbt Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/fixture/main/moon.pkg Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/fixture/moon.mod Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/fixture/native/lib.mbt Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/fixture/native/moon.pkg Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/fixture/native_test/lib.mbt Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/fixture/native_test/moon.pkg Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/fixture/portable/lib.mbt Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/fixture/portable/moon.pkg Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/fixture/portable_test/lib.mbt Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/fixture/portable_test/moon.pkg Unsupported file format
  crates/moon/tests/test_cases/conditional_imports/mod.rs  0% smaller
  crates/moon/tests/test_cases/mod.rs  0% smaller
  crates/moon/tests/test_cases/run_conditional_imports.in/app/main.mbt Unsupported file format
  crates/moon/tests/test_cases/run_conditional_imports.in/app/moon.mod Unsupported file format
  crates/moon/tests/test_cases/run_conditional_imports.in/app/moon.pkg Unsupported file format
  crates/moon/tests/test_cases/run_conditional_imports.in/js_preferred/main.mbt Unsupported file format
  crates/moon/tests/test_cases/run_conditional_imports.in/js_preferred/moon.mod Unsupported file format
  crates/moon/tests/test_cases/run_conditional_imports.in/js_preferred/moon.pkg Unsupported file format
  crates/moon/tests/test_cases/run_conditional_imports.in/moon.work Unsupported file format
  crates/moon/tests/test_cases/target_backend/mod.rs  0% smaller
  crates/moonbuild-rupes-recta/src/build_lower/context.rs  0% smaller
  crates/moonbuild-rupes-recta/src/build_plan/constructor.rs  0% smaller
  crates/moonbuild-rupes-recta/src/build_plan/mod.rs  0% smaller
  crates/moonbuild-rupes-recta/src/discover/synth.rs  0% smaller
  crates/moonbuild-rupes-recta/src/mbtx.rs  0% smaller
  crates/moonbuild-rupes-recta/src/special_cases.rs  0% smaller
  crates/moonbuild-rupes-recta/tests/conditional_imports.rs  0% smaller
  crates/moonutil/tests/expect_moon_pkg.rs  0% smaller
  docs/dev/reference/arch.md Unsupported file format
  docs/dev/reference/cond-comp.md Unsupported file format
  docs/dev/reference/modules-packages.md Unsupported file format
  docs/dev/reference/supported-targets.md Unsupported file format
  docs/dev/reference/testing-strategy.md Unsupported file format
  docs/manual-zh/src/SUMMARY.md Unsupported file format
  docs/manual-zh/src/conditional-imports.md Unsupported file format
  docs/manual/src/SUMMARY.md Unsupported file format
  docs/manual/src/conditional-imports.md Unsupported file format

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 16, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-16T10:31:17.511175Z 70d9348 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@Yu-zh
Yu-zh force-pushed the Yu-zh/conditional-import branch from 70d9348 to 387d7e3 Compare September 16, 2026 10:27

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 70d9348bde

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/moonutil/src/package.rs Outdated

@peter-jerry-ye peter-jerry-ye 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.

The declaration/resolution split and backend-specific dependency graphs are justified. Requesting changes for three P2 issues: duplicate active imports must be rejected before graph insertion, import conversion needs one authoritative normalization path, and planner tests must exercise the production backend-selection policy. There is also a non-blocking naming cleanup noted inline.

The import rule should be explicit: multiple blocks may be active, but each imported package may appear only once per backend within each import kind, regardless of alias. Disjoint conditions and different import kinds remain valid.

Comment thread crates/moonutil/src/package.rs Outdated
Comment thread crates/moonutil/src/package.rs Outdated
Comment thread crates/moon/src/cli/run.rs Outdated
Comment thread crates/moon/src/rr_build/mod.rs Outdated
@Yu-zh
Yu-zh force-pushed the Yu-zh/conditional-import branch from 387d7e3 to 9b9b745 Compare September 17, 2026 09:06
@Yu-zh
Yu-zh force-pushed the Yu-zh/conditional-import branch from 9b9b745 to 9d07c31 Compare September 17, 2026 10:15
@Yu-zh
Yu-zh changed the base branch from main to Yu-zh/conditional-import-parser September 17, 2026 10:15
@peter-jerry-ye
peter-jerry-ye force-pushed the Yu-zh/conditional-import-parser branch 2 times, most recently from 7e192a7 to 01240da Compare September 18, 2026 02:19
Base automatically changed from Yu-zh/conditional-import-parser to main September 18, 2026 02:31
@Yu-zh
Yu-zh force-pushed the Yu-zh/conditional-import branch from 9d07c31 to 1858849 Compare September 18, 2026 03:13
@Yu-zh
Yu-zh marked this pull request as draft September 18, 2026 03:34
@Yu-zh
Yu-zh force-pushed the Yu-zh/conditional-import branch from 1858849 to eca993f Compare September 21, 2026 05:37
@Yu-zh
Yu-zh force-pushed the Yu-zh/conditional-import branch from eca993f to fbd539d Compare September 21, 2026 08:43
@Yu-zh
Yu-zh force-pushed the Yu-zh/conditional-import branch from fbd539d to 27db5b9 Compare September 21, 2026 09:40

This branch has not been deployed

No deployments
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