Skip to content

Commit

Permalink
hack the mod file to fix: plugin was built with a different version
Browse files Browse the repository at this point in the history
  • Loading branch information
a-peyrard committed Feb 13, 2023
1 parent dcc6a1a commit 53ae2c4
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 2 deletions.
10 changes: 10 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ func (d *defaultCompiler) Compile(repoPath, pathInRepo string) (compiledLibPath
commandArgs = append(commandArgs, compiledLibPath)
commandArgs = append(commandArgs, generateBuildPath(repoPath, pathInRepo, descriptor))

err = hackGoMod(repoPath)
if err != nil {
return
}
defer func() {
unHackErr := unHackGoMod(repoPath)
if unHackErr != nil {
err = unHackErr
}
}()
err = execCompilationCommand(repoPath, commandArgs)

if err != nil {
Expand Down
108 changes: 108 additions & 0 deletions compiler/hack_gomod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package compiler

import (
"bytes"
"fmt"
"github.com/a-peyrard/addon-manager/util/file"
"golang.org/x/mod/modfile"
"os"
"os/exec"
"path/filepath"
"strings"
)

func hackGoMod(repoPath string) (err error) {
var moduleToHack, currentProjectPath string
moduleToHack, err = getCurrentModuleName()
if err != nil {
return
}
moduleToHack = strings.TrimSuffix(moduleToHack, "\n")

currentProjectPath, err = os.Getwd()
if err != nil {
return
}

modPath := filepath.Join(repoPath, "go.mod")
modBackupPath := filepath.Join(repoPath, "go.mod.backup")

var stat os.FileInfo
stat, err = os.Stat(modPath)
if err != nil || !stat.Mode().IsRegular() {
fmt.Printf("repository %s is not using go module, so nothing to hack", repoPath)
return
}
err = file.Copy(modPath, modBackupPath)
if err != nil {
return
}

// Parse the go.mod file
var (
parsedFile *modfile.File
content []byte
)
content, err = os.ReadFile(modPath)
parsedFile, err = modfile.Parse(modPath, content, nil)
if err != nil {
return
}

var version string
for _, req := range parsedFile.Require {
if req.Mod.Path == moduleToHack {
version = req.Mod.Version
break
}
}
if version == "" {
err = fmt.Errorf("unable to find the version of the current module %s", moduleToHack)
return
}

// Append a replace directive to the go.mod file
err = parsedFile.AddReplace(
moduleToHack,
version,
currentProjectPath,
"",
)
if err != nil {
return
}

// Write the changes to the go.mod file
content, err = parsedFile.Format()
if err == nil {
err = os.WriteFile(modPath, content, 0644)
}

return
}

func unHackGoMod(repoPath string) (err error) {
modPath := filepath.Join(repoPath, "go.mod")
modBackupPath := filepath.Join(repoPath, "go.mod.backup")
err = file.Copy(modBackupPath, modPath)
if err == nil {
err = os.Remove(modBackupPath)
}
return
}

func getCurrentModuleName() (moduleName string, err error) {
// Execute the `go list` command
cmd := exec.Command("go", "list", "-m")
var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
return
}

// Extract the module name from the output
moduleName = out.String()

return
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.19
require (
github.com/BurntSushi/toml v1.2.1
github.com/hashicorp/go-getter v1.6.2
golang.org/x/mod v0.8.0
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ golang.org/x/lint v0.0.0-20190301231843-5614ed5bae6f/go.mod h1:UVdnD1Gm6xHRNCYTk
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/lint v0.0.0-20190409202823-959b441ac422/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ func main() {
}),
FactoryMethod: "NewProcess",
})
proc, err := processLoader.Load(addonName, version)

//proc, err := LoadProcess[process.Process](os.Args[1], "NewProcess")
proc, err := processLoader.Load(addonName, version)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 53ae2c4

Please sign in to comment.