Skip to content

Stop stripping JSONC comments with a regex in the PowerShell workload - #118

Open
Laurent Zogaj (26zl) wants to merge 1 commit into
microsoft:mainfrom
26zl:pr/vscode-settings-jsonc
Open

Laurent Zogaj (26zl) wants to merge 1 commit into
microsoft:mainfrom
26zl:pr/vscode-settings-jsonc

Conversation

@26zl

Copy link
Copy Markdown
Contributor

Hi! I really like this project. I had built something similar for myself before I found it, and it has become my standard routine for every freshly reset PC; it runs on two of my own machines now.

One thing has been in the way: the PowerShell workload's ScriptAnalyzer unit mangles my VS Code settings.json, so I have been running that workload from a local copy with the unit removed.

Read-VSCodeSettings strips block comments with [regex]::Replace($raw, '/\*[\s\S]*?\*/', '') before ConvertFrom-Json. That pattern also matches from the /** at the end of one glob key to the **/ at the start of the next, so

"files.exclude": { "**/.venv/**": true, "**/node_modules/**": true }

is written back as "**/.venvnode_modules/**": true. The second entry is lost and the first is mangled, silently.

The fix drops the regex pre-processing. The unit runs under pwsh 7, whose ConvertFrom-Json handles //, /* */ and trailing commas natively, and as far as I can tell it already needs PowerShell 6+ (-AsHashtable, $IsMacOS), so nothing gets narrower. One detail: a file that holds only comments parses to $null, where the regex path returned an empty table, so that case is mapped back to an empty table. Reproduced on Windows 11 Pro 25H2 (build 26200.9550) with pwsh 7.6.6 by calling Read-VSCodeSettings on a file with the sample above; after the change both keys survive, a comment-only or empty file gives an empty table, invalid JSON still errors, the unit's three scripts parse and the YAML parses. The signed copy under Workloads/ is left to the sign pipeline.

I have a few more small findings (a couple in the new Uninstall code, mostly docs that no longer match the scripts) and will open them as separate PRs; happy to combine them if you would rather have fewer.

Thanks for having a look.

Read-VSCodeSettings removed /* */ blocks before ConvertFrom-Json. The
pattern also matches from the "/**" at the end of one glob key to the
"**/" at the start of the next, so a settings.json containing

  "files.exclude": { "**/.venv/**": true, "**/node_modules/**": true }

was rewritten as "**/.venvnode_modules/**" and the second entry was lost.
pwsh 7's ConvertFrom-Json accepts // and /* */ comments and trailing
commas natively, so the pre-processing is unnecessary.

A file that holds only comments parses to $null, where the regex path
used to return an empty table; that case returns an empty table again.
@26zl

Copy link
Copy Markdown
Contributor Author

Related PRs from the same pass, each independent: #119, #120, #121, #122, #123.

@26zl

Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

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.

1 participant