Skip to content

Commit

Permalink
fix: pnpm install error (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
beyondkmp authored Sep 24, 2024
1 parent 4bbc955 commit 28db936
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 32 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-badgers-greet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-builder-bin": patch
---

fix: pnpm install error for node module collector (https://github.com/electron-userland/electron-builder/issues/8519)
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ test:
cd pkg/node-modules/tar-demo/ && npm install
cd pkg/node-modules/yarn-demo/ && yarn
cd pkg/node-modules/parse-demo/ && yarn
cd pkg/node-modules/es5-demo/ && pnpm install
go test -v ./pkg/...

assets:
Expand Down
15 changes: 15 additions & 0 deletions pkg/node-modules/es5-demo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "pnpm-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"es5-ext": "0.10.53"
}
}
105 changes: 105 additions & 0 deletions pkg/node-modules/es5-demo/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 48 additions & 27 deletions pkg/node-modules/nodeModuleCollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Collector struct {

excludedDependencies map[string]bool
allDependencies []*Dependency
allDependenciesMap map[string]*Dependency

NodeModuleDirToDependencyMap map[string]*map[string]*Dependency `json:"nodeModuleDirToDependencyMap"`

Expand All @@ -45,7 +46,13 @@ type Collector struct {
func (t *Collector) readDependencyTree(dependency *Dependency) error {
if t.rootDependency == nil {
t.rootDependency = dependency
t.allDependenciesMap = make(map[string]*Dependency)
} else {
key := dependency.alias + dependency.Version + dependency.dir
if _, ok := t.allDependenciesMap[key]; ok {
return nil
}
t.allDependenciesMap[key] = dependency
t.allDependencies = append(t.allDependencies, dependency)
}

Expand Down Expand Up @@ -97,43 +104,57 @@ func (t *Collector) readDependencyTree(dependency *Dependency) error {
}

func (t *Collector) writeToParentConflicDependency(d *Dependency) {
p := d.parent
last := d
for p != t.rootDependency {
if p.conflictDependency != nil {
if c, ok := p.conflictDependency[d.Name]; ok {
if c.Version == d.Version {
return
}
break
for p := d.parent; p != t.rootDependency; p = p.parent {
if strings.HasPrefix(d.dir, p.dir) {
if p.conflictDependency == nil {
p.conflictDependency = make(map[string]*Dependency)
}
p.conflictDependency[d.alias] = d
return
}
last = p
p = p.parent
}

if last.conflictDependency == nil {
last.conflictDependency = make(map[string]*Dependency)
if h, ok := t.HoiestedDependencyMap[d.alias]; ok {
if h.Version == d.Version {
return
}

// for pnpm
p := d.parent
last := d
for p != t.rootDependency {
if t.HoiestedDependencyMap[p.alias] != nil {
last = p
break
}

if p.conflictDependency != nil {
if c, ok := p.conflictDependency[d.alias]; ok {
if c.Version == d.Version {
return
}
break
}
}
last = p
p = p.parent
}

if last.conflictDependency == nil {
last.conflictDependency = make(map[string]*Dependency)
}
last.conflictDependency[d.alias] = d
return
}
last.conflictDependency[d.Name] = d

t.HoiestedDependencyMap[d.alias] = d

}

func (t *Collector) processHoistDependencyMap() {
t.HoiestedDependencyMap = make(map[string]*Dependency)
for _, d := range t.allDependencies {
if e, ok := t.HoiestedDependencyMap[d.Name]; ok {
if e.Version != d.Version {
if d.parent == t.rootDependency {
t.HoiestedDependencyMap[d.Name] = d
t.writeToParentConflicDependency(e)
} else {
t.writeToParentConflicDependency(d)
}
}
} else {
t.HoiestedDependencyMap[d.Name] = d
}

t.writeToParentConflicDependency(d)
}
}

Expand Down
40 changes: 35 additions & 5 deletions pkg/node-modules/nodeModuleCollector_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package node_modules

import (
"fmt"
"path"
"path/filepath"
"testing"
Expand Down Expand Up @@ -34,7 +35,7 @@ func TestReadDependencyTreeByNpm(t *testing.T) {
g.Expect(r).To(ConsistOf([]string{
"js-tokens", "react", "remote", "loose-envify",
}))
remoteModule := collector.HoiestedDependencyMap["@electron/remote"]
remoteModule := collector.HoiestedDependencyMap["remote"]
g.Expect(remoteModule.alias).To(Equal("remote"))
g.Expect(remoteModule.Name).To(Equal("@electron/remote"))
}
Expand Down Expand Up @@ -64,7 +65,7 @@ func TestReadDependencyTreeByPnpm(t *testing.T) {
"js-tokens", "react", "remote", "loose-envify",
}))

remoteModule := collector.HoiestedDependencyMap["@electron/remote"]
remoteModule := collector.HoiestedDependencyMap["remote"]
g.Expect(remoteModule.Name).To(Equal("@electron/remote"))
g.Expect(remoteModule.alias).To(Equal("remote"))
g.Expect(remoteModule.dir).To(Equal(filepath.Join(dir, "node_modules/.pnpm/@electron+remote@2.1.2_electron@31.0.0/node_modules/@electron/remote")))
Expand Down Expand Up @@ -99,9 +100,8 @@ func TestReadDependencyTreeForTar(t *testing.T) {
g.Expect(len(r)).To(Equal(46))

g.Expect(collector.HoiestedDependencyMap["tar"].dir).To(Equal(filepath.Join(dir, "node_modules/tar")))
g.Expect(collector.HoiestedDependencyMap["minipass"].Version).To(Equal("7.1.2"))
g.Expect(collector.HoiestedDependencyMap["minizlib"].Version).To(Equal("3.0.1"))
g.Expect(collector.HoiestedDependencyMap["tar"].conflictDependency["ansi-regex"].Version).To(Equal("5.0.1"))
g.Expect(collector.HoiestedDependencyMap["tar"].conflictDependency["minipass"].Version).To(Equal("7.1.2"))
g.Expect(collector.HoiestedDependencyMap["tar"].conflictDependency["minizlib"].Version).To(Equal("3.0.1"))
}

func TestReadDependencyTreeForYarn(t *testing.T) {
Expand Down Expand Up @@ -158,3 +158,33 @@ func TestReadDependencyTreeForParse(t *testing.T) {
g.Expect(collector.HoiestedDependencyMap["asn1.js"].dir).To(Equal(filepath.Join(Dirname(), "parse-demo/node_modules/asn1.js")))
g.Expect(collector.HoiestedDependencyMap["asn1.js"].Version).To(Equal("4.10.1"))
}

func TestReadDependencyTreeForEs5(t *testing.T) {
g := NewGomegaWithT(t)

collector := &Collector{
unresolvedDependencies: make(map[string]bool),
excludedDependencies: make(map[string]bool),
NodeModuleDirToDependencyMap: make(map[string]*map[string]*Dependency),
}

dir := path.Join(Dirname(), "es5-demo")

dependency, err := readPackageJson(dir)
dependency.dir = dir

g.Expect(err).NotTo(HaveOccurred())

err = collector.readDependencyTree(dependency)
g.Expect(err).NotTo(HaveOccurred())
collector.processHoistDependencyMap()

fmt.Println(collector.HoiestedDependencyMap)

g.Expect(collector.HoiestedDependencyMap["d"].dir).To(Equal(filepath.Join(Dirname(), "es5-demo/node_modules/.pnpm/d@1.0.2/node_modules/d")))
g.Expect(collector.HoiestedDependencyMap["d"].Version).To(Equal("1.0.2"))

g.Expect(collector.HoiestedDependencyMap["d"].conflictDependency["es5-ext"].dir).To(Equal(filepath.Join(Dirname(), "es5-demo/node_modules/.pnpm/es5-ext@0.10.64/node_modules/es5-ext")))
g.Expect(collector.HoiestedDependencyMap["d"].conflictDependency["es5-ext"].Version).To(Equal("0.10.64"))

}

0 comments on commit 28db936

Please sign in to comment.