Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and the project uses [Semantic Versioning](https://semver.org/).

## [Unreleased]

### Changed

- When Tktable cannot be loaded, the tests show the error of every try, and print a
`Tktable load report` at the end, with Tcl's stack trace, what Tcl knows about Tktable and the
files in `TKTABLE_LIBRARY`, to find the cause of the flaky failures on Windows.

## [0.3.0] - 2026-11-04

### Added
Expand Down
7 changes: 7 additions & 0 deletions docs/development.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,10 @@ scripts in `.github/scripts/`. All run on pushes to `main`, and all but
[Magicsplat](https://magicsplat.com/tcl-installer)'s Tktable (not on pull requests);
- `docs.yml`: builds the docs, and publishes them to GitHub Pages on pushes to `main`;
- `release.yml`: see [Releasing](releasing.md).

The Windows jobs sometimes fail because Tktable cannot be loaded right after it was installed
(most likely the antivirus of GitHub's runner is still scanning the new files). The tests try 3
times, and print a `Tktable load report` at the end of their output when a try fails. If the job
passes in other runs, it's a flake: rerun the failed jobs from the run's page in the Actions tab
(`Re-run jobs` > `Re-run failed jobs`), or with `gh run rerun <run-id> --failed`. See
[Re-running workflows and jobs](https://docs.github.com/en/actions/how-tos/manage-workflow-runs/re-run-workflows-and-jobs).
70 changes: 63 additions & 7 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
import time
from collections.abc import Iterator
from pathlib import Path

import pytest

Expand All @@ -18,13 +19,18 @@
TKTABLE_REQUIRED = os.environ.get("TKTABLE_REQUIRED") == "1"

#: How many times to try to load Tktable, and how many seconds to wait between tries.
#: On GitHub's Windows runners, Tcl sometimes finds Tktable's pkgIndex.tcl but then can't read it
#: ("couldn't read file ... no such file or directory"), right after Tktable was installed: most
#: likely the antivirus is still scanning the new files. A later try reads it. Only with
#: On GitHub's Windows runners, loading Tktable sometimes fails right after it was installed, most
#: likely while the antivirus is still scanning the new files: Tcl finds pkgIndex.tcl but can't read
#: it ("couldn't read file ... no such file or directory"), or runs it but Tktable isn't provided
#: ("no version of package Tktable provided"). A later try, or a rerun of the job, works. Only with
#: TKTABLE_REQUIRED=1, so a run without Tktable isn't slowed down.
TKTABLE_LOAD_TRIES = 3 if TKTABLE_REQUIRED else 1
TKTABLE_LOAD_RETRY_SECONDS = 2

#: What Tcl said, and what it saw, when Tktable couldn't be loaded; printed once at the end of the
#: test run (see pytest_terminal_summary), because the error alone often doesn't say why.
TKTABLE_LOAD_REPORT: list[str] = []


@pytest.fixture(scope="session")
def session_root() -> Iterator[tkinter.Tk | str]:
Expand Down Expand Up @@ -52,21 +58,71 @@ def session_tktable_error(session_root: tkinter.Tk | str) -> str:
if isinstance(session_root, str):
return session_root

error = ""
errors: list[str] = []
for tries in range(1, TKTABLE_LOAD_TRIES + 1):
try:
load_tktable(session_root)
if errors:
TKTABLE_LOAD_REPORT.extend([f"Tktable loaded at try {tries}, after:", *errors])
return ""
except tkinter.TclError as exc:
error = str(exc)
errors.append(f"try {tries}: {exc}")
# Tcl's stack trace: which command in pkgIndex.tcl failed, e.g. the load of the library.
TKTABLE_LOAD_REPORT.append(
f"Tcl errorInfo at try {tries}:\n{tcl(session_root, 'set ::errorInfo')}"
)
if tries < TKTABLE_LOAD_TRIES:
time.sleep(TKTABLE_LOAD_RETRY_SECONDS)

TKTABLE_LOAD_REPORT.extend(tktable_load_details(session_root))
auto_path = session_root.tk.splitlist(session_root.tk.call("set", "auto_path"))
folders = ", ".join(str(folder) for folder in auto_path)
library = os.environ.get("TKTABLE_LIBRARY")
details = f"tried {TKTABLE_LOAD_TRIES} times; TKTABLE_LIBRARY={library}; auto_path: {folders}"
return f"{error} ({details})"
details = (
f"TKTABLE_LIBRARY={library}; auto_path: {folders}; see the Tktable load report at the end"
)
# The first error is the real one: later tries can fail just because the first one did.
return f"{'; '.join(errors)} ({details})"


def tcl(tk: tkinter.Tk, script: str) -> str:
"""The result of a Tcl script, or its error, for the report: it must not raise."""
try:
return str(tk.tk.eval(script))
except tkinter.TclError as exc:
return f"error: {exc}"


def tktable_load_details(tk: tkinter.Tk) -> list[str]:
"""What Tcl knows about Tktable, and which files are in TKTABLE_LIBRARY."""
ifneeded = "lmap v [package versions Tktable] {package ifneeded Tktable $v}"
details = [
f"Tcl {tcl(tk, 'info patchlevel')}",
f"Tktable versions known to Tcl: {tcl(tk, 'package versions Tktable')}",
f"package ifneeded scripts: {tcl(tk, ifneeded)}",
# If Tktable's library is listed here, it was loaded, but its init didn't provide Tktable.
f"Loaded libraries: {tcl(tk, 'info loaded')}",
]
library = os.environ.get("TKTABLE_LIBRARY")
if library:
try:
files = [
f"{path} ({path.stat().st_size} bytes)"
for path in sorted(Path(library).rglob("*"))
if path.is_file()
]
details.append("Files in TKTABLE_LIBRARY:\n" + "\n".join(files))
except OSError as exc:
details.append(f"Files in TKTABLE_LIBRARY: cannot be listed: {exc}")
return details


def pytest_terminal_summary(terminalreporter: pytest.TerminalReporter) -> None:
"""Print the Tktable load report once, if there is one, instead of in every failed test."""
if TKTABLE_LOAD_REPORT:
terminalreporter.section("Tktable load report")
for line in TKTABLE_LOAD_REPORT:
terminalreporter.line(line)


@pytest.fixture
Expand Down
Loading