Skip to content

Commit 3f9dce7

Browse files
committed
fix(documentation): Make API sidebar generation deterministic and order-safe
The API sidebar in .vitepress/config.ts is built by walking fs.readdirSync("docs/api") and merging entries into a tree. Two issues were causing failures and inconsistencies: - readdirSync returns entries in filesystem order (insertion order on Linux, alphabetical on macOS APFS). When a "module-..." leaf file was processed before a sibling that needed to nest under the same name (e.g. module-@ui5_logger.md before @ui5_logger_Logger.md), the recursion crashed with "Cannot read properties of undefined (reading 'push')" because the leaf had no items array. This broke the build:vitepress step on GitHub Actions. - Even when it succeeded, sibling order in the generated sidebar varied across platforms. Fixes: - Sort the readdir result so the input order — and therefore the resulting tree — is identical across filesystems. - Make appendToTree defensive: when a leaf and a branch share a name, merge them by promoting the leaf to a branch and attaching the module doc as a 'main' child, in either order. This keeps the build working even if the sort is removed or the file set changes.
1 parent 19e633f commit 3f9dce7

1 file changed

Lines changed: 29 additions & 2 deletions

File tree

internal/documentation/.vitepress/config.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ function guide() {
293293
}]
294294
}
295295

296-
for (let file of fs.readdirSync(path.join("docs", "api"))) {
296+
for (let file of fs.readdirSync(path.join("docs", "api")).sort()) {
297297
file = file.replace(".md", "");
298298
const treePath = file.replace("module-", "").split("_");
299299
appendToTree(tree, file, treePath, 0);
@@ -302,8 +302,21 @@ function guide() {
302302
function appendToTree(tree, file, treePath, index) {
303303
// If it's the last leaf
304304
if (treePath.length - 1 === index) {
305+
const text = treePath[index].replace("module-", "");
306+
// If a branch with the same name was already created (e.g. processing
307+
// "@ui5_logger_Logger" before "module-@ui5_logger"), attach this leaf
308+
// as "main" inside it instead of pushing a duplicate sibling.
309+
for (const treeItem of tree.items) {
310+
if (treeItem.text === text && !treeItem.link) {
311+
treeItem.items.unshift({
312+
text: "main",
313+
link: "/api/" + file
314+
});
315+
return;
316+
}
317+
}
305318
tree.items.push({
306-
text: treePath[index].replace("module-", ""),
319+
text,
307320
link: "/api/" + file
308321
});
309322
return;
@@ -314,6 +327,20 @@ function guide() {
314327
// Checks if the leaf does already exist and adds new leafs to it
315328
for (const treeItem of tree.items) {
316329
if (treeItem.text === treePath[index]) {
330+
// If the existing entry is a leaf (no items) — created earlier from a
331+
// "module-..." file with the same name as this branch — promote it to
332+
// a branch and move the existing leaf to "main" inside it, so the new
333+
// child can be attached. Without this the recursive push would crash
334+
// on the missing items array (filesystem-order-dependent on Linux CI).
335+
if (!treeItem.items) {
336+
const existingLink = treeItem.link;
337+
delete treeItem.link;
338+
treeItem.collapsed = treeItem.text !== "@ui5";
339+
treeItem.items = [{
340+
text: "main",
341+
link: existingLink
342+
}];
343+
}
317344
appendToTree(treeItem, file, treePath, index+1);
318345
found = true;
319346
break;

0 commit comments

Comments
 (0)