Skip to content

Commit 7f1fc7c

Browse files
authored
[plugins] support reading (and discarding) comments plugin.json files (#1923)
## Summary Fixes #1911 This PR cleans the json-file-content of comments before processing it as "json content" i.e. we discard the comments prior to invoking existing logic for ingesting the `plugin.json` file. This PR relies on Devbox not programmatically modifying the `plugin.json`. For now, users hand edit the `plugin.json` files. This is different from `devbox.json` which we do programmatically modify due to `devbox add`, `devbox rm` and similar commands, and so need to keep track of the json-with-comments' AST. ## How was it tested? To test, I added comments to the following: 1. local plugin example 2. a builtin plugin (python) I did NOT test remote Github plugin. But I tried to modify its code path. Did i get it right?
1 parent 1aa988a commit 7f1fc7c

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

examples/plugins/local/my-plugin/plugin.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,18 @@
66
"MY_FOO_VAR": "BAR"
77
},
88
"create_files": {
9+
/*
10+
this is a comment inside the create files
11+
*/
912
"{{ .Virtenv }}/empty-dir": "",
1013
"{{ .Virtenv }}/some-file": "some-file.txt",
1114
"{{ .DevboxDir }}/some-file.txt": "some-file.txt",
1215
"{{ .Virtenv }}/process-compose.yaml": "process-compose.yaml"
1316
},
1417
"shell": {
18+
// this is a comment before init-hooks
1519
"init_hook": [
20+
"echo \"ran local plugin init hook\"",
1621
"export MY_INIT_HOOK_VAR=BAR"
1722
]
1823
}

internal/plugin/github.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ func newGithubPlugin(ref flake.Ref) (*githubPlugin, error) {
4343
}
4444

4545
func (p *githubPlugin) Fetch() ([]byte, error) {
46-
return p.FileContent(pluginConfigName)
46+
content, err := p.FileContent(pluginConfigName)
47+
if err != nil {
48+
return nil, err
49+
}
50+
return jsonPurifyPluginContent(content)
4751
}
4852

4953
func (p *githubPlugin) CanonicalName() string {

internal/plugin/local.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"path/filepath"
66
"strings"
77

8+
"github.com/pkg/errors"
89
"go.jetpack.io/devbox/internal/cachehash"
910
"go.jetpack.io/devbox/nix/flake"
1011
)
@@ -26,7 +27,11 @@ func newLocalPlugin(ref flake.Ref, pluginDir string) (*LocalPlugin, error) {
2627
}
2728

2829
func (l *LocalPlugin) Fetch() ([]byte, error) {
29-
return os.ReadFile(addFilenameIfMissing(l.Path()))
30+
content, err := os.ReadFile(addFilenameIfMissing(l.Path()))
31+
if err != nil {
32+
return nil, errors.WithStack(err)
33+
}
34+
return jsonPurifyPluginContent(content)
3035
}
3136

3237
func (l *LocalPlugin) CanonicalName() string {

internal/plugin/plugin.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ import (
1010
"io/fs"
1111
"os"
1212
"path/filepath"
13+
"slices"
1314
"strings"
1415
"text/template"
1516

1617
"github.com/pkg/errors"
18+
"github.com/tailscale/hujson"
1719
"go.jetpack.io/devbox/internal/devconfig/configfile"
1820
"go.jetpack.io/devbox/internal/devpkg"
1921
"go.jetpack.io/devbox/internal/fileutil"
@@ -167,6 +169,7 @@ func (m *Manager) createFile(
167169
return nil
168170
}
169171

172+
// buildConfig returns a plugin.Config
170173
func buildConfig(pkg Includable, projectDir, content string) (*Config, error) {
171174
cfg := &Config{PluginOnlyData: PluginOnlyData{Source: pkg}}
172175
name := pkg.CanonicalName()
@@ -185,7 +188,16 @@ func buildConfig(pkg Includable, projectDir, content string) (*Config, error) {
185188
return nil, errors.WithStack(err)
186189
}
187190

188-
return cfg, errors.WithStack(json.Unmarshal(buf.Bytes(), cfg))
191+
jsonb, err := jsonPurifyPluginContent(buf.Bytes())
192+
if err != nil {
193+
return nil, err
194+
}
195+
196+
return cfg, errors.WithStack(json.Unmarshal(jsonb, cfg))
197+
}
198+
199+
func jsonPurifyPluginContent(content []byte) ([]byte, error) {
200+
return hujson.Standardize(slices.Clone(content))
189201
}
190202

191203
func createDir(path string) error {

plugins/python.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
"version": "0.0.3",
44
"description": "Python in Devbox works best when used with a virtual environment (vent, virtualenv, etc.). Devbox will automatically create a virtual environment using `venv` for python3 projects, so you can install packages with pip as normal.\nTo activate the environment, run `. $VENV_DIR/bin/activate` or add it to the init_hook of your devbox.json\nTo change where your virtual environment is created, modify the $VENV_DIR environment variable in your init_hook",
55
"env": {
6+
/*
7+
This is a block comment
8+
*/
69
"VENV_DIR": "{{ .Virtenv }}/.venv"
710
},
811
"create_files": {
912
"{{ .Virtenv }}/bin/venvShellHook.sh": "pip/venvShellHook.sh"
1013
},
14+
// this is a line comment above shell
1115
"shell": {
1216
"init_hook": [
1317
"{{ .Virtenv }}/bin/venvShellHook.sh"

0 commit comments

Comments
 (0)