Skip to content

Commit

Permalink
fix: handle comments in import map jsonc (#3035)
Browse files Browse the repository at this point in the history
* fix: handle comments in import map jsonc

* fix: handle comments in vscode settings

* fix: use decoder to handle empty data
  • Loading branch information
sweatybridge authored Jan 13, 2025
1 parent 76272d3 commit d3de637
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ require (
github.com/spf13/viper v1.19.0
github.com/stretchr/testify v1.10.0
github.com/stripe/pg-schema-diff v0.8.0
github.com/tidwall/jsonc v0.3.2
github.com/withfig/autocomplete-tools/packages/cobra v1.2.0
github.com/zalando/go-keyring v0.2.6
go.opentelemetry.io/otel v1.33.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,8 @@ github.com/tetafro/godot v1.4.20 h1:z/p8Ek55UdNvzt4TFn2zx2KscpW4rWqcnUrdmvWJj7E=
github.com/tetafro/godot v1.4.20/go.mod h1:2oVxTBSftRTh4+MVfUaUXR6bn2GDXCaMcOG4Dk3rfio=
github.com/theupdateframework/notary v0.7.0 h1:QyagRZ7wlSpjT5N2qQAh/pN+DVqgekv4DzbAiAiEL3c=
github.com/theupdateframework/notary v0.7.0/go.mod h1:c9DRxcmhHmVLDay4/2fUYdISnHqbFDGRSlXPO0AhYWw=
github.com/tidwall/jsonc v0.3.2 h1:ZTKrmejRlAJYdn0kcaFqRAKlxxFIC21pYq8vLa4p2Wc=
github.com/tidwall/jsonc v0.3.2/go.mod h1:dw+3CIxqHi+t8eFSpzzMlcVYxKp08UP5CD8/uSFCyJE=
github.com/timakin/bodyclose v0.0.0-20241017074812-ed6a65f985e3 h1:y4mJRFlM6fUyPhoXuFg/Yu02fg/nIPFMOY8tOqppoFg=
github.com/timakin/bodyclose v0.0.0-20241017074812-ed6a65f985e3/go.mod h1:mkjARE7Yr8qU23YcGMSALbIxTQ9r9QBVahQOBRfU460=
github.com/timonwong/loggercheck v0.10.1 h1:uVZYClxQFpw55eh+PIoqM7uAOHMrhVcDoWDery9R8Lg=
Expand Down
9 changes: 5 additions & 4 deletions internal/init/init.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package init

import (
"bytes"
"context"
_ "embed"
"encoding/json"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/supabase/cli/internal/utils"
"github.com/tidwall/jsonc"
)

var (
Expand Down Expand Up @@ -100,15 +102,14 @@ func updateGitIgnore(ignorePath string, fsys afero.Fs) error {
type VSCodeSettings map[string]interface{}

func loadUserSettings(path string, fsys afero.Fs) (VSCodeSettings, error) {
// Open our jsonFile
jsonFile, err := fsys.Open(path)
data, err := afero.ReadFile(fsys, path)
if err != nil {
return nil, errors.Errorf("failed to load settings file: %w", err)
}
defer jsonFile.Close()
data = jsonc.ToJSONInPlace(data)
// Parse and unmarshal JSON file.
var userSettings VSCodeSettings
dec := json.NewDecoder(jsonFile)
dec := json.NewDecoder(bytes.NewReader(data))
if err := dec.Decode(&userSettings); err != nil {
return nil, errors.Errorf("failed to parse settings: %w", err)
}
Expand Down
7 changes: 4 additions & 3 deletions internal/utils/deno.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/go-errors/errors"
"github.com/spf13/afero"
"github.com/tidwall/jsonc"
)

var (
Expand Down Expand Up @@ -215,13 +216,13 @@ type ImportMap struct {
}

func NewImportMap(absJsonPath string, fsys afero.Fs) (*ImportMap, error) {
contents, err := fsys.Open(absJsonPath)
data, err := afero.ReadFile(fsys, absJsonPath)
if err != nil {
return nil, errors.Errorf("failed to load import map: %w", err)
}
defer contents.Close()
data = jsonc.ToJSONInPlace(data)
result := ImportMap{}
decoder := json.NewDecoder(contents)
decoder := json.NewDecoder(bytes.NewReader(data))
if err := decoder.Decode(&result); err != nil {
return nil, errors.Errorf("failed to parse import map: %w", err)
}
Expand Down

0 comments on commit d3de637

Please sign in to comment.