Skip to content

Only include used blocks in block-meta.json - #6340

Open
VPS-Obi wants to merge 1 commit into
claude/add-decorators-email-campaign-gybqrafrom
claude/unused-block-registration-4y261x
Open

Only include used blocks in block-meta.json#6340
VPS-Obi wants to merge 1 commit into
claude/add-decorators-email-campaign-gybqrafrom
claude/unused-block-registration-4y261x

Conversation

@VPS-Obi

@VPS-Obi VPS-Obi commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Blocks are registered as a side effect of createBlock. Therefore, blocks that a library creates on import (e.g., the deprecated SpaceBlock) end up in the application's block-meta.json even when the application doesn't use them. This leads to generating types for unused blocks and may lead to broken types when an application defines a block with the same name (e.g., its own Space block).

This is a well known issue. It came up once again while creating a MCP tool that returns a block's schema, so it's time to finally fix it. 😁

I tried to fix this awhile ago by making the block registration explicit when a block is used (see #2889). For instance, RootBlockType would call this.block.register() in its constructor. I can't remember why we didn't continue down this path, but I believe it may be because we were unsure if this would correctly register all used blocks.

This PR takes a different approach: Blocks continue to be registered as a side effect of createBlock. getBlocksMeta() gets support for passing used blocks via the blocks option (with a fallback to getRegisteredBlocks(). BlocksMetaService discovers all used root blocks and passes them to getBlocksMeta() when generating block-meta.json.

https://claude.ai/code/session_01NN1pwjLehGbk1ChEtWmvqu

Blocks are registered as a side effect of createBlock. Blocks a library
creates on import, such as the deprecated SpaceBlock, therefore ended up
in the application's block-meta.json even when the application doesn't
use them. Besides generating types for unused blocks, this breaks the
generated types when an application defines a block with the same name:
the demo's own Space block and the library's Space block were both
written to block-meta.json, so the generated SpaceBlockData interfaces
merged into one containing the fields of both blocks.

Determine the used blocks from the application instead: all root blocks
discovered from the entities and, recursively, all blocks they
reference. Blocks that reference other blocks without exposing them in
their block meta - the link block embedded in rich text content -
declare them in the new optional referencedBlocks property of Block.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NN1pwjLehGbk1ChEtWmvqu
@coderabbitai

coderabbitai Bot commented Sep 9, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Advanced

Run ID: 6dbf4c29-a064-478d-8271-936a37dc5718

📥 Commits

Reviewing files that changed from the base of the PR and between 6cc32b1 and 38d59e9.

📒 Files selected for processing (9)
  • .changeset/only-register-used-blocks.md
  • demo/api/block-meta.json
  • packages/api/cms-api/src/blocks/block.ts
  • packages/api/cms-api/src/blocks/blocks-meta.service.ts
  • packages/api/cms-api/src/blocks/blocks-meta.ts
  • packages/api/cms-api/src/blocks/factories/createRichTextBlock.ts
  • packages/api/cms-api/src/blocks/tipTap/createTipTapRichTextBlock.ts
  • packages/api/cms-api/src/blocks/used-blocks.spec.ts
  • packages/api/cms-api/src/blocks/used-blocks.ts
💤 Files with no reviewable changes (1)
  • demo/api/block-meta.json

Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review.


📝 Summary

Summary by CodeRabbit

  • New Features

    • Block metadata now includes only root blocks and the blocks they reference, reducing unnecessary entries.
    • Rich-text links and nested blocks are included automatically when used.
    • Block metadata generation can now target a specified set of blocks.
  • Bug Fixes

    • Removed obsolete newsletter image and space definitions from the demo metadata.

Walkthrough

The API now generates block metadata from discovered root blocks and their recursively referenced blocks. It adds explicit block references, updates rich-text blocks, changes metadata generation inputs, adds traversal tests, and removes unused demo metadata.

Changes

Used block metadata

Layer / File(s) Summary
Block reference traversal
packages/api/cms-api/src/blocks/block.ts, packages/api/cms-api/src/blocks/used-blocks.ts
Block supports optional referencedBlocks. getUsedBlocks recursively collects blocks from metadata fields and explicit references without duplicates.
Metadata generation wiring
packages/api/cms-api/src/blocks/blocks-meta.service.ts, packages/api/cms-api/src/blocks/blocks-meta.ts, packages/api/cms-api/src/blocks/factories/createRichTextBlock.ts, packages/api/cms-api/src/blocks/tipTap/createTipTapRichTextBlock.ts
Startup discovers root blocks, computes used blocks, and passes them to getBlocksMeta. Rich-text blocks expose configured link blocks as references.
Output and validation
packages/api/cms-api/src/blocks/used-blocks.spec.ts, demo/api/block-meta.json, .changeset/only-register-used-blocks.md
Tests cover nested references, unused blocks, TipTap dependencies, and deduplication. Demo metadata removes unused definitions. The changeset documents the new behavior.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant NestLifecycle
  participant BlocksMetaService
  participant DiscoverService
  participant getUsedBlocks
  participant getBlocksMeta
  NestLifecycle->>BlocksMetaService: onModuleInit()
  BlocksMetaService->>DiscoverService: discoverRootBlocks()
  DiscoverService-->>BlocksMetaService: root blocks
  BlocksMetaService->>getUsedBlocks: collect used blocks
  getUsedBlocks-->>BlocksMetaService: deduplicated blocks
  BlocksMetaService->>getBlocksMeta: generate metadata
Loading

Merge Risk: ⚪ Minimal · up to 38d59

Metadata generation now includes only discovered and referenced blocks, with coverage for nested dependencies and unused-block exclusion. No actionable merge-blocking risk is evidenced.


Caution

Pre-merge checks failed

Please resolve all errors before merging. Addressing warnings is optional.

  • Ignore (reviewers only)

❌ Failed checks (1 error, 1 warning)

Check name Status Explanation Resolution
Requires Human Review ❌ Error The PR changes the public API. It adds referencedBlocks?: Block[] to the exported Block type in block.ts, and changes the exported getBlocksMeta() signature to accept an optional Block[] arg… Require human review of the public API changes. Confirm that the Block.referencedBlocks addition and the getBlocksMeta(blocks?) signature are intentional, documented, and compatible with consumers before merging.
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 7 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the primary change: limiting block-meta.json to blocks used by the application.
Description check ✅ Passed The description explains the existing issue, the proposed implementation, and the expected benefit. It is directly related to the changeset.
Full details: Docstring Coverage

Explanation

Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 8 functions across 7 files. (1 skipped: 1 unsupported.)

Full details: Requires Human Review

Explanation

The PR changes the public API. It adds referencedBlocks?: Block[] to the exported Block type in block.ts, and changes the exported getBlocksMeta() signature to accept an optional Block[] argument. src/index.ts re-exports both Block and getBlocksMeta. The PR adds 63 non-ignored handwritten source lines, which is below the 300-line limit. No deleted tests or other listed trigger is present.

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/unused-block-registration-4y261x

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@VPS-Obi
VPS-Obi changed the base branch from main to claude/add-decorators-email-campaign-gybqra September 9, 2026 10:44
@VPS-Obi VPS-Obi self-assigned this Sep 9, 2026
@VPS-Obi
VPS-Obi added this pull request to stack #6341 September 9, 2026 10:47
@VPS-Obi

VPS-Obi commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

@greptileai review.

@VPS-Obi
VPS-Obi marked this pull request as ready for review September 10, 2026 13:18
@VPS-Obi
VPS-Obi requested a review from nsams September 10, 2026 13:18
@greptile-apps

greptile-apps Bot commented Sep 10, 2026

Copy link
Copy Markdown

Greptile Summary

This PR limits generated block metadata to root blocks discovered from annotated entities and their recursively referenced dependencies.

  • Adds recursive used-block discovery across data metadata, input metadata, TipTap child blocks, and explicit hidden references.
  • Updates rich-text factories to declare link-block dependencies.
  • Integrates root-block discovery into BlocksMetaService.
  • Adds focused traversal and deduplication tests.

Confidence Score: 5/5

The PR appears safe to merge with no actionable defects identified in the changed behavior.

The recursive traversal covers the repository’s current block-reference representations, the rich-text factories declare their otherwise hidden dependencies, and the service wiring resolves in the application’s current module composition.

Important Files Changed

Filename Overview
packages/api/cms-api/src/blocks/blocks-meta.service.ts Generates metadata from discovered root blocks and their dependency closure rather than every globally registered block.
packages/api/cms-api/src/blocks/used-blocks.ts Recursively traverses all block-bearing metadata kinds and explicit references while preventing repeated object traversal.
packages/api/cms-api/src/blocks/block.ts Extends the public Block shape with optional explicit references for dependencies not represented in metadata.
packages/api/cms-api/src/blocks/blocks-meta.ts Allows callers to provide a filtered block collection while preserving the registered-block fallback.
packages/api/cms-api/src/blocks/factories/createRichTextBlock.ts Declares the embedded link block as an explicit dependency.
packages/api/cms-api/src/blocks/tipTap/createTipTapRichTextBlock.ts Declares the optional link block as an explicit dependency while child blocks remain discoverable through metadata.
packages/api/cms-api/src/blocks/used-blocks.spec.ts Covers recursive references, exclusion of unused registrations, TipTap dependencies, and deduplication.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Annotated root-block entities] --> B[DiscoverService.discoverRootBlocks]
    B --> C[getUsedBlocks]
    C --> D[Traverse blockMeta]
    C --> E[Traverse blockInputMeta]
    C --> F[Traverse referencedBlocks]
    D --> C
    E --> C
    F --> C
    C --> G[getBlocksMeta]
    G --> H[Write block-meta.json]
Loading

Reviews (1): Last reviewed commit: "cms-api: Only write used blocks to block..." | Re-trigger Greptile

@VPS-Obi

VPS-Obi commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

@greptileai why didn't you approve?

@greptile-apps

greptile-apps Bot commented Sep 10, 2026

Copy link
Copy Markdown

You're right to ask. I found no actionable defects and rated the PR safe to merge, but I posted that as a summary instead of submitting a formal GitHub approval. So there wasn't a technical reason to withhold approval—the approval state simply wasn't created. Sorry for the confusion.

@VPS-Obi

VPS-Obi commented Sep 10, 2026

Copy link
Copy Markdown
Contributor Author

@greptileai approve then?

@greptile-apps

greptile-apps Bot commented Sep 10, 2026

Copy link
Copy Markdown

Yes — approved from my side. I found no actionable defects in the implementation; the remaining CodeRabbit “Requires Human Review” item is an automated public-API confirmation, not a code issue.

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