Skip to content

Hint at common mistakes with a docs link (pitfalls) - #302

Merged
vito merged 4 commits into
mainfrom
pitfall-diagnostics
Nov 12, 2026
Merged

vito merged 4 commits into
mainfrom
pitfall-diagnostics

Conversation

@vito

@vito vito commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

Dang now recognizes a few common mistakes, which it calls pitfalls: // comments, &&/||, for/while loops, single-brace records and truthiness. The diagnostic for one carries a one-line hint and a link to the docs section that covers it. This is a language feature, so it shows up everywhere a diagnostic does: CLI errors, analysis diagnostics (and so analysis.Render, which the engine's DangCheck/DangEval use), the LSP, the REPL and the playground.

Error: syntax error: `//` is not a comment
  --> main.dang:2:17
     |
   2 | let retries = 3 // before giving up
                       ^^^^^^^^^^^^^^^^^^^
     |
     = hint: comments start with `#`
     = docs: https://danglang.org/syntax#comments
main.dang:2:5: error: condition must be Boolean!, got [Int!]!
  hint: there is no truthiness: test explicitly, e.g. `x != null` or `!xs.isEmpty`
  docs: https://danglang.org/control-flow#if-else

How it works

pkg/dang/pitfalls.go holds a small registry, Pitfalls. Each entry has an ID, a hint, a docs URL, and a detector:

  • Syntax: checked against the PEG parser's failure offset and the source text, before tree-sitter runs. That means it works without cgo, which is how the engine builds Dang. When a syntax pitfall matches, its message replaces the parser's. A small lexer (codeMask) keeps detectors off comments and string text for every quote kind of Dang 2.5 ("…", """…""", '…', '''…''', backticks). The contents of a ${…} interpolation count as code.
  • Type: NewInferError matches typed errors (notBooleanErr, nameNotFoundError, notAFunctionError). The original message stays and the hint is added to it.

A pitfall never changes semantics and never hides an error: it either adds a hint, or it replaces a parse error's message with a more specific one at the same failure.

Docs links point at explicit {#tag} headings, so rewording a heading doesn't break them. Three of the five tags (comments, logical, loop) are the slugs the live site already serves. record-literals and if-else are new, so those two links land at the top of their page until the docs redeploy. TestPitfallsAreDocumented checks that every link names a tagged heading in docs/lit. To support that, dagger.toml now includes docs/lit/** in the Go checks.

The pitfalls

ID Detected from Example
line-comment parse failure at // let x = 1 // one
logical-operators parse failure at &&/||; also &&x at the start of a line (it parses as &(&x)) a && false
loop-keywords parse failure in a for/while header (not in Dang < 2.1); also "for" not found for x in xs {
single-brace-record parse failure at the value of { name: value where an expression starts let p = { name: "a" }
truthiness a non-Boolean condition, logical operand or ! operand if (xs) { … }

There's no pitfall for interpolation. Since #300, "…" interpolates, and an undefined ${NAME} in one already gets its own hint ("to write ${NAME} as text, single-quote the string or escape it as ${NAME}"). That hint could become a pitfall later to gain a docs link, but that would reword #300's message, so it isn't in this PR.

Tests

TestPitfalls (pkg/dang/analysis/pitfalls_test.go) is a table: source goes in, rendered diagnostics come out. It has 50 rows: 21 positive and 29 negative. Negative rows are code that looks like a pitfall but isn't one: floor division n // 2, // and && inside double-quoted, single-quoted and backtick strings (including ones that interpolate), { count: int = 1 }, for in Dang 2.0, ${while} in a string (which has #300's own hint), Decoded conditions, and so on. Positive rows include a && inside an interpolation and a // after an interpolating string. A valid negative row must produce no diagnostics at all. An invalid one may error, but not as a pitfall.

Golden fixtures show the CLI rendering:

  • tests/errors/pitfall_line_comment.dang is new.
  • The for_removed and condition_* goldens now include the hint.

TestPitfalls and TestUnexpectedSyntaxError pass with both CGO_ENABLED=0 and CGO_ENABLED=1. I ran those locally; the repo's Go checks only run with cgo (see below).

Last commit: replace the parser dump (droppable)

"errors: name the token a parse failed at" is separate on purpose so it can be dropped on its own. Without cgo, a parse error that isn't a pitfall used to be pigeon's raw no match found, expected: "!", "#", … dump. It is now syntax error: unexpected 'x' at the token where the parser failed, in the same shape as the cgo output. With cgo, it also stands in when tree-sitter finds nothing to report; tree-sitter's diagnosis still comes first, using #300's choice of the error nearest where the PEG failed. Nothing else depends on this commit.

Remaining differences between the builds:

  • Pitfalls read the same in both.
  • Other parse errors don't. With cgo, tree-sitter describes them, e.g. missing closing ')' or invalid expression after '=', sometimes at a wider span. Without cgo, the message names the token the parser stopped at, e.g. unexpected 'print'.

CI

The Go checks (go/packages/tests/test) only build with cgo, since the toolchain sets CC=clang. So the path the engine uses isn't covered by CI. Adding a nocgo run would take a check that runs CGO_ENABLED=0 go test ./pkg/dang/... ./pkg/lsp (for example, a @check on Test in .dagger/main.dang). ./tests should stay cgo-only, because its syntax-error goldens record the tree-sitter messages.

Docs

  • The testing skill now has a "Pitfalls" section: where pitfalls live, how each kind is detected, and how to add one along with its table rows.
  • docs/lit says outright that // is not a comment and that there is no &&/||.

@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Deploying dang with  Cloudflare Pages  Cloudflare Pages

Latest commit: 385a331
Status: ✅  Deploy successful!
Preview URL: https://b284a796.dang-3kk.pages.dev
Branch Preview URL: https://pitfall-diagnostics.dang-3kk.pages.dev

View logs

vito added 4 commits November 12, 2026 18:04
Give the sections pitfall diagnostics link to explicit tags, so their
anchors survive a heading's rewording. Each tag but if-else and
record-literals is the slug the live site already serves. Also say
outright that `//` is not a comment and that there is no `&&` or `||`.

Signed-off-by: Alex Suraci <[email protected]>
Add a small registry of pitfalls (pkg/dang/pitfalls.go): mistakes Dang
can recognize precisely, each with an ID, a one-line hint and a link to
the docs. An error that is one carries it, and the CLI, the analysis
package (and so analysis.Render), the LSP, the REPL and the playground
show the hint and link beside the error.

- line-comment, logical-operators, loop-keywords, single-brace-record:
  detected at a parse failure from the PEG parser's failure offset and
  the source text, so they work without tree-sitter (the engine builds
  without cgo, where a parse error is otherwise a raw parser dump). They
  replace the parser's message. A small lexer (codeMask) keeps them off
  comments and string text, every quote kind of Dang 2.5 included; an
  interpolation counts as code.
- loop-keywords also matches `for`/`while` that parsed as a call to an
  undefined name, logical-operators a `&&` starting a line (which parses
  as a reference to a reference), and truthiness a non-Boolean
  condition or logical operand: detected in NewInferError from typed
  errors, keeping the message.

Each leaves look-alike code alone: Python's floor division (`n // 2`),
a declaration with a misspelled type in a block (`{ count: int = 1 }`),
a nullable Boolean, Decoded data or a function as a condition, and
`for` loops before Dang 2.1.

A pitfall only adds guidance: no semantics change. TestPitfalls
(pkg/dang/analysis) is the table of positive and negative cases, run in
both builds; TestPitfallsAreDocumented checks each docs link names a
tagged heading in docs/lit, which the Go checks now include.

Signed-off-by: Alex Suraci <[email protected]>
Describe where pitfalls live, how each kind is detected, and how to add
one with its test rows.

Signed-off-by: Alex Suraci <[email protected]>
Without cgo there is no tree-sitter recovery, so a parse error that is
no pitfall was the parser's raw message, listing every token it would
have taken. That is what the engine shows, since it embeds Dang without
cgo. Report it as `syntax error: unexpected 'x'` at the token the parser
failed at instead (the whole word or `//`, when it failed inside one),
in the same shape as a tree-sitter diagnosis. With cgo, it stands in
when tree-sitter finds nothing to diagnose.

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the pitfall-diagnostics branch from 5e41219 to 385a331 Compare November 12, 2026 18:13
@vito
vito added this pull request to stack #304 November 12, 2026 18:19
@vito
vito merged commit 1313b09 into main Nov 12, 2026
9 checks passed
vito added a commit to dagger/dagger that referenced this pull request Nov 13, 2026
Pulls in pitfall diagnostics (vito/dang#302) and tree-sitter compiled
to WebAssembly and run by wazero (vito/dang#303, #307): Dang no longer
needs cgo anywhere, and its highlighter (dang.Highlight) is usable from
any binary.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Nov 13, 2026
Pulls in pitfall diagnostics (vito/dang#302), tree-sitter compiled to
WebAssembly and run by wazero (vito/dang#303, #307): Dang no longer needs
cgo anywhere, and its highlighter (dang.Highlight) is usable from any
binary. Also checker diagnostics fixes (vito/dang#309): GraphQL object list
hints suggest `value.{{id}}`, and a failed declaration no longer cascades
"not found" errors through its uses.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Nov 13, 2026
Pulls in pitfall diagnostics (vito/dang#302), tree-sitter compiled to
WebAssembly and run by wazero (vito/dang#303, #307): Dang no longer needs
cgo anywhere, and its highlighter (dang.Highlight) is usable from any
binary. Also checker diagnostics fixes (vito/dang#309): GraphQL object list
hints suggest `value.{{id}}`, and a failed declaration no longer cascades
"not found" errors through its uses.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit to dagger/dagger that referenced this pull request Nov 13, 2026
Pulls in pitfall diagnostics (vito/dang#302), tree-sitter compiled to
WebAssembly and run by wazero (vito/dang#303, #307): Dang no longer needs
cgo anywhere, and its highlighter (dang.Highlight) is usable from any
binary. Also checker diagnostics fixes (vito/dang#309): GraphQL object list
hints suggest `value.{{id}}`, and a failed declaration no longer cascades
"not found" errors through its uses.

Signed-off-by: Alex Suraci <[email protected]>
vito added a commit that referenced this pull request Nov 13, 2026
Hints for common mistakes get a section, with two errors as printed.
Faster Dagger modules, failed declarations reported once and the GraphQL
object list hint join the smaller changes. Go embedders learn that Dang no
longer needs cgo, and about LoadTreeSitter. The lede and sources cover
them, verified at 2918064.

Signed-off-by: Alex Suraci <[email protected]>
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.

1 participant