Repository navigation
web: Link only the components of an app's UI set that the app imports - #4066
Conversation
`#[native_module]` also goes on an inline module: each `pub fn` in it is a part of the set. The describe program records each part's exports, and the web runtime gets one gate per part (`ui_set_part!`), so `nsh package` keeps the parts the app imports and splits off those of code loaded with `import()`. A set of one function keeps its one gate. The web runtime's library is written once the app is described, and in part mode has no function for the whole set.
A part is now a function marked `#[part]` in `#[native_module]`'s module, of any visibility; other functions are helpers. Parts are registered with `Registry::part`, which records what each adds: its exports, and whether it registers for the set as a whole (a setup, files, tokens, scroll bars, a GPU renderer, a fake or a submodule). The description reads that from the registry, so the part table, its stand-in and `Description::with_parts` are gone. `nsh package` keeps the set-level parts whenever the app imports the set or one of its submodules, an import for effects only included, and an import from a submodule keeps the part that registered it. The web runtime registers a set's parts in the set's order, whichever order their gates run in. A part's `#[cfg]`s are copied onto what is made for it, a raw identifier names its part without `r#`, and two sets or parts whose web gates come out alike are refused, naming both.
…parts part by part A part a side module brings after the app started now registers into the set the runtime made, instead of the set being made again: the core keeps the setups it ran and runs only the new part's. Before the app starts the parts still register in the set's order. A set is described in parts, and gated part by part, only when `#[native_module]` on a module made it (each part has a function of its own) and everything it registers is in a part; any other set, including one whose `#[native_module]` function calls `Registry::part` itself, keeps one gate for the whole set. A part's function is now made on every target, calling the part under its `cfg`s, and of a part's `cfg_attr`s only the `cfg`s are copied. A part's name is ASCII, and `#[part]` in a `cfg_attr` is refused. An import of a name no part of a set exports, which keeps every part, is warned of.
|
💡 Needs a maintainer call on API: this adds public surface right before the 0.9.0 freeze. Reviewed e0c13de (diff only). New public items: |
The set is described on the machine that builds the app, so a part under a `cfg` of the target (`target_arch`, `unix`, ...) was described as one thing and linked as another on the web: it compiled and the part was silently missing or never linked. `#[native_module]` now fails on such a `cfg` on a part, in `all`/`any`/`not` and in a `cfg_attr` too. Features, `test` and `debug_assertions` stay allowed.
An import by name of a name the set lacks fails the build, so the warning for a name no part exports is reached by a member read off `import * as` or by a re-export, which the bundle imports by name. The warning now names that, and its test reads the names from such code.
|
💡 The two new commits tighten the macro: a UI-set part under a target |
…nsh` generates uses They are the protocol between `nsh` and the runtime of the same version, so they are `#[doc(hidden)]` and may change. Their docs now say that before the app starts parts register in `index` order, and that a part added after joins the running set last, whatever its `index`. They take a part as a plain `fn(&mut Registry)`. `nsh` gives a set one gate or a gate per part, never both, so `AppSets` asserts in debug builds that no set is added both whole and in parts instead of silently keeping the first.
…ption` non-exhaustive `element::Part` is gone: a bare `Part` at the root of `element` reads as a component's part, and callers pass a function item or a closure that captures nothing without naming the type. `nsh_base::parts::Part` is the same type, and a test registers nsh/base's parts with `Registry::part`. `SetDescription` is `#[non_exhaustive]`, since adding `parts` already broke struct literals: other crates build it with `SetDescription::of` or `From<Registry>`. `PartDescription::submodules` is `#[doc(hidden)]`, for nsh's tools. `Registry::part`'s docs say first to write parts as `#[part]` functions of `#[native_module]` on a module: called by hand it only groups what the set registers. A test covers the panic of a part registered from another part. `part_registering` is now `record_registration`, which says that it records.
`AppSet::unexported` serves both the gates an import keeps (every part, for such a name) and the warning, so the two cannot disagree.
Drop `#[part]`, `#[native_module]` on a module and the public `Registry::part`: the macro gives each component its function registers directly a hidden part of its own, and the rest of the function one more, so a TSX app's web runtime links only the components it imports. Agent: lead
…iption` The rest of a function `#[native_module]` splits is registered through the hidden `__private::native_module_rest`, which marks it for the set as a whole: any import of the set keeps it, so a plain side effect runs on the web and a component registered in a block is always linked. `PartDescription` and `SetDescription::parts` are `#[doc(hidden)]`: only nsh's tools read them. A component Rust code makes by name that no linked set registers logs at `warn`, naming it.
A chain of the registry's own methods on the parameter is split too: its components are parts, its other calls `__set` statements in order, so a chain ending in `module` calls splits unchanged. A body is kept whole when splitting could change it: an early `return` or `?`, an item other than `use`, a rebound or `mut` parameter, a method on the registry other than its registering ones, an assignment or swap of it, or a macro other than std's and `log`'s outside closures. The function itself gets `#[allow(dead_code)]` once split, so a split crate builds clean under `-D warnings`; the tests no longer call it and deny dead code.
`nsh package --target web` says, one line per set, when an app imports only some of the components a set links whenever it is imported: a set kept whole, or the components its `__set` registers. The gate name clash error says what the user can change now that parts are named after types.
|
💡 The rework follows the agreed direction (no new public API, per-component hidden parts), but two promised pieces are missing. Reviewed 2dbbab5 (diff read-only).
|
`the_parts_register_as_parts_of_a_set` fed `parts::ALL` to the parts of an app's split set, which `nsh/base` does not use: it checked nothing the change relies on. `nsh/base` keeps its own parts here; moving it to the per-component split is a change of its own.
…on` as released `registry()` runs the `#[native_module]` function as written again; a hidden entry makes the set from its parts. The describe program makes each set both ways and keeps the parts only when both register the same, else links the set whole and `nsh package` says why. The parts carry the function's lint attributes (`expect` as `allow`) and the spans of its `register` calls. A body is not split when a macro names the parameter (a format string's implicit argument too), when it defines a macro, or when it uses the parameter other than through the registry's own methods or by passing it to a function. `SetDescription` loses the `parts` field and `#[non_exhaustive]`, as in 0.8.0: the parts travel under a key of their own, through hidden functions of `description`. The packaging warning names a library the app depends on as such.
…nd check them in a process of their own `#[native_module]` now lists the parts of a set it splits (`__nsh_native_module_split` returns their names and functions), so `Registry` is back to what it was: the describe program derives each component part's exports by registering it into an empty set, and `__set` has the rest. It makes the set of parts in a process of its own (`--split <index>`), so a function that does something only once is no longer described as registering something else, and `catch_unwind` goes. The macro's check keeps only what the build or the describe check could not catch: items other than `use`, `macro_rules!`, an early `return`, a rebound parameter, a macro statement before a component's (it could declare what that names), and the registry named in code under a `cfg` other than a chain of its own methods (the check runs on the machine that builds the app). `__set` always allows unused variables: the parameter may be used only under a `cfg` that is off, and the function itself reports the user's own. The parts are hidden functions at the crate's root, and the warning for a set linked whole has one wording.
…its components A macro statement before a component's keeps the function whole, as it could declare what that statement names; the standard library's and `log`'s, named through their crate, expand to expressions.
… beside it `expand` takes the function again and checks its signature where it did; the parts and their list come from one function next to it, and the list names `description::Split`.
A part allows lints of the code the macro adds (`unused_variables` on `__set`, `non_snake_case` on a component's part), which a copied `#[forbid(..)]` refused with E0453. The function keeps its `forbid`.
The describe check compared descriptions only, so parts that lost a setup or a fake (a helper that registers one depending on what the set has) were kept. The process that makes the set of parts now also writes its fakes' and GPU renderers' names, how many setups it has and whether it has tokens and scroll bars, and the parts are kept only when that matches too. The `description` module doc no longer links the hidden `describe_split`.
…eb app imports (#164) Builds on longbridge/nsh#4066 (merged), which makes `#[native_module]` split a UI set by the components its function registers, so a web app links only the components it imports. The `[patch.crates-io]` here points at the commit #4066 was merged as (`0b172b6e3` on nsh `main`, every nsh crate at the full revision, `Cargo.lock` updated). Once an nsh release has the change, the patch goes and `nsh-core` / `nsh-base` are raised to that release. ## Size Measured with `nsh package --target web` built from #4066's head (`685cb840d`; the gallery row with the CLI of `e660ddfea`, an earlier head that packages the same), this library at `main` (raw / gzip / brotli of the app's `nsh.<digest>.wasm`; "whole" is the same library with an early `return` added, which the macro does not split, so it is one gate as before #4066, packaged with the same CLI): | App | Whole | Split | |---|---|---| | Renders one `Button` | 7.61 / 2.68 / 1.83 MB | 4.54 / 1.71 / 1.27 MB | | `Button`, and a page loaded with `import()` that uses `DatePicker` and `DataTable` | 7.61 / 2.68 / 1.83 MB | main 4.57 / 1.73 / 1.27 MB; `DatePicker` (0.72 / 0.24 / 0.17) and `DataTable` (0.47 / 0.16 / 0.12) load with the page | | The gallery (nearly every component) | 9.17 / 3.22 / 2.21 MB | 9.18 / 3.22 / 2.22 MB | The lazy page was checked in headless Chrome: its two modules and the page load only when it opens, and the date picker opens. ## Description The library's UI set was one gate on the web, so an app that imports one component linked all 117. With #4066 the set needs no change for this: `src/lib.rs` is the same as on `main` (one chain of `register` calls ending in two `module` calls), and the macro splits it into one part per component plus the rest (`Labels`, `Toasts`), which every import keeps. While describing the app, on the machine that builds it, nsh checks that the parts register the same set as the function (its components, modules and files, and its setups, fakes, tokens and renderers). Native builds, tests and apps whose root is Rust run the function as written, as before. What this pull request changes: - `Cargo.toml` / `Cargo.lock`: the nsh patch moves to #4066's merged commit, with a comment saying why and when it goes. - `CHANGELOG.md`: a new entry, web apps link only the components they import; this needs the NSH release after 0.8.0, and with 0.8.0 the library still works, linked whole. The existing "NSH 0.8" entry is unchanged. - Two specs and their tests follow behavior changes of `nsh/base`'s that the new patch brings in from nsh `main` (not from #4066, whose split changes no behavior): both tests already failed at nsh `main` before #4066 was merged, and a bisect between the previous patch (`2e2afa01d`) and `main` lands on each of these commits. - **Tab leaves a menu in one press** (longbridge/nsh#4015, `f61086222`). Tab and Shift+Tab in an open `DropdownMenu` or `ContextMenu` close every level and move the focus on to the stop after the trigger (before it, on Shift+Tab), as WAI-ARIA's menu button does; the focus used to stop on the trigger. `tab_from_a_submenu_closes_every_level_and_the_focus_goes_to_the_trigger` expected the old stop; it becomes `…_the_focus_moves_past_the_trigger` and checks both directions (its fixture gets a button before the trigger, so Shift+Tab does not wrap around to the same button). `DropdownMenu`'s NSH gap 16 and `ContextMenu`'s gap 6, which described the old stop, move to their Decisions; the later gaps are renumbered with their references. - **A shown tooltip binds no key** (longbridge/nsh#4163, `62e112a99`). Escape no longer hides a tooltip, and `onOpenChange` no longer reports `"cancel"`, so on Windows, whose keyboard focus shows a tooltip, the first Escape on a `SelectionBar`'s focused "×" clears. `escape_in_the_bar_asks_to_clear` expects that now. `Tooltip`'s spec, `TooltipEvent::OpenChange`'s doc and the generated skill reference, and the Escape wording of `SelectionBar` and `Popover`, follow; `SelectionBar`'s gap 7 moves to its Decisions. This failure was hidden on CI behind the first one (the test run stops at the first failing test binary). - `CHANGELOG.md`: an entry for both. An earlier revision restructured the set as a module of `#[part]` functions, one per file, with a test and a convention to keep it that way; nsh dropped that form, so those are gone and `src/lib.rs`, `tests/api.rs` and `docs/conventions.md` match `main`. ## Public API No change. Two behavior changes come with the new nsh (Tab leaving a menu in one press, Escape passing a tooltip by), listed above and in `CHANGELOG.md`. ## Tests `cargo check --all-targets`, `cargo clippy --all-targets --locked -- -D warnings`, `cargo fmt --check`, `cargo test --test api --test tokens --test skill` and `cargo test --no-fail-fast --tests` (every test binary) pass against the patched nsh.
Description
A TSX app that imports one component from a Rust UI set (
nsh.json"rust",#[native_module]) kept the whole set on the web: every component of a large library shipped with an app that renders a single button. The app's imports already say which components it uses, so the framework now uses them. Library authors write nothing new.Size
A component library of 117 components, unchanged (its
#[native_module]function is one chain ofregistercalls ending in twomodulecalls), packaged with the release CLI of this branch at685cb840d(the gallery row with the CLI ofe660ddfea, an earlier head that packages the same). "Whole" is the same library with an earlyreturnadded, which the macro does not split: the one gate every set had before this change, with the same CLI.nsh.wasmraw / gzip / brotli; on the gallery the split costs the gates of every component, and pruning has nothing to remove:Buttononly (a)Button, and a page loaded withimport()that usesDatePickerandDataTable(b)DatePicker0.72 / 0.24 / 0.17,DataTable0.47 / 0.16 / 0.12What it does
#[native_module]leaves its function andregistry()exactly as onmain, and adds the parts beside them. Each top-level statement that is a chain of the registry's own methods on the parameter (register,module,class,setup,tokens_from,assets, … andgpu_renderer/rich_extension) gives each component it registers a hidden function of its own,__nsh_native_part_<Type>(a repeated name gets_2). The chain's other calls go to the rest, in order.__set, kept whenever the app imports the set (side-effect, named or submodule import). A component registered any other way (in a loop, a block, a helper function, under acfg) is in__set.__nsh_native_module_split(), lists the parts (__setfirst).Registryknows nothing of parts: a part is a plainfn(&mut Registry), and the web runtime calls the parts of one set into oneRegistry, in the set's order.allow,warn,deny,expectasallow,forbidasdeny, also undercfg_attr, before or after the attribute) and the spans of itsregistercalls, so a crate clean under-D warningsstays clean (also with a statement under acfgthat is off), a#[forbid(..)]on the function still compiles, and "registered twice" points at the user's line.registry(), native runners andcargo testrun the function as written. Only the describe program and the TSX web runtime use the parts.ui_set_part!), andnsh packagekeeps the gates of the components the app imports plus__set. Components used only by code loaded withimport()load with that code. Cross-component use (DatePickerusingCalendar) is handled by wasm reachability pruning, as fornsh/base. A Rust app's root still gets the whole set.warn(wasdebug), once per name.What is split, what is not
The macro does not split a function where splitting could change what compiles or what it means (the web runtime runs
__setbefore the components):returnoutside closures);use, or defines a macro (macro_rules!, at any depth);letbinding it, or a parameter that is not a plain name);log's named by its crate (log::info!,std::println!): a macro could declare something the components' statements name;cfg(#[cfg(..)],#[cfg_attr(..)]at any depth, orcfg!), other than in a plain chain of the registry's methods: what such code does depends on the target, and the check below runs on the machine that builds the app.Everything else is checked on the machine that builds the app. When
nshdescribes the app, the describe program runs itself again as a fresh process for each split set (--split <i>), makes the set from its parts there, and compares that set with the function's as written, which the describe program made in its own process: their descriptions, and what a description leaves out (the names of fakes and GPU renderers, how many setups, whether tokens and scroll bars are set). Each way runs once in a process of its own. It keeps the parts only when the two are equal; otherwise (different, or making the parts panics) the set is linked whole, and-vshows what making the parts wrote to stderr (a panic's message). A function that does something only once per process therefore compares correctly. This catches a helper that registers depending on what the set has (a component, a setup or a fake), a crate's own macro named likelog's, anassert!on the set and aClone::clone_fromof it.Limits of the check:
cfg; a helper function that has acfginside its own body is not seen.nsh package --target webwarns, one line per set, when an app imports only some of the components a set links whenever it is imported: a set kept whole (by the macro, or by the describe check), or components in__set. One wording, with the reason and how to write the function so it splits; for a set from a library the app depends on (an nshm, a Cargo dependency), it says "the library of the UI set …" and to report it to the library's authors:For library authors
registercalls, or a chain mixingregisterwithmodule/class/setup/ … needs no change.nsh package --target websays so when an app uses only some of its components.Public API
No new public authoring API: no attribute, method or type for library authors. Nothing released changes:
Registry,SetDescriptionandDescriptionare exactly as in 0.8.0 (Registryonly gets apub(crate)method the check uses).New public but
#[doc(hidden)], for nsh's tools (may change), innsh_core::element::description:type Split = (&'static str, &'static [(&'static str, fn(&mut Registry))]): a set's specifier and its parts;PartDescription { name, exports, set_level, submodules }andSetParts { specifier, parts, split_changes }, both#[non_exhaustive];describe_split(sets: &[fn() -> Registry], split: &[fn() -> Option<Split>]) -> String: the describe program's body;parts_from_json(&str) -> Result<Vec<SetParts>, String>.The JSON description carries a split set's parts under its own
"parts"key (or"split_changes"), which readers of format 2, 0.8.0's included, skip;FORMATstays 2.Hidden, for generated code only:
nsh_core::element::__private::native_module_unsplit,nsh_platform_web::{add_ui_set_part, ui_set_part!}, and, in the user's crate,#[doc(hidden)] pub fn __nsh_native_module_split()and one#[doc(hidden)] pub fn __nsh_native_part_<Type>(registry: &mut Registry)per part.Breaking Changes
None.
Tests
nsh-macrosintegration tests: the set made of the parts against the function as written (full description) for a mixed chain; each part on its own; the rest kept with__set; a component in a block kept with__set; bodies not split (earlyreturn, rebound parameter, local items,macro_rules!in a block, a crate macro before the components, a helper undercfg, anif cfg!branch); bodies split that only read the set; the describe check dropping the parts of bodies that register otherwise when split (a read, in the body or a closure, that decides what to register,Clone::clone_from, a helper that registers onspecs().is_empty(), a crate's owninfo!,assert!on the set) and keeping them otherwise; lint attributes before and after the attribute in a module under#[deny(warnings)], including a statement under#[cfg(any())]that names the registry, and#[forbid(non_snake_case)]and#[forbid(unused_variables)]on a split function; a component registered twice panics at the user's line; part names (Badge_2,r#type,Of<Pin>→Of). Unit tests: which bodies split, thestd/logmacro exemption, thecfgcases, a mixed chain's__setstatements in order, and the lint attributes the parts get.nsh-core: a split set keeps its parts only when they register the same, a setup included; a part without later keys is read.nsh-cli: the describe program; parts read from the description; gates; the warning for a set kept whole, kept whole by the check, from a library, and with components in__set, and none for a set imported wholly or taken whole.nsh-platform-web(tests/text): the parts of a set make up one set in the set's order.cargo clippy … -D warningsfornsh-macros,nsh-core,nsh-platform-web,nsh-cli,tasks-rs; the library above builds clean under its ownclippy --all-targets -D warnings.registry()and parts, with the describe check in fresh processes:registry()always equals the function; every body whose parts would register a different set ends linked whole (helper_cond,assert_capture,crate_macro_named_info,clone_from_ufcs,helper_reads_then_registers, a closure or a shadowed registry that decides what to register, areturninside a crate macro, a helper that registers a fake or a setup only when the set has a component, a part that exits); bodies that run differently the second time in a process (once_flag,once_panic) keep their parts; bodies with target-dependent code are not split; read-only bodies are split with the same set (the limit above). Twenty lint crates are clean underclippy -D warningson the host and on wasm, including one under#![deny(warnings)]with a statement under acfgthat is off, andforbidon the function (non_snake_case,unused_variables,unused,warnings) or on the crate.init ran: true), and sets for each warning.Follow-ups
nsh/baseon the same mechanism. Not in this PR: itsnsh_part_*gate names andnsh-<part>file names are documented as stable, and its hand-made groups (core,menu,overlays, …) differ from a per-component split, so moving it changes those names: a separate change.MAX_LAZY(127) lazy gates, past which gates fold into the main module without a warning; merging the gates that the same chunks load into one side module would be a packaging change only.__setin the web runtime, before the components; one that only reads the set sees it without them. Refusing such calls would be possible later without API change.