MCPcopy Create free account
hub / github.com/vercel/vercel / buildHandlerAsPackageMain

Function buildHandlerAsPackageMain

packages/go/src/index.ts:552–595  ·  view source on GitHub ↗

* Builds the wrapped Go function using the legacy mode where package name is * `"main"` and we need `main.go` in the same dir as the entrypoint, otherwise * `go build` will refuse to build.

({
  entrypointAbsolute,
  entrypointDirname,
  go,
  handlerFunctionName,
  outDir,
  undo,
}: BuildHandlerOptions)

Source from the content-addressed store, hash-verified

550 * `go build` will refuse to build.
551 */
552async function buildHandlerAsPackageMain({
553 entrypointAbsolute,
554 entrypointDirname,
555 go,
556 handlerFunctionName,
557 outDir,
558 undo,
559}: BuildHandlerOptions): Promise<void> {
560 debug('Building Go handler as package "main" (legacy)');
561
562 await writeEntrypoint(
563 join(entrypointDirname, MAIN_GO_FILENAME),
564 '',
565 handlerFunctionName
566 );
567
568 undo.fileActions.push({
569 to: undefined, // delete
570 from: join(entrypointDirname, MAIN_GO_FILENAME),
571 });
572
573 // `go get` will look at `*.go` (note we set `cwd`), parse the `import`s
574 // and download any packages that aren't part of the stdlib
575 debug('Running `go get`...');
576 try {
577 await go.get();
578 } catch (err) {
579 console.error('Failed to `go get`');
580 throw err;
581 }
582
583 debug('Running `go build`...');
584 const destPath = join(outDir, HANDLER_FILENAME);
585 try {
586 const src = [
587 join(entrypointDirname, MAIN_GO_FILENAME),
588 entrypointAbsolute,
589 ].map(file => normalize(file));
590 await go.build(src, destPath);
591 } catch (err) {
592 console.error('failed to `go build`');
593 throw err;
594 }
595}
596
597async function renameHandlerFunction(fsPath: string, from: string, to: string) {
598 let fileContents = await readFile(fsPath, 'utf8');

Callers 1

buildFunction · 0.85

Calls 7

debugFunction · 0.90
writeEntrypointFunction · 0.85
normalizeFunction · 0.85
errorMethod · 0.80
pushMethod · 0.65
getMethod · 0.65
buildMethod · 0.65

Tested by

no test coverage detected