Skip to content

Build and check Swift snippets#42

Open
nickpdemarco wants to merge 2 commits intomainfrom
npd/swift-check
Open

Build and check Swift snippets#42
nickpdemarco wants to merge 2 commits intomainfrom
npd/swift-check

Conversation

@nickpdemarco
Copy link
Member

This PR adds support for checking Swift examples, copying the Rust-mdbook experience as much as possible: lines beginning with # will be omitted from the book output, but are used to compile the examples. Additionally, as a preprocessor step, all { ... } blocks are replaced with { fatalError() }.

# let a = [1, 2, 3]
# let x = 1
var i = 0
while (i != a.count && a[i] != x) {
  i += 1
}

A block marked with swift,ignore will not be built.

The examples can be checked with mdbook build. Several examples were slightly changed to pass the check. Without the two # lines above, the following error is emitted.

$ mdbook build better-code 
2026-02-03 16:23:39 [INFO] (mdbook::book): Book building has started
2026-02-03 16:23:39 [INFO] (mdbook::book): Running the html backend
2026-02-03 16:23:40 [INFO] (mdbook::book): Running the swift-test backend
2026-02-03 16:23:40 [INFO] (mdbook::renderer): Invoking the "swift-test" renderer
============================================================
Swift Code Example Testing
============================================================

Results: 19 passed, 1 failed, 20 total

------------------------------------------------------------
FAILURES:
------------------------------------------------------------

FAIL: chapter-2-contracts.md:238
/var/folders/zm/ll4504dd6dgbvy20y9ls2_hr0000gq/T/tmpwu0wf27f.swift:2:13: error: cannot find 'a' in scope
1 | var i = 0
2 | while (i != a.count && a[i] != x) {
  |             `- error: cannot find 'a' in scope
3 |   i += 1
4 | }

/var/folders/zm/ll4504dd6dgbvy20y9ls2_hr0000gq/T/tmpwu0wf27f.swift:2:24: error: cannot find 'a' in scope
1 | var i = 0
2 | while (i != a.count && a[i] != x) {
  |                        `- error: cannot find 'a' in scope
3 |   i += 1
4 | }

/var/folders/zm/ll4504dd6dgbvy20y9ls2_hr0000gq/T/tmpwu0wf27f.swift:2:32: error: cannot find 'x' in scope
1 | var i = 0
2 | while (i != a.count && a[i] != x) {
  |                                `- error: cannot find 'x' in scope
3 |   i += 1
4 | }

============================================================
2026-02-03 16:23:44 [ERROR] (mdbook::renderer): Renderer exited with non-zero return code.
2026-02-03 16:23:44 [ERROR] (mdbook::utils): Error: Rendering failed
2026-02-03 16:23:44 [ERROR] (mdbook::utils):    Caused By: The "swift-test" renderer failed

@dabrahams
Copy link
Collaborator

SwiftyLab/setup-swift is the reliable Swift installation action we've been using for Hylo, FWIW.

# Preprocessor to hide `# ` lines in Swift code blocks (like mdbook does for Rust)
# Only strips lines for HTML output; swift-test backend sees the full code
[preprocessor.swift-hidden]
command = "python3 scripts/mdbook-swift-hidden.py"
Copy link
Member

Choose a reason for hiding this comment

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

Can you use the built-in mdbook support?

[output.html.code.hidelines]
swift = "#"

Copy link
Collaborator

Choose a reason for hiding this comment

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

Swift has # directives so might need a different character

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems to work! Huge simplification, thanks for pointing that out.

[output.html.print]
enable = true

# Swift code example testing backend
Copy link
Collaborator

Choose a reason for hiding this comment

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

This line isn't adding anything

result_lines = []
in_swift_block = False

for line in content.split("\n"):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Raw loop should be algorithms and/or list comprehensions

"""Process all chapters in the book, stripping hidden lines."""

def process_item(item: dict) -> dict:
"""Recursively process book items."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Content free comment



def process_book(book: dict) -> dict:
"""Process all chapters in the book, stripping hidden lines."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Content free comment

Comment on lines +104 to +105
# We support all renderers, but only modify content for html
sys.exit(0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What the heck is this??

@dabrahams
Copy link
Collaborator

I got partway through reading the actual code which began to smell sloppy, and when I reached the point that it decided there was a case it wasn't going to support, and it would just exit with success (the output is not HTML), that was an almost certain sign that this was generated with AI and not carefully reviewed. Please forgive me if I've gotten that wrong, but Everything we check in here, even tooling should uphold the principles of better code. that includes real meaningful contract documentation for example.

@nickpdemarco
Copy link
Member Author

nickpdemarco commented Feb 4, 2026

Thanks for that feedback @dabrahams - I'll take a closer look and address what you've mentioned. In hindsight I should have made this a draft PR; it wasn't my intention to request a review, just share progress.

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.

3 participants