Skip to content

ci(lint): lint against forbidden patterns - #3549

Open
moigagoo wants to merge 25 commits into
masterfrom
lint/forbidden-call
Open

moigagoo wants to merge 25 commits into
masterfrom
lint/forbidden-call

Conversation

@moigagoo

@moigagoo moigagoo commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Add error-level checks for forbidden patterns:

  • project-wide
    • echo
    • sleepAsync
  • in tests:
    • wait
    • withTimeout
    • check false

Uses: https://iftech.github.io/nim-lint/stock_checks.html#patterns

@greptile-apps

greptile-apps Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

[Low impact] The changes affect the repository's CI lint results, not runtime behavior.

The PR is not ready to merge because the new error-level checks still reject existing code.

RetriggerView in Greptile

Findings

  1. P1 Existing calls now fail CI ▶
  2. P2 Comments become lint errors ▶
Fix with agent prompt
### Issue 1
linter.nim:85-89
The new error-level checks reject unchanged calls, including `sleepAsync(checkResponse.retryAfter)` in `libp2p/autotls/acme/api.nim`, `echo PBCapParserHeader` in `tools/pbcap_parser.nim`, and `readFut.withTimeout(100.milliseconds)` in the tests. None is excluded, and CI runs this configuration. Keep existing calls advisory, or migrate them before making these checks errors.

### Issue 2
linter.nim:undefined-93
The `echo` patterns reject ordinary comments and strings, not just calls. For example, `echo message without change` in a comment in `libp2p/protocols/kademlia/put.nim` matches the first pattern. Contributors would have to rewrite harmless prose to pass lint. Check parsed calls instead of raw text.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (3) · Last reviewed commit: "Fix typo." · Reviewed by Greptile

Comment thread tools/forbidden_call_linter.nim Resolved
Comment thread tools/forbidden_call_linter.nim Resolved
Comment thread tools/tests/test_forbidden_call.nim Resolved
@moigagoo
moigagoo marked this pull request as draft November 27, 2026 09:54
@moigagoo moigagoo changed the title feat(lint): flag literal timeout calls ci(lint): flag literal timeout calls Nov 27, 2026
@vladopajic

Copy link
Copy Markdown
Member

@moigagoo let's implement this one with regexp linter seems like really nice usecase for it

@moigagoo
moigagoo marked this pull request as ready for review December 5, 2026 08:56
@moigagoo

moigagoo commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

/review

@moigagoo moigagoo changed the title ci(lint): flag literal timeout calls ci(lint): lint against forbidden patterns Dec 5, 2026
Comment thread linter.nim Outdated
Comment on lines +85 to +89
step "Forbidden patterns accross project":
check patterns:
severity = error
settings:
forbidden = [r"\bsleepAsync\s+\S+\b", r"\bsleepAsync\(\S+\)"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Existing calls now fail CI

The new error-level checks reject unchanged calls, including sleepAsync(checkResponse.retryAfter) in libp2p/autotls/acme/api.nim, echo PBCapParserHeader in tools/pbcap_parser.nim, and readFut.withTimeout(100.milliseconds) in the tests. None is excluded, and CI runs this configuration. Keep existing calls advisory, or migrate them before making these checks errors.

Prompt To Fix With AI
This is a comment left during a code review.
Path: linter.nim
Line: 85-89

Comment:
**Existing calls now fail CI**

The new error-level checks reject unchanged calls, including `sleepAsync(checkResponse.retryAfter)` in `libp2p/autotls/acme/api.nim`, `echo PBCapParserHeader` in `tools/pbcap_parser.nim`, and `readFut.withTimeout(100.milliseconds)` in the tests. None is excluded, and CI runs this configuration. Keep existing calls advisory, or migrate them before making these checks errors.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Comment thread linter.nim
check patterns:
severity = error
settings:
forbidden = [r"\becho\s+\S+\b", r"\becho\(\S+\)"]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Comments become lint errors

The echo patterns reject ordinary comments and strings, not just calls. For example, echo message without change in a comment in libp2p/protocols/kademlia/put.nim matches the first pattern. Contributors would have to rewrite harmless prose to pass lint. Check parsed calls instead of raw text.

Prompt To Fix With AI
This is a comment left during a code review.
Path: linter.nim
Line: 93

Comment:
**Comments become lint errors**

The `echo` patterns reject ordinary comments and strings, not just calls. For example, `echo message without change` in a comment in `libp2p/protocols/kademlia/put.nim` matches the first pattern. Contributors would have to rewrite harmless prose to pass lint. Check parsed calls instead of raw text.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor

OpenCodeReview · Complete review: 1 of 1 selected files covered

6 findings.

  • Successfully posted inline: 6 comment(s)
Review info

Reviewed commits: 3f6ab0814f91da80f7b42ce87f7e750eedbe3802 → a754854b1039ddb8d4b4605634e0144b05386c01

File Result
linter.nim Reviewed

Model: qwen4-max
Review generated in: 3m 42s

Comment thread linter.nim Resolved
Comment thread linter.nim
check patterns:
severity = error
settings:
forbidden = [r"\bsleepAsync\s+\S+\b", r"\bsleepAsync\(\S+\)"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

bug · high
This project-wide ban on sleepAsync (severity = error, no ignoredPatterns) matches dozens of existing, legitimate uses: e.g. libp2p/utils/heartbeat.nim (await sleepAsync(interval)), libp2p/connmanager.nim, libp2p/transports/tcptransport.nim (accept backoff), plus interop/, simulation/, examples/ and many tests. None of these carry #nolint, so the lint job will fail as soon as this step is enabled. Retry backoffs and heartbeat timers are valid sleeps (the project convention allows a sleep with an adjacent explanatory comment), which a blanket regex cannot distinguish. Scope this check to tests (or add ignores/an allow mechanism for justified sleeps) and land the corresponding code cleanups together with the rule.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's a valid concern. A better pattern would be to ban hardcoded literal sleeps, e.g. sleepAsync(10.second).

@vladopajic vladopajic Dec 5, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

we can have it as a hint, just o hint developer with message that it is not advised to use sleepAsync, especailly when it is used to fix race conditions.

we can promote it later to error - maybe error in tests, hint in library.

Comment thread linter.nim
check patterns:
severity = error
settings:
forbidden = [r"\becho\s+\S+\b", r"\becho\(\S+\)"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

bug · high
The project-wide echo ban flags examples/ and tools/, where console output is the intended behavior: e.g. examples/directchat.nim, examples/tutorial_6_peerscoring.nim, tools/pbcap_parser.nim, tools/markdown_builder.nim, tools/gen_multicodec.nim. If the patterns check scans comments/strings, it also hits doc comments such as echo value in libp2p/utils/opt.nim. Add ignoredPatterns = ["./examples/**/*", "./tools/**/*", ...] (consistent with the other steps) or the lint run will fail on existing code.

Comment thread linter.nim
forbidden = [r"\becho\s+\S+\b", r"\becho\(\S+\)"]

step "Forbidden patterns in tests":
ignoredPatterns = ["./libp2p/**/*", "./examples/**/*", "./linter.nim"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

bug · medium
This step is named "in tests" but its ignore list only excludes libp2p/, examples/ and linter.nim, so it also applies to non-test code in tools/, interop/, and simulation/. If the intent is tests-only, restrict sources/ignores accordingly (e.g. also ignore ./tools/**/*, ./interop/**/*, ./simulation/**/*); otherwise rename the step to reflect its actual scope.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Some of the paths you listed do not contain .nim files and the checks only apply to .nim files.

But you're right about tools and examples.

Comment thread linter.nim
check patterns:
severity = error
settings:
forbidden = [r"\bwait\s+\S+\b", r"\bwait\(\S+\)"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

bug · high
This error-severity ban on wait flags legitimate, condition-aware chronos synchronization APIs that are the recommended alternative to sleepAsync: AsyncEvent.wait() (note \S+ matches the closing )), Future.wait(timeout), and wait(fut, timeout). These are used in 60+ existing test files (e.g. tests/libp2p/discovery/test_mdns.nim, tests/libp2p/muxers/test_yamux.nim, tests/libp2p/protocols/waggle/test_waggle.nim) and in interop/ (e.g. interop/unified_testing.nim). The \bwait\s+\S+\b variant also matches non-call code such as the wait = 10.milliseconds parameter of the shared tryPublish helper (tests/libp2p/pubsub/utils.nim:446) and English prose like "wait for …" in comments/test names if the check scans raw text. As written, this rule fails CI on correct code and discourages the very event-driven waiting the project conventions require. If the intent is to ban a specific blocking/polling helper, target its exact qualified name instead of the generic word wait.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

so this pattern needs to match .wait( {argument timeout} ) for example .wait(1.second) but it should ignore .wait() without argument, these are falls positives, such as AsyncEvent.wait() calls

Comment thread linter.nim
check patterns:
severity = error
settings:
forbidden = [r"\bwithTimeout\s+\S+\b", r"\bwithTimeout\(\S+\)"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

bug · high
Despite the step name, ignoredPatterns = ["./libp2p/**/*", ...] does not exclude ./tests/libp2p/**/*, and ~20 existing test files use withTimeout(...) (e.g. tests/libp2p/muxers/test_mplex.nim, tests/libp2p/stream/test_bufferstream.nim, tests/libp2p/test_conn_manager.nim, tests/tools/test_sync.nim), all matching \bwithTimeout\(\S+\) at error severity — the lint job will fail with no accompanying code changes in this PR. Also, the wait regex \bwait\s+\S+\b matches ordinary prose like "wait for ..." in comments/strings if the check is text-based. Either migrate the existing tests to the preferred helper in the same PR or narrow the patterns/scope.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

20 existing test files use withTimeout(...)

most of those cases seem to use it unnecessary.

there could be some specific cases could be #nolint, for example

check not (await connMngr.getIncomingSlot().withTimeout(10.millis))

^ because awaiting on this feature will never complete, so we need to cancel it.

but it might be nicer if there us utility that is used in tests:

check (await connMngr.getIncomingSlot()).isBlockedAfter(10.millis)

which can do something like this:

proc isBlockedAfter(fut: Future, timeout: Duration): bool = 
  await asyncSleep(timeout) #nolint
  return not fut.completed # or something better i am not sure from top of my head

the idea is to hide forbidden pattern in utility which will be only once ignored with #nolint and expos utility as nicer more deliberate way for asserting invariant we actually want to assert - in named utility.

This branch has not been deployed

No deployments
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