func buildFn()

in cmd/go/appengine_gopath/main.go [57:134]


func buildFn(ctx *gcp.Context) error {
	l, err := ctx.Layer("gopath", gcp.BuildLayer)
	if err != nil {
		return fmt.Errorf("creating gopath layer: %w", err)
	}

	goPath := l.Path
	goPathSrc := filepath.Join(goPath, "src")

	if err := ctx.MkdirAll(goPathSrc, 0755); err != nil {
		return err
	}

	l.BuildEnvironment.Override("GOPATH", goPath)
	l.BuildEnvironment.Override("GO111MODULE", "off")

	stagerGoPath := filepath.Join(ctx.ApplicationRoot(), "_gopath")
	stagerGoPathSrc := filepath.Join(stagerGoPath, "src")
	stagerGoPathMain := filepath.Join(stagerGoPath, "main-package-path")

	stagerGoPathSrcExists, err := ctx.FileExists(stagerGoPathSrc)
	if err != nil {
		return err
	}
	if stagerGoPathSrcExists {
		files, err := ctx.ReadDir(stagerGoPathSrc)
		if err != nil {
			return err
		}
		for _, f := range files {
			// To avoid superfluous files in root of stagerGoPathSrc, copy the subdirectories individually.
			if !f.IsDir() {
				continue
			}
			copyDir(ctx, filepath.Join(stagerGoPathSrc, f.Name()), filepath.Join(goPathSrc, f.Name()))
		}
	}

	var buildMainPath string
	stagerGoPathMainExists, err := ctx.FileExists(stagerGoPathMain)
	if err != nil {
		return err
	}
	if stagerGoPathMainExists {
		goPathMainBytes, err := ctx.ReadFile(stagerGoPathMain)
		if err != nil {
			return err
		}
		buildMainPath = filepath.Join(goPathSrc, strings.TrimSpace(string(goPathMainBytes)))
		// Remove stager directory prior to copying to make sure we don't copy the stager directory to $GOPATH.
		if err := ctx.RemoveAll(stagerGoPath); err != nil {
			return err
		}
		if err := ctx.MkdirAll(buildMainPath, 0755); err != nil {
			return err
		}
		copyDir(ctx, ctx.ApplicationRoot(), buildMainPath)
	} else {
		buildMainPath = "./..."
		// Remove stager directory to make sure there's only one go package in application root.
		if err := ctx.RemoveAll(stagerGoPath); err != nil {
			return err
		}
	}

	if _, exists := os.LookupEnv(env.Buildable); !exists {
		l.BuildEnvironment.Override(env.Buildable, buildMainPath)
	}

	// Unlike in the appengine_gomod buildpack, we do not have to compile gopath apps from a path that ends in /srv/. There are two cases:
	//  * _gopath/main-package-path exists and app source is put on GOPATH, which is handled by:
	//			https://github.com/golang/appengine/blob/553959209a20f3be281c16dd5be5c740a893978f/delay/delay.go#L136.
	//  * _gopath/main-package-path does not exist and the app is built from the current directory, which is handled by:
	//			https://github.com/golang/appengine/blob/553959209a20f3be281c16dd5be5c740a893978f/delay/delay.go#L125-L127

	// TODO(b/145608768): Investigate creating and caching a GOCACHE layer.
	return nil
}