Skip to content

Commit c96735e

Browse files
committed
Go: Remove auto-generated go.mod files when done
1 parent db1d24a commit c96735e

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

go/extractor/autobuilder/build-environment.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,9 @@ func IdentifyEnvironment() {
269269
var v versionInfo
270270
workspaces := project.GetWorkspaceInfo(false)
271271

272+
// Remove temporary extractor files (e.g. auto-generated go.mod files) when we are done
273+
defer project.RemoveTemporaryExtractorFiles()
274+
272275
// Find the greatest Go version required by any of the workspaces.
273276
greatestGoVersion := project.GoVersionInfo{Version: "", Found: false}
274277
for _, workspace := range workspaces {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@ func installDependenciesAndBuild() {
526526
os.Setenv("GO111MODULE", "auto")
527527
}
528528

529+
// Remove temporary extractor files (e.g. auto-generated go.mod files) when we are done
530+
defer project.RemoveTemporaryExtractorFiles()
531+
529532
for _, workspace := range workspaces {
530533
goVersionInfo := workspace.RequiredGoVersion()
531534

go/extractor/project/project.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,12 @@ func checkDirsNested(inputDirs []string) (string, bool) {
109109
return dirs[0], true
110110
}
111111

112+
// A list of files we created that should be removed after we are done.
113+
var filesToRemove []string
114+
112115
// Try to initialize a go.mod file for projects that do not already have one.
113116
func initGoModForLegacyProject(path string) {
114-
log.Printf("Project appears to be a legacy Go project, attempting to initialize go.mod")
117+
log.Printf("Project appears to be a legacy Go project, attempting to initialize go.mod in %s\n", path)
115118

116119
modInit := toolchain.InitModule(path)
117120

@@ -120,6 +123,9 @@ func initGoModForLegacyProject(path string) {
120123
return
121124
}
122125

126+
// Add the go.mod file to a list of files we should remove later.
127+
filesToRemove = append(filesToRemove, filepath.Join(path, "go.mod"))
128+
123129
modTidy := toolchain.TidyModule(path)
124130
out, err := modTidy.CombinedOutput()
125131
log.Println(string(out))
@@ -133,6 +139,18 @@ func initGoModForLegacyProject(path string) {
133139
}
134140
}
135141

142+
// Attempts to remove all files that we created.
143+
func RemoveTemporaryExtractorFiles() {
144+
for _, path := range filesToRemove {
145+
err := os.Remove(path)
146+
if err != nil {
147+
log.Printf("Unable to remove file we created at %s: %s\n", path, err.Error())
148+
}
149+
}
150+
151+
filesToRemove = nil
152+
}
153+
136154
// Find all go.work files in the working directory and its subdirectories
137155
func findGoWorkFiles() []string {
138156
return util.FindAllFilesWithName(".", "go.work", "vendor")

0 commit comments

Comments
 (0)