diff --git a/builder.go b/builder.go index 24553ac..50571c6 100644 --- a/builder.go +++ b/builder.go @@ -41,6 +41,7 @@ type Builder struct { TimeoutGet time.Duration `json:"timeout_get,omitempty"` TimeoutBuild time.Duration `json:"timeout_build,omitempty"` RaceDetector bool `json:"race_detector,omitempty"` + SkipCleanup bool `json:"skip_cleanup,omitempty"` } // Build builds Caddy at the configured version with the @@ -83,6 +84,10 @@ func (b Builder) Build(ctx context.Context, outputFile string) error { env = setEnv(env, "GOOS="+b.OS) env = setEnv(env, "GOARCH="+b.Arch) env = setEnv(env, "GOARM="+b.ARM) + if b.RaceDetector && !b.Compile.Cgo { + log.Println("[WARNING] Enabling cgo because it is required by the race detector") + b.Compile.Cgo = true + } env = setEnv(env, fmt.Sprintf("CGO_ENABLED=%s", b.Compile.CgoEnabled())) log.Println("[INFO] Building Caddy") diff --git a/cmd/xcaddy/main.go b/cmd/xcaddy/main.go index 47c9889..e8c778d 100644 --- a/cmd/xcaddy/main.go +++ b/cmd/xcaddy/main.go @@ -32,6 +32,7 @@ import ( var ( caddyVersion = os.Getenv("CADDY_VERSION") raceDetector = os.Getenv("XCADDY_RACE_DETECTOR") == "1" + skipCleanup = os.Getenv("XCADDY_SKIP_CLEANUP") == "1" ) func main() { @@ -117,6 +118,7 @@ func runBuild(ctx context.Context, args []string) error { Plugins: plugins, Replacements: replacements, RaceDetector: raceDetector, + SkipCleanup: skipCleanup, } err := builder.Build(ctx, output) if err != nil { @@ -204,12 +206,16 @@ func runDev(ctx context.Context, args []string) error { // build caddy with this module plugged in builder := xcaddy.Builder{ + Compile: xcaddy.Compile{ + Cgo: os.Getenv("CGO_ENABLED") == "1", + }, CaddyVersion: caddyVersion, Plugins: []xcaddy.Dependency{ {ModulePath: importPath}, }, Replacements: replacements, RaceDetector: raceDetector, + SkipCleanup: skipCleanup, } err = builder.Build(ctx, binOutput) if err != nil { @@ -227,6 +233,10 @@ func runDev(ctx context.Context, args []string) error { return err } defer func() { + if skipCleanup { + log.Printf("[INFO] Skipping cleanup as requested; leaving artifact: %s", binOutput) + return + } err = os.Remove(binOutput) if err != nil && !os.IsNotExist(err) { log.Printf("[ERROR] Deleting temporary binary %s: %v", binOutput, err) diff --git a/environment.go b/environment.go index 3a97283..b7f921a 100644 --- a/environment.go +++ b/environment.go @@ -95,6 +95,7 @@ func (b Builder) newEnvironment(ctx context.Context) (*environment, error) { caddyModulePath: caddyModulePath, tempFolder: tempFolder, timeoutGoGet: b.TimeoutGet, + skipCleanup: b.SkipCleanup, } // initialize the go module @@ -165,11 +166,16 @@ type environment struct { caddyModulePath string tempFolder string timeoutGoGet time.Duration + skipCleanup bool } // Close cleans up the build environment, including deleting // the temporary folder from the disk. func (env environment) Close() error { + if env.skipCleanup { + log.Printf("[INFO] Skipping cleanup as requested; leaving folder intact: %s", env.tempFolder) + return nil + } log.Printf("[INFO] Cleaning up temporary folder: %s", env.tempFolder) return os.RemoveAll(env.tempFolder) }