You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
What to do about use_air() and JSON files with comments in them? #2114
use_air() modifies a settings.json file using jsonlite, but #2109 (comment) let us know that jsonlite will fail to parse JSONC (json with comments), and settings.json is a JSONC file.
This is fairly annoying because it is somewhat common to put comments in a file type like this.
I'm not sure what the right thing for use_air() is. Ideally we'd have a parser that supports JSONC, but in the meantime...?
Maybe we'd:
Create settings.json and extensions.json if they don't exist, and dump in the template containing what we know we want to put there
Open settings.json or extensions.json if they do exist, and output stuff in the console telling the user what they should add to those files (since we can't reliably do that for them). Opening the file at least makes it slightly harder to miss that you are supposed to do something here, but it is still less optimal than just modifying it for the user.
OR we could take a hammer to an existing settings.json or extensions.json and overwrite it completely with our template for air settings, relying on the user to use git diffs to work backwards from there and re-add anything we removed (merging with the new air settings). This is what we do for github actions workflows, but does feel kind of aggressive here
It's worth giving this some thought because we are likely going to have other helpers that touch settings.json as time goes on and more people use Positron (See #2111 too)
(Also, what ends up happening is that a settings.json containing JSON with comments is actually lexed correctly, but the comments are completely dropped, and so they end up getting dropped from the output we write back as well, but everything else works right. This is actually slightly less bad than i thought, but still not great)
Until a more accommodating JSON parser enters the chat, I think the current stance has to be "use version control". And then, if comments have gone missing, selectively stage the changes you want and discard those you don't.
In related news, I have noticed that VS Code and Positron tolerate trailing commas in settings.json, which some JSONC parsers do and others do not. So that's a whole 'nother level of leniency that would be convenient in the parser we use to handle settings.json.
Just noting here that {yyjson} natively supports both single-line (// ...) and C-style block (/* ... */) comments in JSON if you use the YYJSON_READ_ALLOW_COMMENTS flag.
Just to clarify, I realize that a minor feature request like supporting JSONC probably isn't enough to warrant a major overhaul of the jsonlite dependency—especially given its maturity and integration in the R ecosystem. Still, I wanted to point out that yyjson not only supports JSON-with-comments via an explicit parser flag, but also brings additional benefits around parsing performance and flexibility (though it’s not an official r-lib package).
In any case, I thought sharing this context might be helpful for anyone exploring alternative approaches or companion tools—especially for handling commented config formats like JSONC.
As is, I think the way it works is that if there is a json file with comments, we read them in but drop them entirely when we write the file on the way out.
Neither yyjson nor jsonlite seem to have any support for reading a file containing comments in a way that stores location information about those comments robustly enough that we can roundtrip them on the way out (possibly after some tweak to the file contents).
@lionel- and I learned that the VS Code Settings UI uses a standalone parser node-jsonc-parser which actually does retain comments (notably their Extension API drops comments, which is annoying, different parsers!!)
Activity
DavisVaughan commentedon Mar 14, 2025
(Also, what ends up happening is that a
settings.jsoncontaining JSON with comments is actually lexed correctly, but the comments are completely dropped, and so they end up getting dropped from the output we write back as well, but everything else works right. This is actually slightly less bad than i thought, but still not great)jennybc commentedon Jul 10, 2025
Until a more accommodating JSON parser enters the chat, I think the current stance has to be "use version control". And then, if comments have gone missing, selectively stage the changes you want and discard those you don't.
In related news, I have noticed that VS Code and Positron tolerate trailing commas in
settings.json, which some JSONC parsers do and others do not. So that's a whole 'nother level of leniency that would be convenient in the parser we use to handlesettings.json.DavisVaughan commentedon Jul 10, 2025
I tend to agree for now
jimbrig commentedon Aug 3, 2025
@DavisVaughan @jennybc
Just noting here that {yyjson} natively supports both single-line (
// ...) and C-style block (/* ... */) comments in JSON if you use theYYJSON_READ_ALLOW_COMMENTSflag.This feature is formally documented in the API:
See example in yyjson docs
Just to clarify, I realize that a minor feature request like supporting JSONC probably isn't enough to warrant a major overhaul of the
jsonlitedependency—especially given its maturity and integration in the R ecosystem. Still, I wanted to point out that yyjson not only supports JSON-with-comments via an explicit parser flag, but also brings additional benefits around parsing performance and flexibility (though it’s not an official r-lib package).In any case, I thought sharing this context might be helpful for anyone exploring alternative approaches or companion tools—especially for handling commented config formats like JSONC.
DavisVaughan commentedon Aug 4, 2025
I'm fairly certain the problem is less about reading comments than it is about writing them back after we make modifications. Even the jsonlite library allows reading comments
https://github.com/jeroen/jsonlite/blob/4ba1a17db67ae47c0fdd3c5728e254f6d38502b3/src/yajl/api/yajl_parse.h#L123
As is, I think the way it works is that if there is a json file with comments, we read them in but drop them entirely when we write the file on the way out.
Neither yyjson nor jsonlite seem to have any support for reading a file containing comments in a way that stores location information about those comments robustly enough that we can roundtrip them on the way out (possibly after some tweak to the file contents).
jimbrig commentedon Aug 4, 2025
Makes sense
DavisVaughan commentedon Aug 5, 2025
@lionel- and I learned that the VS Code Settings UI uses a standalone parser node-jsonc-parser which actually does retain comments (notably their Extension API drops comments, which is annoying, different parsers!!)
posit-dev/air#390 (comment)
https://github.com/microsoft/node-jsonc-parser
I wonder if it is possible to use V8 to wrap this parser fairly quickly, the "built" JS files live here under the
Codetabhttps://www.npmjs.com/package/jsonc-parser?activeTab=code