Skip to content

Fix: relative -d/--project-dir path causes FileNotFoundError in device monitor#5470

Merged
ivankravets merged 1 commit into
platformio:developfrom
joshazmy:fix/monitor-relative-project-dir
Jul 8, 2026
Merged

Fix: relative -d/--project-dir path causes FileNotFoundError in device monitor#5470
ivankravets merged 1 commit into
platformio:developfrom
joshazmy:fix/monitor-relative-project-dir

Conversation

@joshazmy

@joshazmy joshazmy commented Jul 5, 2026

Copy link
Copy Markdown

Problem

Closes #5439.

Running pio device monitor -d <relative-path> can crash with a FileNotFoundError
when the current platform's monitor filter (e.g. espressif32's exception decoder)
changes the working directory internally.

Root cause

The -d/--project-dir option on device_monitor_cmd uses click.Path(...) without
resolve_path=True, so a relative path stays relative. device_monitor_cmd itself calls
fs.cd(project_dir) once; when load_build_metadata() later calls fs.cd() again from
within a monitor filter, the still-relative path gets resolved against the new cwd,
duplicating the path segment and producing a non-existent path.

Fix

One-line change: add resolve_path=True to the click.Path(...) type for -d on
device_monitor_cmd, so the path is resolved to an absolute path before any fs.cd()
calls happen. Checked all other -d/--project-dir definitions in the codebase (12 total)
— this fix is scoped to the one command affected; no shared helper was available to reuse
without touching unrelated commands.

Testing

  • Added tests/commands/test_device_monitor.py (3 tests), reusing the existing
    clirunner/validate_cliresult fixtures from tests/conftest.py.
  • Confirmed the new regression test fails pre-fix (FileNotFoundError) and passes
    post-fix.
  • black --check, isort --check, and pylint --rcfile=./.pylintrc all clean (10.00/10).

`device_monitor_cmd`'s `-d/--project-dir` option accepted a relative path
without resolving it to absolute. The command then entered
`with fs.cd(options["project_dir"]):`, changing the working directory into
that relative subdir. Downstream, a platform monitor filter (e.g. the
espressif32 exception decoder) re-enters the same still-relative
`project_dir` via `load_build_metadata()`'s own `fs.cd()`, duplicating the
path segment onto the already-changed cwd and crashing with
FileNotFoundError (platformio#5439).

Add `resolve_path=True` to the click.Path type so the path is resolved to
absolute once, at the point it enters the system, before any fs.cd() chain
runs. This does not change the no-flag default (os.getcwd already returns
absolute) or the already-absolute case (passes through unchanged, modulo
symlink resolution).

Add a regression test mirroring the nested fs.cd() scenario, plus coverage
for the default and already-absolute cases.
@CLAassistant

CLAassistant commented Jul 5, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@ivankravets
ivankravets merged commit a8ff6fe into platformio:develop Jul 8, 2026
30 checks passed
@ivankravets

Copy link
Copy Markdown
Member

Thanks for the PR!

@ivankravets ivankravets added this to the 6.2.0 milestone Jul 8, 2026
ivankravets added a commit that referenced this pull request Jul 8, 2026
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.

Unexpected error occours when specifying a custom directory to device monitor

3 participants