Skip to content

fix(doc_cli): include module and class members in the HTML search index - #1476

Merged
CppCXY merged 2 commits into
EmmyLuaLs:mainfrom
ligurio:ligurio/gh-1475-fix-search-index
Nov 30, 2026
Merged

CppCXY merged 2 commits into
EmmyLuaLs:mainfrom
ligurio:ligurio/gh-1475-fix-search-index

Conversation

@ligurio

@ligurio ligurio commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #1475

@github-actions github-actions 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.

Code Review

.github/workflows/coverage.yml

1. concurrency.group collision risk (minor)
The group uses coverage-${{ github.workflow }}-${{ github.ref }}. github.workflow is already Coverage, so github.ref is the discriminator. This is fine, but ${{ github.workflow }} is redundant. Not a bug.

2. Fork-PR skip means missing PR coverage comment (design note)
For fork PRs the upload is skipped, so Coveralls won't report on those PRs. That's an explicit trade-off documented in the comment — acceptable, but worth confirming that's the desired UX (e.g. continue-on-error: true on the upload step might be preferable so non-fork failures still surface but don't fail the build).

3. fetch-depth: 0 on every run
Full history is only needed by coveralls for base-commit diffs. Combined with cancel-in-progress on PRs this is usually fine, but it slows clone time noticeably. Consider fetch-depth: 0 only on the pull_request/push events that need it (all of them here, so low priority).

4. Missing --locked
cargo llvm-cov --workspace --all-features will update Cargo.lock if it's stale. In CI you typically want --locked to fail fast on a dirty lockfile. Same applies if a Cargo.lock isn't committed.


crates/emmylua_doc_cli/src/html_generator/mod.rs

5. &mut pages iteration + later &pages borrow (correct, but note)
for (filename, doc) in &mut pages then write_search_index(&output, &index_doc.nav, &pages) — this is fine because the mutable borrow ends with the loop, but it's slightly fragile if anyone later moves a &pages use inside the loop. Not a defect.

6. Potential duplicate search entries from methods.chain(fields)
If a name appears in both doc.methods and doc.fields (e.g. overloaded / re-declared member, or a field that's also emitted as a method by the doc resolver), the search index will contain two entries with the same name and href. The tests use find_entry which asserts len == 1, so a fixture triggering this would fail — but the production code has no dedup. Consider using a HashSet or asserting/no-op on duplicates. At minimum, a duplicate silently produces duplicate search results.

7. Anchor generation assumption is untested by the helper
format!("{href}#{}", member.name) assumes the member's DOM id is exactly member.name. The tests validate this for the given fixtures (assert_member_anchors_exist), but there's no guard in production code. If HtmlDoc member name ever differs from the anchor id (e.g. HTML-escaping for names with <, &, or Unicode that gets escaped differently), search links will silently 404 the anchor. Worth a comment or a shared constant/function that both the template and the index use to derive the anchor id.

8. HTML-unsafe anchors
member.name is inserted into a JSON href verbatim. serde_json::to_string escapes for JS string context, so search-index.js itself is safe. However, if main.js sets location.hash from a name containing #, ?, spaces, or non-ASCII, the browser may misparse the anchor. If Lua identifiers are guaranteed ASCII [A-Za-z0-9_.], ignore; otherwise percent-encoding the fragment is safer.

9. pages type coupling
Changing write_search_index to take &[(String, HtmlDoc)] couples it to the concrete page tuple. A small struct PageRef<'a> { href: &'a str, doc: &'a HtmlDoc } or just impl Iterator<Item = (&str, &HtmlDoc)> would be cleaner and avoid the &pages shape leaking. Not blocking.


crates/emmylua_doc_cli/tests/common/mod.rs

10. Brittle string strip
strip_prefix("window.SEARCH_INDEX = ") / strip_suffix(';') will break the moment the emitter is wrapped in a function, minified, or gains a trailing newline (the suffix ; alone requires no trailing whitespace — actually strip_suffix(';') does not tolerate a trailing \n). The current writer emits window.SEARCH_INDEX = {json};, matching, but this is fragile. If/when the generator changes, every test breaks with a confusing "unexpected search index format" panic. Consider asserting the exact prefix/suffix in the generator test, or reading via a lightweight extraction that trims first.


crates/emmylua_doc_cli/tests/html_search_index.rs

11. Test gaps

  • No test covers a duplicate member name across methods/fields (see item 6).
  • No test covers a member whose name collides with a page-level entry name (e.g. global g vs the field g, or a class Widget vs. a member named Widget). find_entry requiring exactly one match would catch page-level collisions, but there is no explicit case.
  • No test for an empty fixture / no pages — trivially fine, but a guard against a panic-on-empty regression is cheap.
  • assert_member_anchors_exist skips non-member kinds, so a regression that emitted members under a different kind label (e.g. "function" instead of "method") would slip through the anchor check (though the explicit assert_eq!(method["kind"], "method") per test covers it).

12. find_entry panics with the full entries dump on failure
For large fixtures this is noisy but acceptable in tests. Fine.

13. common module is shared
html_search_index.rs declares mod common; — if common/mod.rs was previously only used by another test file, adding a second consumer is fine (each integration test compiles its own copy), but unused-fn warnings can appear per test binary. Since read_search_index is used here, no warning. OK.


Summary

Severity Issue
Medium Possible duplicate search entries when a name is both method and field (item 6)
Medium Anchor id derived independently of template; no production-side guard (item 7)
Low strip_suffix(';') brittle to trailing whitespace (item 10)
Low Missing --locked in cargo llvm-cov (item 4)
Low Duplicate/collision test coverage gaps (item 11)
Nit Redundant github.workflow in concurrency group; &mut pages iteration fragility

@ligurio
ligurio force-pushed the ligurio/gh-1475-fix-search-index branch from 4780618 to f44f980 Compare November 27, 2026 19:21
Include module, class, enum and global members in
static/search-index.js with their page anchors so searching for a
member name finds it.

Fixes EmmyLuaLs#1475
@ligurio
ligurio force-pushed the ligurio/gh-1475-fix-search-index branch from f44f980 to cf0c5bc Compare November 27, 2026 19:21
Deduplicate identical member entries, share member anchors between
the templates and the index, and make search-index parsing in
tests resilient to whitespace. Expand coverage for globals, enums,
private members, namespaced anchors, empty docs and duplicate
entries

Follows up EmmyLuaLs#1475
@ligurio
ligurio force-pushed the ligurio/gh-1475-fix-search-index branch from cf0c5bc to 2ba0e41 Compare November 27, 2026 19:30
@ligurio

ligurio commented Nov 27, 2026

Copy link
Copy Markdown
Contributor Author

Addressed

5. &mut pages iteration followed by a &pages borrow

Reworked as suggested in spirit: search entries are now built before the
render loop:

let search_entries = build_search_entries(&nav, &pages);
// ... render loop mutates `pages` ...
write_search_index(&output, &search_entries)?;

write_search_index no longer receives pages at all, so there is no
mutable/immutable borrow pair across the loop and no place for a future
&pages use to accidentally land inside it.

6. Duplicate member entries

build_search_entries now deduplicates by (name, href) with a HashSet,
preserving insertion order. write_search_index is reduced to serialization
and writing.

A unit test puts the same member into both HtmlDoc.methods and
HtmlDoc.fields and asserts that the index contains exactly one entry:

  • html_generator::tests::search_entries_dedupe_identical_members

7. Anchor id derived independently of the template

Added HtmlMember::anchor as the single source of truth for a member's DOM
id. It is used by:

  • template/html/macros.html — id="{{ m.anchor }}"
  • template/html/item.html — TOC href="#{{ m.anchor }}"
  • build_search_entries — format!("{href}#{}", member.anchor)

A unit test deliberately uses a different name and anchor to verify that
the search href follows anchor while the displayed name stays unchanged:

  • html_generator::tests::search_entries_use_member_anchor_for_href

The existing integration helper assert_member_anchors_exist still checks
that every generated member href resolves to an id present in the page.

10. Brittle search-index.js parsing in tests

tests/common/mod.rs::read_search_index now tolerates whitespace/newlines:

let json = js
    .strip_prefix("window.SEARCH_INDEX =")
    .unwrap_or_else(...)
    .trim()
    .strip_suffix(';')
    .unwrap_or_else(...)
    .trim();

11. Test coverage gaps

Added:

  • Duplicate member entry unit test (item 6 above).
  • Unit test for the shared anchor contract (item 7 above).
  • Unit test for an empty index (search_entries_empty_without_docs).
  • Integration test that a fixture with no documented items produces an empty
    search-index.js (search_index_is_empty_without_docs).

Member kinds are already asserted explicitly (assert_eq!(kind, "method" / "field")) in every integration test, so a wrong kind label cannot pass just
because assert_member_anchors_exist skips non-member entries.

Not changed

8. HTML-unsafe anchors

This is a real edge case for bracket/index keys such as ["a#b"], but it
predates this change and affects the existing id attributes and TOC links as
well. Fixing it properly requires a shared policy for DOM-id escaping and URL
fragment encoding (and changes the resulting anchors). It is marked with a
TODO on HtmlMember::anchor and should be handled in a separate change.
For regular Lua identifier members, name is a safe ASCII anchor.

9. pages type coupling

write_search_index no longer takes pages; the tuple shape is confined to
the private build_search_entries helper. Introducing PageRef would not
remove the underlying (href, doc) association or improve readability, so it
was left as-is.

4. Missing --locked in cargo llvm-cov

The current tree has no llvm-cov workflow; .github/workflows/test.yml runs
cargo test --workspace --all-features. This item appears to belong to a
different patch/branch, so there is nothing to change here. If a coverage
workflow is added, --locked should be used there as well.

Nits

  • The redundant github.workflow concurrency group does not exist in the
    current workflows.
  • find_entry includes the full entries dump in its panic message. That is
    intentional: fixtures are small and the diagnostic is useful.
  • tests/common/mod.rs is compiled per integration test binary; the newly
    added read_search_index is used by html_search_index.rs, so there are no
    unused-function warnings.

@ligurio

ligurio commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

@CppCXY Can we fix this? It would make searching much more convenient.

@CppCXY
CppCXY merged commit 384535d into EmmyLuaLs:main Nov 30, 2026
16 checks passed
@ligurio
ligurio deleted the ligurio/gh-1475-fix-search-index branch November 30, 2026 09:23
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.

fix(doc_cli): include module and class members in the HTML search index

2 participants