Skip to content

fix: prevent metadata loss in multi-threaded md2md builds#1596

Merged
martyanovandrey merged 5 commits intomasterfrom
multithreadMeta
Jan 21, 2026
Merged

fix: prevent metadata loss in multi-threaded md2md builds#1596
martyanovandrey merged 5 commits intomasterfrom
multithreadMeta

Conversation

@martyanovandrey
Copy link
Copy Markdown
Contributor

@martyanovandrey martyanovandrey commented Jan 15, 2026

fix: prevent metadata loss when file is both entry and include

Problem

When building documentation in md2md mode, files that are both a TOC entry and included by another file could lose their metadata (like __system variables).

This issue is especially noticeable in multi-threaded builds, where processing order is non-deterministic. The metadata loss appears random — sometimes present, sometimes missing — depending on which thread writes last.

Root Cause

The issue occurs because the same file can be written twice:

  1. As an entry — written WITH YAML frontmatter (metadata included)
  2. As an include dependency — written WITHOUT YAML frontmatter

The last write wins. If the include write happens after the entry write, all metadata is lost.

Example scenario:

entry-as-include.md  ← listed in toc.yaml as entry
includer.md          ← contains {% include [](entry-as-include.md) %}

Processing order:

1. Process entry-as-include.md → write WITH frontmatter ✓
2. Process includer.md → write entry-as-include.md as include WITHOUT frontmatter ✗
   (overwrites the file, metadata lost)

In single-threaded mode, the order is deterministic (though still incorrect for affected files). In multi-threaded mode, the order depends on thread scheduling, making the bug appear intermittent.

How to Reproduce

  1. Create a file that is both:

    • Listed in toc.yaml as an entry
    • Included via {% include %} in another file
  2. Build with --output-format=md --add-system-meta

  3. Check output — the file may be missing __system metadata depending on processing order

Solution

Add YAML frontmatter when writing include files in output-md:

Now both writers (entry and include) produce the same output with frontmatter, so the write order no longer matters.

@martyanovandrey martyanovandrey requested a review from a team as a code owner January 15, 2026 16:45
@martyanovandrey martyanovandrey requested review from separatrixxx and removed request for a team January 15, 2026 16:45
@Feverqwe Feverqwe self-requested a review January 16, 2026 07:11
Copy link
Copy Markdown
Contributor

@Feverqwe Feverqwe left a comment

Choose a reason for hiding this comment

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

Looks grate!

@martyanovandrey martyanovandrey merged commit 13d9480 into master Jan 21, 2026
5 of 6 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.

2 participants