Skip to content

Commit

Permalink
fix(nodejs): use project dir when searching for workspaces for Yarn.l…
Browse files Browse the repository at this point in the history
…ock files (#6102)
  • Loading branch information
DmitriyLewen committed Feb 13, 2024
1 parent 3c1601b commit 3ac6388
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "@test/bar-generators",
"version": "0.0.1",
"dependencies": {
"hoek": "6.1.3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "@test/foo",
"version": "1.0.0",
"repository": "ssh://git@test.com/test.git/foo",
"workspaces": [
"bar/*"
],
"main": "index.js",
"license": "MIT"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


hoek@6.1.3:
version "6.1.3"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-6.1.3.tgz#73b7d33952e01fe27a38b0457294b79dd8da242c"
integrity sha512-YXXAAhmF9zpQbC7LEcREFtXfGq5K1fmd+4PHkBq8NUqmzW3G+Dq10bI/i0KucLRwss3YYFQ0fSfoxBZYiGUqtQ==
7 changes: 5 additions & 2 deletions pkg/fanal/analyzer/language/nodejs/yarn/yarn.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (a yarnAnalyzer) parsePackageJsonDependencies(fsys fs.FS, filePath string)
devDependencies := rootPkg.DevDependencies

if len(rootPkg.Workspaces) > 0 {
pkgs, err := a.traverseWorkspaces(fsys, rootPkg.Workspaces)
pkgs, err := a.traverseWorkspaces(fsys, path.Dir(filePath), rootPkg.Workspaces)
if err != nil {
return nil, nil, xerrors.Errorf("traverse workspaces error: %w", err)
}
Expand All @@ -281,7 +281,7 @@ func (a yarnAnalyzer) parsePackageJsonDependencies(fsys fs.FS, filePath string)
return dependencies, devDependencies, nil
}

func (a yarnAnalyzer) traverseWorkspaces(fsys fs.FS, workspaces []string) ([]packagejson.Package, error) {
func (a yarnAnalyzer) traverseWorkspaces(fsys fs.FS, dir string, workspaces []string) ([]packagejson.Package, error) {
var pkgs []packagejson.Package

required := func(path string, _ fs.DirEntry) bool {
Expand All @@ -298,6 +298,9 @@ func (a yarnAnalyzer) traverseWorkspaces(fsys fs.FS, workspaces []string) ([]pac
}

for _, workspace := range workspaces {
// We need to add the path to the `package.json` file
// to properly get the pattern to search in `fs`
workspace = path.Join(dir, workspace)
matches, err := fs.Glob(fsys, workspace)
if err != nil {
return nil, err
Expand Down
25 changes: 25 additions & 0 deletions pkg/fanal/analyzer/language/nodejs/yarn/yarn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,31 @@ func Test_yarnLibraryAnalyzer_Analyze(t *testing.T) {
},
},
},
{
name: "Project with workspace placed in sub dir",
dir: "testdata/project-with-workspace-in-subdir",
want: &analyzer.AnalysisResult{
Applications: []types.Application{
{
Type: types.Yarn,
FilePath: "foo/yarn.lock",
Libraries: types.Packages{
{
ID: "hoek@6.1.3",
Name: "hoek",
Version: "6.1.3",
Locations: []types.Location{
{
StartLine: 5,
EndLine: 8,
},
},
},
},
},
},
},
},
{
name: "no package.json",
dir: "testdata/no-packagejson",
Expand Down

0 comments on commit 3ac6388

Please sign in to comment.