Skip to content
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

fix(nodejs): add support for parsing workspaces from package.json as an object #6231

28 changes: 26 additions & 2 deletions pkg/dependency/parser/nodejs/packagejson/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"io"

"github.com/samber/lo"
"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/dependency/parser/types"
Expand All @@ -17,7 +18,7 @@ type packageJSON struct {
Dependencies map[string]string `json:"dependencies"`
OptionalDependencies map[string]string `json:"optionalDependencies"`
DevDependencies map[string]string `json:"devDependencies"`
Workspaces []string `json:"workspaces"`
Workspaces any `json:"workspaces"`
}

type Package struct {
Expand Down Expand Up @@ -57,7 +58,7 @@ func (p *Parser) Parse(r io.Reader) (Package, error) {
Dependencies: pkgJSON.Dependencies,
OptionalDependencies: pkgJSON.OptionalDependencies,
DevDependencies: pkgJSON.DevDependencies,
Workspaces: pkgJSON.Workspaces,
Workspaces: parseWorkspaces(pkgJSON.Workspaces),
}, nil
}

Expand All @@ -73,3 +74,26 @@ func parseLicense(val interface{}) string {
}
return ""
}

// parseWorkspaces returns slice of workspaces
func parseWorkspaces(val any) []string {
// Workspaces support 2 types - https://github.com/SchemaStore/schemastore/blob/master/src/schemas/json/package.json#L777
DmitriyLewen marked this conversation as resolved.
Show resolved Hide resolved
switch ws := val.(type) {
// Workspace as object (map[string][]string)
// e.g. "workspaces": {"packages": ["packages/*", "plugins/*"]},
case map[string]interface{}:
// Take only workspaces for `packages` - https://classic.yarnpkg.com/blog/2018/02/15/nohoist/
if pkgsWorkspaces, ok := ws["packages"]; ok {
return lo.Map(pkgsWorkspaces.([]interface{}), func(workspace interface{}, _ int) string {
return workspace.(string)
})
}
// Workspace as string array
// e.g. "workspaces": ["packages/*", "backend"],
case []interface{}:
return lo.Map(ws, func(workspace interface{}, _ int) string {
return workspace.(string)
})
}
return nil
}
17 changes: 15 additions & 2 deletions pkg/dependency/parser/nodejs/packagejson/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package packagejson_test

import (
"os"
"path"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -77,6 +76,20 @@ func TestParse(t *testing.T) {
},
},
},
{
name: "happy path - workspace as struct",
inputFile: "testdata/workspace_as_map_package.json",
want: packagejson.Package{
Library: types.Library{
ID: "[email protected]",
Name: "example",
Version: "1.0.0",
},
Workspaces: []string{
"packages/*",
},
},
},
{
name: "sad path",
inputFile: "testdata/invalid_package.json",
Expand All @@ -99,7 +112,7 @@ func TestParse(t *testing.T) {
}

for _, v := range vectors {
t.Run(path.Base(v.name), func(t *testing.T) {
t.Run(v.name, func(t *testing.T) {
f, err := os.Open(v.inputFile)
require.NoError(t, err)
defer f.Close()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "example",
"version": "1.0.0",
"workspaces": {
"packages": ["packages/*"],
"nohoist": ["**/react-native", "**/react-native/**"]
}
}
Loading