Skip to content

Commit c2596cc

Browse files
committed
copy go.mod file
1 parent 489285d commit c2596cc

1 file changed

Lines changed: 46 additions & 11 deletions

File tree

tsunami/build/build.go

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -128,19 +128,54 @@ func verifyEnvironment(verbose bool) (*BuildEnv, error) {
128128
func createGoMod(tempDir, appDirName, goVersion string, opts BuildOpts, verbose bool) error {
129129
modulePath := fmt.Sprintf("tsunami/app/%s", appDirName)
130130

131-
// Create new modfile
132-
modFile := &modfile.File{}
133-
if err := modFile.AddModuleStmt(modulePath); err != nil {
134-
return fmt.Errorf("failed to add module statement: %w", err)
135-
}
131+
// Check if go.mod already exists in original directory
132+
originalGoModPath := filepath.Join(opts.Dir, "go.mod")
133+
var modFile *modfile.File
134+
var err error
136135

137-
if err := modFile.AddGoStmt(goVersion); err != nil {
138-
return fmt.Errorf("failed to add go version: %w", err)
139-
}
136+
if _, err := os.Stat(originalGoModPath); err == nil {
137+
// go.mod exists, copy and parse it
138+
if verbose {
139+
log.Printf("Found existing go.mod, copying from %s", originalGoModPath)
140+
}
141+
142+
// Copy existing go.mod to temp directory
143+
tempGoModPath := filepath.Join(tempDir, "go.mod")
144+
if err := copyFile(originalGoModPath, tempGoModPath); err != nil {
145+
return fmt.Errorf("failed to copy existing go.mod: %w", err)
146+
}
147+
148+
// Parse the existing go.mod
149+
goModContent, err := os.ReadFile(tempGoModPath)
150+
if err != nil {
151+
return fmt.Errorf("failed to read copied go.mod: %w", err)
152+
}
153+
154+
modFile, err = modfile.Parse("go.mod", goModContent, nil)
155+
if err != nil {
156+
return fmt.Errorf("failed to parse existing go.mod: %w", err)
157+
}
158+
} else if os.IsNotExist(err) {
159+
// go.mod doesn't exist, create new one
160+
if verbose {
161+
log.Printf("No existing go.mod found, creating new one")
162+
}
163+
164+
modFile = &modfile.File{}
165+
if err := modFile.AddModuleStmt(modulePath); err != nil {
166+
return fmt.Errorf("failed to add module statement: %w", err)
167+
}
140168

141-
// Add requirement for tsunami SDK
142-
if err := modFile.AddRequire("github.com/wavetermdev/waveterm/tsunami", "v0.0.0"); err != nil {
143-
return fmt.Errorf("failed to add require directive: %w", err)
169+
if err := modFile.AddGoStmt(goVersion); err != nil {
170+
return fmt.Errorf("failed to add go version: %w", err)
171+
}
172+
173+
// Add requirement for tsunami SDK
174+
if err := modFile.AddRequire("github.com/wavetermdev/waveterm/tsunami", "v0.0.0"); err != nil {
175+
return fmt.Errorf("failed to add require directive: %w", err)
176+
}
177+
} else {
178+
return fmt.Errorf("error checking for existing go.mod: %w", err)
144179
}
145180

146181
// Add replace directive for tsunami SDK

0 commit comments

Comments
 (0)