Fix: relative -d/--project-dir path causes FileNotFoundError in device monitor#5470
Merged
ivankravets merged 1 commit intoJul 8, 2026
Merged
Conversation
`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.
Member
|
Thanks for the PR! |
ivankravets
added a commit
that referenced
this pull request
Jul 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Closes #5439.
Running
pio device monitor -d <relative-path>can crash with aFileNotFoundErrorwhen the current platform's monitor filter (e.g. espressif32's exception decoder)
changes the working directory internally.
Root cause
The
-d/--project-diroption ondevice_monitor_cmdusesclick.Path(...)withoutresolve_path=True, so a relative path stays relative.device_monitor_cmditself callsfs.cd(project_dir)once; whenload_build_metadata()later callsfs.cd()again fromwithin 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=Trueto theclick.Path(...)type for-dondevice_monitor_cmd, so the path is resolved to an absolute path before anyfs.cd()calls happen. Checked all other
-d/--project-dirdefinitions 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
tests/commands/test_device_monitor.py(3 tests), reusing the existingclirunner/validate_cliresultfixtures fromtests/conftest.py.FileNotFoundError) and passespost-fix.
black --check,isort --check, andpylint --rcfile=./.pylintrcall clean (10.00/10).