Skip to content

Commit 843f769

Browse files
committed
Go: Only relocate project to temp dir if there is only one workspace
1 parent 3a982de commit 843f769

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

go/extractor/cli/go-autobuilder/go-autobuilder.go

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,31 @@ func installDependenciesAndBuild() {
545545
// Remove temporary extractor files (e.g. auto-generated go.mod files) when we are done
546546
defer project.RemoveTemporaryExtractorFiles()
547547

548+
// If there is only one workspace and it needs a GOPATH set up, which may be the case if
549+
// we don't use Go modules, then we move the repository to a temporary directory and set
550+
// the GOPATH to it.
551+
if len(workspaces) == 1 {
552+
workspace := workspaces[0]
553+
554+
importpath := getImportPath()
555+
needGopath := getNeedGopath(workspace, importpath)
556+
557+
inLGTM := os.Getenv("LGTM_SRC") != "" || os.Getenv("LGTM_INDEX_NEED_GOPATH") != ""
558+
559+
if inLGTM && needGopath {
560+
paths := moveToTemporaryGopath(srcdir, importpath)
561+
562+
// schedule restoring the contents of newdir to their original location after this function completes:
563+
defer restoreRepoLayout(paths.newdir, paths.files, filepath.Base(paths.scratch), srcdir)
564+
565+
pt := createPathTransformerFile(paths.newdir)
566+
defer os.Remove(pt.Name())
567+
568+
writePathTransformerFile(pt, paths.realSrc, paths.root, paths.newdir)
569+
setGopath(paths.root)
570+
}
571+
}
572+
548573
// Attempt to extract all workspaces; we will tolerate individual extraction failures here
549574
for i, workspace := range workspaces {
550575
goVersionInfo := workspace.RequiredGoVersion()
@@ -565,24 +590,6 @@ func installDependenciesAndBuild() {
565590

566591
tryUpdateGoModAndGoSum(workspace)
567592

568-
importpath := getImportPath()
569-
needGopath := getNeedGopath(workspace, importpath)
570-
571-
inLGTM := os.Getenv("LGTM_SRC") != "" || os.Getenv("LGTM_INDEX_NEED_GOPATH") != ""
572-
573-
if inLGTM && needGopath {
574-
paths := moveToTemporaryGopath(srcdir, importpath)
575-
576-
// schedule restoring the contents of newdir to their original location after this function completes:
577-
defer restoreRepoLayout(paths.newdir, paths.files, filepath.Base(paths.scratch), srcdir)
578-
579-
pt := createPathTransformerFile(paths.newdir)
580-
defer os.Remove(pt.Name())
581-
582-
writePathTransformerFile(pt, paths.realSrc, paths.root, paths.newdir)
583-
setGopath(paths.root)
584-
}
585-
586593
// check whether an explicit dependency installation command was provided
587594
inst := util.Getenv("CODEQL_EXTRACTOR_GO_BUILD_COMMAND", "LGTM_INDEX_BUILD_COMMAND")
588595
shouldInstallDependencies := false

0 commit comments

Comments
 (0)