Repository navigation
fix(cli): keep doc comments of imported grammar elements when embedding - #2231
Merged
msujew merged 2 commits intoSep 23, 2026
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2065
Root cause
embedReferencedGrammar(packages/langium-cli/src/generate.ts) copies imported grammar elements withAstUtils.copyAstNode, which drops the `` reference. Both comment consumers resolve comments through the CST:comments: true→DefaultCommentProvider→findCommentNode(node.$cstNode, ...))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:
Embedded grammars are only used for read-only generation, so sharing the CST node with the imported grammar's document is safe. (The
definePropertyroute is used because `` is typed readonly andcopyAstNodedeliberately skips `$`-properties.)Tests
New
packages/langium-cli/test/generator/embed-grammars.test.tsdrives the realembedGrammarspath with a two-grammar setup (import + cross-reference) and locks down:vitest run packages/langium-cli/test: 57 passed, 0 failed (54 existing + 3 new).Two adjacent observations from reproducing this
DefaultCommentProviderfollowsGrammarConfig.multilineCommentRules(auto-derived from multiline comment terminal rules, soSL_COMMENT////line docs are invisible by design), and the BNF generator hardcodes['ML_COMMENT']instead of going throughCommentProvider— so a custommultilineCommentRulesoverride wouldn't reach the BNF output. Worth a deliberate decision at some point; out of scope here.ast.tsnever 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 togrammar.tsand BNF output, both covered by this fix.Reproduction project: two grammars (import + cross-reference),
bnftarget enabled — happy to share if useful.