Skip to content

Catch serial.tools.list_ports import exception for unsupported platforms - #970

Open
sqbi-q wants to merge 1 commit into
meshtastic:masterfrom
sqbi-q:master
Open

Catch serial.tools.list_ports import exception for unsupported platforms#970
sqbi-q wants to merge 1 commit into
meshtastic:masterfrom
sqbi-q:master

Conversation

@sqbi-q

@sqbi-q sqbi-q commented Aug 10, 2026

Copy link
Copy Markdown

My fix for #969.

Summary by CodeRabbit

  • Bug Fixes
    • Improved application startup when serial port discovery is unavailable.
    • Added clearer diagnostic messages for serial port detection errors and warnings.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

serial.tools.list_ports now loads conditionally. Import failures produce diagnostic messages instead of preventing meshtastic.util from loading.

Changes

Serial port import handling

Layer / File(s) Summary
Guard serial port import
meshtastic/util.py
The serial.tools.list_ports import now catches ImportError and prints error and platform-related warning messages.

Estimated code review effort: 2 (Simple) | ~5 minutes

Suggested reviewers: ianmcorvidae

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: handling the import exception for serial.tools.list_ports on unsupported platforms.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@meshtastic/util.py`:
- Around line 22-27: Initialize list_ports to None in the ImportError handler,
then update findPorts() to return an empty list before calling
list_ports.comports() when the fallback is unavailable. Preserve normal port
discovery when the import succeeds, and add a regression test covering the
failed-import path.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 259d213f-e2ae-44d0-8fef-3c2de5187898

📥 Commits

Reviewing files that changed from the base of the PR and between ae71047 and e9ab622.

📒 Files selected for processing (1)
  • meshtastic/util.py

Comment thread meshtastic/util.py
Comment on lines +22 to +27
try:
import serial.tools.list_ports # type: ignore[import-untyped]
except ImportError as e:
print("Error: ", e)
print("Error: Cannot list ports on this platform")
print("Warning: Skipping import serial.tools.list_ports")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Define a fallback for failed imports.

When this import raises ImportError, the handler does not initialize a usable fallback. However, findPorts() still calls serial.tools.list_ports.comports() at Line 162. On an unsupported platform, findPorts() can raise NameError or AttributeError instead of returning no ports.

Store a sentinel such as list_ports = None, check it at the start of findPorts(), and return an empty list before accessing the unavailable module. Add a regression test for the failed-import path.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@meshtastic/util.py` around lines 22 - 27, Initialize list_ports to None in
the ImportError handler, then update findPorts() to return an empty list before
calling list_ports.comports() when the fallback is unavailable. Preserve normal
port discovery when the import succeeds, and add a regression test covering the
failed-import path.

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