-
Notifications
You must be signed in to change notification settings - Fork 115
fix: properly parse JSONC in extensions.json and batch extension installs #707
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rodmk
wants to merge
1
commit into
coder:main
Choose a base branch
from
rodmk:fix/jsonc-extension-parsing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
55 changes: 55 additions & 0 deletions
55
registry/coder/modules/code-server/parse_jsonc_extensions.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Parses a JSONC file and prints extension recommendations, one per line. | ||
| // Handles // comments, /* */ block comments (including multi-line), and trailing commas. | ||
| // Used by code-server and vscode-web modules to parse .vscode/extensions.json | ||
| // and .code-workspace files. | ||
| // | ||
| // Environment variables: | ||
| // FILE - path to the JSONC file | ||
| // QUERY - jq-style query: "recommendations" (default) or "extensions.recommendations" | ||
| var fs = require("fs"); | ||
| var text = fs.readFileSync(process.env.FILE, "utf8"); | ||
| var result = ""; | ||
| var inString = false; | ||
| var i = 0; | ||
|
|
||
| while (i < text.length) { | ||
| if (inString) { | ||
| if (text[i] === "\\" && i + 1 < text.length) { | ||
| result += text.slice(i, i + 2); | ||
| i += 2; | ||
| continue; | ||
| } | ||
| if (text[i] === '"') inString = false; | ||
| result += text[i++]; | ||
| } else { | ||
| if (text[i] === '"') { | ||
| inString = true; | ||
| result += text[i++]; | ||
| continue; | ||
| } | ||
| if (text[i] === "/" && text[i + 1] === "/") { | ||
| while (i < text.length && text[i] !== "\n") i++; | ||
| continue; | ||
| } | ||
| if (text[i] === "/" && text[i + 1] === "*") { | ||
| i += 2; | ||
| while (i < text.length && !(text[i] === "*" && text[i + 1] === "/")) i++; | ||
| i += 2; | ||
| continue; | ||
| } | ||
| result += text[i++]; | ||
| } | ||
| } | ||
|
|
||
| result = result.replace(/,(\s*[\]}])/g, "$1"); | ||
| var data = JSON.parse(result); | ||
| var query = process.env.QUERY || "recommendations"; | ||
| var recommendations; | ||
| if (query === "extensions.recommendations") { | ||
| recommendations = (data.extensions && data.extensions.recommendations) || []; | ||
| } else { | ||
| recommendations = data.recommendations || []; | ||
| } | ||
| recommendations.forEach(function (e) { | ||
| console.log(e); | ||
| }); | ||
70 changes: 70 additions & 0 deletions
70
registry/coder/modules/code-server/parse_jsonc_extensions.test.ts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| import { describe, expect, it } from "bun:test"; | ||
| import { spawn, readableStreamToText } from "bun"; | ||
| import { unlink } from "node:fs/promises"; | ||
| import { join } from "node:path"; | ||
|
|
||
| const PARSER = join(import.meta.dir, "parse_jsonc_extensions.js"); | ||
| const TMP = join(import.meta.dir, "tmp_test.json"); | ||
|
|
||
| async function parseExtensions( | ||
| json: string, | ||
| query?: string, | ||
| ): Promise<string[]> { | ||
| await Bun.write(TMP, json); | ||
| try { | ||
| const proc = spawn([process.execPath, PARSER], { | ||
| env: { FILE: TMP, QUERY: query ?? "recommendations" }, | ||
| stdout: "pipe", | ||
| stderr: "pipe", | ||
| }); | ||
| const out = await readableStreamToText(proc.stdout); | ||
| const exitCode = await proc.exited; | ||
| if (exitCode !== 0) { | ||
| throw new Error(await readableStreamToText(proc.stderr)); | ||
| } | ||
| return out.trim().split("\n").filter(Boolean); | ||
| } finally { | ||
| await unlink(TMP).catch(() => {}); | ||
| } | ||
| } | ||
|
|
||
| describe("parse_jsonc_extensions", () => { | ||
| it("handles comments and trailing commas", async () => { | ||
| const result = await parseExtensions(`{ | ||
| // line comment | ||
| "recommendations": [ | ||
| "ms-python.python", | ||
| /* block comment */ | ||
| "dbaeumer.vscode-eslint", // inline | ||
| ], | ||
| }`); | ||
| expect(result).toEqual(["ms-python.python", "dbaeumer.vscode-eslint"]); | ||
| }); | ||
|
|
||
| it("does not mangle URLs in strings", async () => { | ||
| const result = await parseExtensions(`{ | ||
| "recommendations": [ | ||
| "ms-python.python", | ||
| "https://example.com/custom.vsix" | ||
| ] | ||
| }`); | ||
| expect(result).toEqual([ | ||
| "ms-python.python", | ||
| "https://example.com/custom.vsix", | ||
| ]); | ||
| }); | ||
|
|
||
| it("handles .code-workspace format", async () => { | ||
| const result = await parseExtensions( | ||
| `{ | ||
| "folders": [{"path": "."}], | ||
| "extensions": { | ||
| // Recommended | ||
| "recommendations": ["ms-python.python"], | ||
| }, | ||
| }`, | ||
| "extensions.recommendations", | ||
| ); | ||
| expect(result).toEqual(["ms-python.python"]); | ||
| }); | ||
| }); |
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
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
55 changes: 55 additions & 0 deletions
55
registry/coder/modules/vscode-web/parse_jsonc_extensions.js
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Parses a JSONC file and prints extension recommendations, one per line. | ||
| // Handles // comments, /* */ block comments (including multi-line), and trailing commas. | ||
| // Used by code-server and vscode-web modules to parse .vscode/extensions.json | ||
| // and .code-workspace files. | ||
| // | ||
| // Environment variables: | ||
| // FILE - path to the JSONC file | ||
| // QUERY - jq-style query: "recommendations" (default) or "extensions.recommendations" | ||
| var fs = require("fs"); | ||
| var text = fs.readFileSync(process.env.FILE, "utf8"); | ||
| var result = ""; | ||
| var inString = false; | ||
| var i = 0; | ||
|
|
||
| while (i < text.length) { | ||
| if (inString) { | ||
| if (text[i] === "\\" && i + 1 < text.length) { | ||
| result += text.slice(i, i + 2); | ||
| i += 2; | ||
| continue; | ||
| } | ||
| if (text[i] === '"') inString = false; | ||
| result += text[i++]; | ||
| } else { | ||
| if (text[i] === '"') { | ||
| inString = true; | ||
| result += text[i++]; | ||
| continue; | ||
| } | ||
| if (text[i] === "/" && text[i + 1] === "/") { | ||
| while (i < text.length && text[i] !== "\n") i++; | ||
| continue; | ||
| } | ||
| if (text[i] === "/" && text[i + 1] === "*") { | ||
| i += 2; | ||
| while (i < text.length && !(text[i] === "*" && text[i + 1] === "/")) i++; | ||
| i += 2; | ||
| continue; | ||
| } | ||
| result += text[i++]; | ||
| } | ||
| } | ||
|
|
||
| result = result.replace(/,(\s*[\]}])/g, "$1"); | ||
| var data = JSON.parse(result); | ||
| var query = process.env.QUERY || "recommendations"; | ||
| var recommendations; | ||
| if (query === "extensions.recommendations") { | ||
| recommendations = (data.extensions && data.extensions.recommendations) || []; | ||
| } else { | ||
| recommendations = data.recommendations || []; | ||
| } | ||
| recommendations.forEach(function (e) { | ||
| console.log(e); | ||
| }); |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The trailing-comma pass
result.replace(/,(\s*[\]}])/g, "$1")is not string-aware, so it also rewrites valid string data that happens to contain,]or,}. Inextensions.json, a recommendation likehttps://example.com/a,].vsixis mutated tohttps://example.com/a].vsixbeforeJSON.parse, which leads to installing the wrong extension ID/URL (or a failed install); the same parser logic is duplicated for vscode-web, so both modules are affected.Useful? React with 👍 / 👎.