Preserve user .vscode/extensions.json on IDE regeneration (#5473)#5474
Open
tillig wants to merge 1 commit into
Open
Preserve user .vscode/extensions.json on IDE regeneration (#5473)#5474tillig wants to merge 1 commit into
tillig wants to merge 1 commit into
Conversation
…#5473) Regenerating the VSCode integration overwrote an existing .vscode/extensions.json from a template on every index rebuild, reverting the user's ordering, formatting, and comments. Worse, the merge only stripped full-line // comments before json.loads, so inline or block comments made parsing fail silently (except ValueError: pass) and the user's recommendation entries were dropped entirely. Add a small JSONC helper (comment- and trailing-comma-tolerant parsing plus in-place array insertion) and use it in ProjectGenerator so that, when extensions.json already exists, the required entries are inserted in place without touching anything else. The file is only rewritten when an entry is actually added. When the file is absent, it is still scaffolded from the (now static) template. Add unit tests for the JSONC helper and end-to-end init tests covering the preserve-on-rerun and insert-missing-with-comments cases.
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.
Description
Fixes #5473.
Regenerating the VSCode integration (
pio project init --ide vscode, which the VSCode extension triggers on every IntelliSense index rebuild) unconditionally overwrote an existing.vscode/extensions.jsonfrom a template. This had two consequences:re.sub(r"^\s*//.*$", ...)(full-line//only) and thenjson.loads. An inline//or/* */block comment (both valid JSONC, which VSCode supports for this file) left invalid JSON,json.loadsraisedValueError, andexcept ValueError: passswallowed it — collapsingrecommendationsback to just the PlatformIO default and deleting the user's own recommendations.Changes
platformio/project/integration/jsonc.py— minimal JSONC support: comment/trailing-comma-tolerantloads(), andensure_array_items()which inserts missing array entries in place, preserving the surrounding comments, ordering, and formatting.ProjectGenerator._merge_contents— forextensions.json, when the file already exists the required entries are inserted in place; the file is only rewritten when an entry is actually added. If the existing file cannot be parsed as JSONC, it safely falls back to regenerating from the template. When the file is absent, it is scaffolded from the template as before. Other generated files (c_cpp_properties.json,launch.json) are unaffected and still regenerate.project init --ide vscodetests covering (a) an existing customized file left byte-for-byte unchanged on re-run and (b) a missing recommendation inserted while the user's inline comment and existing entry survive.Why a hand-rolled JSONC helper instead of a dependency?
The problem is not parsing JSONC — it's editing it in place without destroying the user's comments. Standard JSONC parsers (
json5,pyjson5,commentjson,hjson) only parse to a Python object; comments do not survive into the parsed result, so a load → mutate → dump round-trip would reintroduce the exact data loss this PR fixes. Comment-preserving round-trip editing of JSONC has no established Python library (unlikeruamel.yamlfor YAML). Since a parser dependency wouldn't remove the need for the in-place insertion logic — and PlatformIO keeps its dependency set intentionally small — the helper parses only to decide whether an entry is missing, then performs a surgical string insertion into the original text, leaving every other byte untouched. It is pure-Python, has no I/O, and is covered by unit tests. Happy to swap in a maintainer-preferred approach if you'd rather.Checklist
make before-commit(codespell, isort, black, pylint) passes on the changed files (pylint 10.00/10)