Skip to content

fix: resolve Sphinx diagram zoom links relative to the current page - #650

Merged
fgmacedo merged 2 commits into
fgmacedo:developfrom
rastagan-git:fix/sphinx-diagram-relative-target
Oct 11, 2026
Merged

fgmacedo merged 2 commits into
fgmacedo:developfrom
rastagan-git:fix/sphinx-diagram-relative-target

Conversation

@rastagan-git

Copy link
Copy Markdown
Contributor

Problem

An empty :target: on statemachine-diagram writes a standalone SVG to the build's _images directory, but links to it with a leading slash. When the documentation is hosted below a URL prefix, clicking the diagram navigates outside the documentation deployment.

For example, a page at https://example.com/project/en/stable/guide/overview.html currently links to https://example.com/_images/statemachine-<digest>.svg, although the file belongs under /project/en/stable/_images/.

To reproduce, add this directive to a Sphinx document and build it with html or dirhtml:

.. statemachine-diagram:: tests.examples.traffic_light_machine.TrafficLightMachine
   :target:

Resolve the generated link as if the output were served below /project/en/stable/. The link loses that prefix on the current develop branch.

Changes

  • Obtain the page URI from the active Sphinx builder and use Sphinx's relative_uri helper to link to the generated SVG.
  • Preserve explicit target URLs and the existing SVG filename/content behavior.
  • Add six real Sphinx-build regressions: html and dirhtml, each with a root index, a nested document, and a nested index. Assert that the link resolves inside the deployment prefix and that the generated SVG exists.
  • Update existing target assertions and their mocked builder context.
  • Regenerate the README traffic-light image as required by the repository's generate-images pre-commit hook; the states, transitions, and labels are unchanged.

Validation

Ubuntu / Python 3.14.7 / Sphinx 9.1.0 / pydot 4.0.1 / Graphviz 2.43.0:

  • With the original return logic restored, all six new regressions fail because the generated URL drops the deployment prefix.
  • uv run pytest tests/test_contrib_diagram.py --timeout=30 -q --tb=short --cov=statemachine.contrib.diagram.sphinx_ext --cov-report=term-missing --cov-fail-under=100: 149 passed, affected-module line/branch coverage 100%.
  • uv run pytest -n 4 --timeout=30 --cov --cov-report=term-missing --tb=short: 2147 passed, 1 skipped, 44 xfailed, overall line/branch coverage 100%.
  • Ruff lint/format, mypy and Pyright checks passed locally.

The real Sphinx builds emit deprecation warnings for BuildEnvironment.app, which this extension already uses; migrating that API is outside this patch. Windows / Python 3.14.4 / Graphviz 16.1.0 also passes the six new builds, but two existing SVG-prefix assertions fail on Graphviz's CRLF output (147 passed, 2 failed in the full diagram file). The Linux run above passes those assertions. The complete supported Python-version matrix remains for CI.

Prepared with AI assistance; reproduction, before/after tests, and scope are documented above.

@codecov

codecov Bot commented Sep 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (78e2b4c) to head (9ac68e0).
⚠️ Report is 2 commits behind head on develop.

Additional details and impacted files
@@            Coverage Diff            @@
##           develop      #650   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           52        52           
  Lines         5572      5597   +25     
  Branches       879       886    +7     
=========================================
+ Hits          5572      5597   +25     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@fgmacedo fgmacedo left a comment •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Hi @rastagan-git, welcome, and thank you for your first contribution to python-statemachine! I appreciate the time you put into this: the reproduction, the before/after tests on both html and dirhtml, and the clear write-up made the review quick.

One change request inline about the singlehtml builder, plus a follow-up idea. I pushed the fix myself in 9ac68e0 to get this merged quickly; see the thread for details.

Comment thread statemachine/contrib/diagram/sphinx_ext.py Outdated
Comment thread statemachine/contrib/diagram/sphinx_ext.py Outdated
The href was computed in the read phase, where `singlehtml` does not yet
know the document and returns a per-file URI, so nested pages ended up
with `../_images/...` from `index.html`. Keep the root-relative href on
the raw node and rewrite it in a `doctree-resolved` handler, once the
builder knows the page's final URI. Covers html, dirhtml and singlehtml.

Signed-off-by: Fernando Macedo <fgmacedo@gmail.com>
@sonarqubecloud

Copy link
Copy Markdown

@fgmacedo
fgmacedo dismissed their stale review October 11, 2026 21:58

Fix pushed in 9ac68e0 on the contributor's branch.

@fgmacedo
fgmacedo merged commit fe5f070 into fgmacedo:develop Oct 11, 2026
10 checks passed
@fgmacedo fgmacedo added this to the 3.2.2 milestone Oct 12, 2026
fgmacedo added a commit that referenced this pull request Oct 13, 2026
Open the notes with a summary and a list of the behavior changes to check
when upgrading, group the fixes by theme, and cite the issue and the PR of
every entry. Add the missing entries for #650 and the GraphvizError
exception change, the rejection of nested callbacks named like an event,
and the events dropped after a top-level final state.

Document Event(delay=..., internal=...) in the events page, the declared
delay in the delayed events section, and the queue being discarded on
termination in the processing model.

Signed-off-by: Fernando Macedo <fgmacedo@gmail.com>
fgmacedo added a commit that referenced this pull request Oct 13, 2026
Open the notes with a summary and a list of the behavior changes to check
when upgrading, group the fixes by theme, and cite the issue and the PR of
every entry. Add the missing entries for #650 and the GraphvizError
exception change, the rejection of nested callbacks named like an event,
and the events dropped after a top-level final state.

Document Event(delay=..., internal=...) in the events page, the declared
delay in the delayed events section, and the queue being discarded on
termination in the processing model.

Signed-off-by: Fernando Macedo <fgmacedo@gmail.com>
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