in cmd/go/legacy_worker/main.go [66:128]
func buildFn(ctx *gcp.Context) error {
l, err := ctx.Layer(layerName, gcp.LaunchLayer)
if err != nil {
return fmt.Errorf("creating %v layer: %w", layerName, err)
}
if err := ctx.SetFunctionsEnvVars(l); err != nil {
return err
}
ctx.AddWebProcess([]string{golang.OutBin})
fnTarget := os.Getenv(env.FunctionTarget)
// Move the function source code into a subdirectory in order to construct the app in the main application root.
if err := ctx.RemoveAll(fnSourceDir); err != nil {
return err
}
if err := ctx.MkdirAll(fnSourceDir, 0755); err != nil {
return err
}
// mindepth=1 excludes '.', '+' collects all file names before running the command.
// Exclude serverless_function_source_code and .google* dir e.g. .googlebuild, .googleconfig
command := fmt.Sprintf("find . -mindepth 1 -not -name %[1]s -prune -not -name %[2]q -prune -exec mv -t %[1]s {} +", fnSourceDir, ".google*")
if _, err := ctx.Exec([]string{"bash", "-c", command}, gcp.WithUserTimingAttribution); err != nil {
return err
}
fnSource := filepath.Join(ctx.ApplicationRoot(), fnSourceDir)
pkgName, err := extractPackageNameInDir(ctx, fnSource)
if err != nil {
return fmt.Errorf("extracting package name: %w", err)
}
fn := fnInfo{
Source: fnSource,
Target: fnTarget,
Package: pkgName,
}
l.LaunchEnvironment.Default("X_GOOGLE_ENTRY_POINT", os.Getenv(env.FunctionTarget))
triggerType := os.Getenv(env.FunctionSignatureType)
if triggerType == "http" || triggerType == "" {
triggerType = "HTTP_TRIGGER"
}
l.LaunchEnvironment.Default("X_GOOGLE_FUNCTION_TRIGGER_TYPE", triggerType)
goMod := filepath.Join(fn.Source, "go.mod")
goModExists, err := ctx.FileExists(goMod)
if err != nil {
return err
}
if !goModExists {
return createMainVendored(ctx, fn)
}
isWriteable, err := ctx.IsWritable(goMod)
if err != nil {
return err
}
if !isWriteable {
// Preempt an obscure failure mode: if go.mod is not writable then `go list -m` can fail saying:
// go: updates to go.sum needed, disabled by -mod=readonly
return gcp.UserErrorf("go.mod exists but is not writable")
}
return createMainGoMod(ctx, fn)
}