Skip to content

Commit 367ecf7

Browse files
committed
Go: Use import path for auto-generated Go module names
1 parent 2aa093c commit 367ecf7

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

go/extractor/toolchain/toolchain.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"log"
66
"os"
77
"os/exec"
8+
"path/filepath"
89
"strings"
910

11+
"github.com/github/codeql-go/extractor/util"
1012
"golang.org/x/mod/semver"
1113
)
1214

@@ -81,7 +83,20 @@ func TidyModule(path string) *exec.Cmd {
8183

8284
// Run `go mod init` in the directory given by `path`.
8385
func InitModule(path string) *exec.Cmd {
84-
modInit := exec.Command("go", "mod", "init", "codeql/auto-project")
86+
moduleName := "codeql/auto-project"
87+
88+
if importpath := util.GetImportPath(); importpath != "" {
89+
// This should be something like `github.com/user/repo`
90+
moduleName = importpath
91+
92+
// If we are not initialising the new module in the root directory of the workspace,
93+
// append the relative path to the module name.
94+
if relPath, err := filepath.Rel(".", path); err != nil && relPath != "." {
95+
moduleName = moduleName + "/" + relPath
96+
}
97+
}
98+
99+
modInit := exec.Command("go", "mod", "init", moduleName)
85100
modInit.Dir = path
86101
return modInit
87102
}

0 commit comments

Comments
 (0)