elastic / elastic/ai-github-actions

[framework-best-practices] Use MkDocs 1.6 `File.generated(...)` in docs hook instead of temp-file plumbing

Open
#1,117 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
11
Forks
16
Avg merge
22h 9m
Merged PRs (30d)
31

Description

## Framework / Library Best Practices Findings

### 1. Generated docs hook manually writes temp files instead of using MkDocs' native generated-file API
**Library:** `mkdocs>=1.6.0,<2.0` (from `pyproject.toml:7`)
**Library feature:** `mkdocs.structure.files.File.generated(...)` (available since MkDocs 1.6)

**Current code:**
- `docs/hooks.py:9-13` imports and uses `tempfile`/`shutil` + module-global `_tmp_dir`
- `docs/hooks.py:117-124` creates and manages a temp dir in `on_pre_build`
- `docs/hooks.py:139-149` writes generated content to disk and appends `File(..., src_dir=_tmp_dir, ...)`

**What is wrong:**
The hook reimplements generated-file handling with manual temporary-directory lifecycle and disk writes, even though the project already requires a MkDocs version that provides native in-memory generated files.

**Why it matters (medium):**
This adds avoidable mutable global state and filesystem lifecycle code (`mkdtemp`, cleanup, per-file writes), increasing maintenance surface and failure modes (cleanup/state bugs) in docs builds.

**Simplification:**
Replace temp-dir + `File(... src_dir=...)` flow with `File.generated(...)` in `on_files`, passing `rel_path` and generated page content directly; remove `_tmp_dir` state and `on_pre_build` cleanup logic.

**Documentation:**
- MkDocs 1.6 release notes (`File.generated`): (www.mkdocs.org/redacted)
- MkDocs API reference: (www.mkdocs.org/redacted)

## Suggested Actions
- [ ] Refactor `docs/hooks.py` to append generated pages via `File.generated(...)` instead of writing temporary files.
- [ ] Remove `_tmp_dir`/`on_pre_build` temp-directory management and related cleanup code.
- [ ] Keep generated page content/paths unchanged and run docs build to confirm output parity.

> [!WARNING]
>
> ⚠️ Firewall blocked 1 domain
>
> The following domain was blocked by the firewall during workflow execution:
>
> - `www.mkdocs.org`
>
> To allow these domains, add them to the `network.allowed` list in your workflow frontmatter:
>
> ```yaml
> network:
> allowed:
> - defaults
> - "www.mkdocs.org"
> ```
>
> See [Network Configuration](https://github.github.com/gh-aw/reference/network/) for more information.
>
>

---
[What is this?](https://ela.st/github-ai-tools) | [From workflow: Trigger Framework Best Practices](https://github.com/elastic/ai-github-actions/actions/runs/25920843044)

Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.