Skip to content

fix(cli): keep doc comments of imported grammar elements when embedding - #2231

Merged
msujew merged 2 commits into
eclipse-langium:mainfrom
simonyang08:fix/2065-embed-keeps-comments
Sep 23, 2026
Merged

msujew merged 2 commits into
eclipse-langium:mainfrom
simonyang08:fix/2065-embed-keeps-comments

Conversation

@simonyang08

Copy link
Copy Markdown
Contributor

Fixes #2065

Root cause

embedReferencedGrammar (packages/langium-cli/src/generate.ts) copies imported grammar elements with AstUtils.copyAstNode, which drops the `` reference. Both comment consumers resolve comments through the CST:

  • the grammar serializer (comments: true → DefaultCommentProvider → findCommentNode(node.$cstNode, ...))
  • the BNF generator (findCommentNode(rule.$cstNode, ['ML_COMMENT']))

so rules and types from imported grammars lose their doc comments in the generated grammar.ts (``) and BNF output.

Fix

Re-attach the original CST node to each copied element:

Object.defineProperty(copy, '', { value: element., writable: true });

Embedded grammars are only used for read-only generation, so sharing the CST node with the imported grammar's document is safe. (The defineProperty route is used because `` is typed readonly and copyAstNode deliberately skips `$`-properties.)

Tests

New packages/langium-cli/test/generator/embed-grammars.test.ts drives the real embedGrammars path with a two-grammar setup (import + cross-reference) and locks down:

  1. embedded elements keep their ``
  2. the BNF output contains the imported rule's doc comment
  3. the serialized grammar contains `` for imported rules and types

vitest run packages/langium-cli/test: 57 passed, 0 failed (54 existing + 3 new).

Two adjacent observations from reproducing this

  1. Only multiline comments are collected. DefaultCommentProvider follows GrammarConfig.multilineCommentRules (auto-derived from multiline comment terminal rules, so SL_COMMENT//// line docs are invisible by design), and the BNF generator hardcodes ['ML_COMMENT'] instead of going through CommentProvider — so a custom multilineCommentRules override wouldn't reach the BNF output. Worth a deliberate decision at some point; out of scope here.
  2. ast.ts never emits JSDoc. We checked the current main and v3.5.0: the AST generator contains no comment emission at all, so the 'JSDoc appears on interfaces but not types' half of the original report doesn't reproduce on official code — the actual comment-loss symptoms are confined to grammar.ts and BNF output, both covered by this fix.

Reproduction project: two grammars (import + cross-reference), bnf target enabled — happy to share if useful.

embedReferencedGrammar copies imported grammar elements with
AstUtils.copyAstNode, which drops the $cstNode reference. Comment
consumers resolve comments through the CST - the grammar serializer
via CommentProvider and the BNF generator via findCommentNode - so
rules and types from imported grammars lost their doc comments in the
generated grammar.ts ($comment) and BNF output.

Re-attach the original CST node to each copied element. Embedded
grammars are only used for read-only generation, so sharing the CST
node with the imported grammar's document is safe.

Fixes eclipse-langium#2065

Signed-off-by: simonyang08 <ppt5928@gmail.com>
LanguageInfo is not re-exported from generate.ts; the wrong import
path broke the CI type check over test sources.

Signed-off-by: simonyang08 <ppt5928@gmail.com>

@msujew msujew left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks, works well 👍

@msujew
msujew merged commit 22ccef9 into eclipse-langium:main Sep 23, 2026
4 checks passed
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.

Comment Collection / Inlining

3 participants