From 9277c26753f5f0017a7a7d556252435755a8c239 Mon Sep 17 00:00:00 2001 From: hub-mayank <229884081+hub-mayank@users.noreply.github.com> Date: Fri, 4 Dec 2026 09:00:29 +0530 Subject: [PATCH] feat: publish FAQ structured data for install errors (#307) --- scripts/build_pages.py | 28 ++++++++++++++++++++++------ tests/test_notes_on_site.py | 4 ++-- tests/test_package_jsonld.py | 27 ++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/scripts/build_pages.py b/scripts/build_pages.py index 6d8cf67..51760d2 100644 --- a/scripts/build_pages.py +++ b/scripts/build_pages.py @@ -7,7 +7,7 @@ robots.txt naming the sitemap. Each page shows the package's current notes from notes_dir (default: the repository's notes/) and, when it has a gap, an "Add a note" link. A `bundled` cell says which version Pyodide ships (from the committed lock files, see pyodide_locks.py). Runs at publish time; the files are not -committed. The pages are plain HTML (no JavaScript; one JSON-LD data block) and reuse site/style.css, so +committed. The pages are plain HTML (no JavaScript; JSON-LD data blocks) and reuse site/style.css, so search engines can find "does have ARM wheels". """ @@ -277,6 +277,10 @@ def json_ld(name: str, version: str, url: str, description: str) -> str: } if version: data["version"] = version + return json_ld_block(data) + + +def json_ld_block(data: dict) -> str: text = json.dumps(data, ensure_ascii=False, indent=2) text = text.replace("<", "\\u003c").replace(">", "\\u003e").replace("&", "\\u0026") return f' \n' @@ -303,7 +307,7 @@ def gap_platforms(package: dict, targets: list[dict], statuses: tuple[str, ...] def answer_html( name: str, version: str, python: str, example: str, platforms: list[str], *, builds: bool -) -> str: +) -> tuple[str, str]: """The short answer for someone who arrived with the install's error ("Failed building wheel for NAME", or pip's "No matching distribution found" where there's no sdist to build): why it happens, where it does for this package (`platforms`, on a regular Python), the ways out, and a command for `python` on `example`, a platform where that @@ -340,7 +344,7 @@ def answer_html( else: tool_intro = "To find every dependency like this before you install, on the platforms you ship to:" command = f"uvx wheelgap requirements.txt -p {python} -t {example}" - return ( + section = ( '
\n' "

Why the install fails, and the fix

\n" f"

{why}

\n" @@ -349,6 +353,18 @@ def answer_html( f"
{e(command)}
\n" "
\n" ) + error = "Failed building wheel for" if builds else "No matching distribution found for" + question = f"Why does pip install {name} fail with '{error} {name}'?" + faq = { + "@context": "https://schema.org", + "@type": "FAQPage", + "mainEntity": [{ + "@type": "Question", + "name": question, + "acceptedAnswer": {"@type": "Answer", "text": html.unescape(f"{why} {ways}")}, + }], + } + return section, json_ld_block(faq) def add_note_url(name: str, platforms: list[str], today: str, exists: bool) -> str: @@ -728,7 +744,7 @@ def page( else: title = f"{name} wheel coverage: Linux, Alpine, macOS, Windows, ARM, Pyodide" heading = f"{name} wheel coverage" - answer = "" + answer = faq = "" if cells: # The example command names a Python and platform that really has the page's error: the Python the summary # answers for when it has one, else the newest that does, and that Python's first such platform. @@ -738,7 +754,7 @@ def page( example_python = preferred if preferred in pythons else max(pythons, key=install_results.python_key) example_platform = next(pl for pl in PLATFORM_NAMES if (example_python, pl) in kind) where = [pl for pl in PLATFORM_NAMES if any(p == pl for _, p, _ in cells)] - answer = answer_html(name, version, example_python, example_platform, where, builds=builds) + answer, faq = answer_html(name, version, example_python, example_platform, where, builds=builds) campaign_links = [] for status, campaign in campaigns or []: link = f'{e(campaign["title"])}' @@ -875,7 +891,7 @@ def cell(py: str, pl: str) -> str:

Package JSON ยท All {len(scan["packages"]):,} packages

{build_campaigns.FOOTER} -{json_ld(name, version, url, summary)} +{json_ld(name, version, url, summary)}{faq} """ diff --git a/tests/test_notes_on_site.py b/tests/test_notes_on_site.py index fa73de1..a4423b2 100644 --- a/tests/test_notes_on_site.py +++ b/tests/test_notes_on_site.py @@ -247,8 +247,8 @@ def test_current_note_is_shown_escaped_with_backticks_as_code(self): "<script>alert(1)</script> & go.

", page, ) - # The only script tag is the JSON-LD data block (#100), and the note's text isn't in one. - self.assertEqual(page.count("')) self.assertIn('") + software, faq = [json.loads(block) for block in blocks(page)] + assert software["@type"] == "SoftwareSourceCode" + assert faq["@type"] == "FAQPage" + question, = faq["mainEntity"] + assert question["@type"] == "Question" + assert question["name"] == f"Why does pip install a&b fail with '{error} a&b'?" + section = page.split('id="why-it-fails">', 1)[1].split("", 1)[0] + paragraphs = re.findall(r"

(.*?)

", section)[:2] + assert question["acceptedAnswer"] == {"@type": "Answer", "text": html.unescape(" ".join(paragraphs))} + assert all("<" not in block for block in blocks(page)) + + +def test_no_faq_on_fully_covered_package(): + page = _page(["wheel", "wheel"], ["3.13 linux-x86_64", "3.13 win-arm64"]) + assert [json.loads(block)["@type"] for block in blocks(page)] == ["SoftwareSourceCode"] + + def test_open_graph_and_twitter_card_tags(): page = build(scan()) @@ -75,8 +96,8 @@ def test_a_script_end_tag_in_the_name_cannot_end_the_block(): name = "evil&x" page = build(scan(name=name)) assert "") == 1 # only the block's own end tag - (block,) = blocks(page) + assert page.count("") == 2 # only the two blocks' own end tags + block = blocks(page)[0] assert "<" not in block and ">" not in block and "&" not in block data = json.loads(block) assert data["name"] == name