in cmd/go/functions_framework/main.go [226:293]
func createMainGoModVendored(ctx *gcp.Context, fn fnInfo) error {
l, err := ctx.Layer(gopathLayerName, gcp.BuildLayer)
if err != nil {
return fmt.Errorf("creating %v layer: %w", gopathLayerName, err)
}
l.BuildEnvironment.Override("GOPATH", l.Path)
if err := ctx.Setenv("GOPATH", l.Path); err != nil {
return err
}
_, fnPackage, err := moduleAndPackageNames(ctx, fn)
if err != nil {
return fmt.Errorf("extracting module and package names: %w", err)
}
fn.Package = fnPackage
fnFrameworkVendoredPathExists, err := ctx.FileExists(fn.Source, "vendor", functionsFrameworkPackage)
if err != nil {
return err
}
version, err := frameworkSpecifiedVersion(ctx, fn.Source)
if err != nil {
return fmt.Errorf("checking for functions framework dependency in go.mod: %w", err)
}
// The function must declare functions framework as a dependency.
if version == "" || !fnFrameworkVendoredPathExists {
// Vendored dependencies must include the functions framework. Modifying vendored dependencies
// and adding the framework ourselves by merging two vendor directories is brittle and likely
// to cause conflicts among the function's and the framework's dependencies.
return gcp.UserErrorf("vendored dependencies must include %q; if your function does not depend on the module, please add a blank import: `_ %q`", functionsFrameworkModule, functionsFrameworkPackage)
}
cloudfunctions.AddFrameworkVersionLabel(ctx, &cloudfunctions.FrameworkVersionInfo{
Runtime: "go",
Version: version,
Injected: false,
})
var buildablePackagePath string
var appDir string
supportsVendorModification, err := golang.SupportsVendorModificaton(ctx)
if err != nil {
return fmt.Errorf("checking if vendor modification is supported: %w", err)
}
if !supportsVendorModification {
appDir = filepath.Join(fn.Source, appModule)
buildablePackagePath = appDir
} else {
appDir = filepath.Join(fn.Source, "vendor", appModule)
buildablePackagePath = appModule
}
if err := ctx.MkdirAll(appDir, 0755); err != nil {
return err
}
appMainPath := filepath.Join(appDir, "main.go")
l.BuildEnvironment.Override(env.Buildable, buildablePackagePath)
l.BuildEnvironment.Override(golang.BuildDirEnv, fn.Source)
return createMainGoFile(ctx, fn, appMainPath, version)
}