Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build go function fails if there are more than main.go in src #13725

Open
2 tasks done
nam-truong-le opened this issue Apr 18, 2024 · 8 comments
Open
2 tasks done

Build go function fails if there are more than main.go in src #13725

nam-truong-le opened this issue Apr 18, 2024 · 8 comments
Assignees
Labels
bug Something isn't working functions Issues tied to the functions category p2

Comments

@nam-truong-le
Copy link

nam-truong-le commented Apr 18, 2024

How did you install the Amplify CLI?

yarn

If applicable, what version of Node.js are you using?

20

Amplify CLI Version

12.11.1

What operating system are you using?

Ubuntu

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

No

Describe the bug

This change here fails go function build if there are more than main.go in src.

Expected behavior

Go function should be built

Reproduction steps

  1. Create a lambda function using go
  2. in main.go use a function defined in another.go
  3. Build will fail

Project Identifier

No response

Log output

No response

Additional information

No response

Before submitting, please confirm:

  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • I have removed any sensitive information from my code snippets and submission.
@nam-truong-le nam-truong-le added the pending-triage Issue is pending triage label Apr 18, 2024
@ykethan
Copy link
Contributor

ykethan commented Apr 18, 2024

Hey @nam-truong-le, thank you for reaching out. Marking as bug for further investigation.

@ykethan ykethan added functions Issues tied to the functions category bug Something isn't working and removed pending-triage Issue is pending triage bug Something isn't working labels Apr 18, 2024
@ykethan
Copy link
Contributor

ykethan commented Apr 18, 2024

Hey @nam-truong-le, wanted update on this issue. With the latest update to the Lambda functions on #13671.
You will need to upgrade the local go runtime to match the Lambda function. refer to AWS Documentation providing additional information.

On a quick test with the following steps the push did not run into an error on a new function

  1. amplify add function
  2. created a folder say test folder with test.go file
// <function-name>/src/test/test.go
package test

import (
	"fmt"
  )

func DoSomething() {
	fmt.Println("Hello World!")
  }

in main.go

// <function-name>/src/main.go
package main

import (
  "fmt"
  "context"
  "github.com/aws/aws-lambda-go/lambda"

  test "lambda/test"
)

type MyEvent struct {
  Name string `json:"name"`
}

func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
  return fmt.Sprintf("Hello %s!", name.Name ), nil
}

func main() {
  test.DoSomething()
  lambda.Start(HandleRequest)
}

@ykethan ykethan added the pending-response Issue is pending response from the issue author label Apr 18, 2024
@nam-truong-le
Copy link
Author

@ykethan which version of @aws-amplify/cli are you using?

@github-actions github-actions bot removed the pending-response Issue is pending response from the issue author label Apr 18, 2024
@ykethan ykethan added the pending-triage Issue is pending triage label Apr 19, 2024
@ykethan
Copy link
Contributor

ykethan commented Apr 19, 2024

@nam-truong-le this is on the latest version of Amplify CLI: 12.11.1

@ykethan ykethan added the pending-response Issue is pending response from the issue author label Apr 19, 2024
@nam-truong-le
Copy link
Author

I use the same version of CLI but I get this:

# command-line-arguments
Error: ./main.go:63:6: undefined: processOrder
🛑 go failed, error message was Command failed with exit code 1: go build -o ../bin/bootstrap main.go

Learn more at: https://docs.amplify.aws/cli/project/troubleshooting/

Session Identifier: ca285f60-6882-4563-a1ea-1ba98447a976
Error: Process completed with exit code 1.

As you can see, the build command is go build -o ../bin/bootstrap main.go and this won't work, I can repro the issue with just a simple go repo where I use another function outside of main.go.

@github-actions github-actions bot removed the pending-response Issue is pending response from the issue author label Apr 28, 2024
@ykethan
Copy link
Contributor

ykethan commented Apr 30, 2024

Hey @nam-truong-le, could you provide us the reproduction steps or sample snippets of the example with the file paths used to reproduce the issue?

@ykethan ykethan added the pending-response Issue is pending response from the issue author label Apr 30, 2024
@jo-su
Copy link

jo-su commented May 2, 2024

I am experiencing the same issue with my Go Lambda functions. In my case I have multiple files in my main package.
Everything outside main.go is ignored as the command used is go build -o ../bin/bootstrap main.go.

I'm using the 12.1.1 version of the CLI on Ubuntu 20.04.6 LTS.

This are the steps taken to reproduce:
1 - amplify build function gomultiplefiles
2 - On amplify/backend/function/gomultiplefiles/src/main.go

package main

import (
	"context"
	"fmt"

	"github.com/aws/aws-lambda-go/lambda"
)

func HandleRequest(ctx context.Context, name MyEvent) (string, error) {
	return fmt.Sprintf("Hello %s!", name.Name), nil
}

func main() {
	lambda.Start(HandleRequest)
}

2 - On amplify/backend/function/gomultiplefiles/src/types.go

package main

type MyEvent struct {
	Name string `json:"name"`
}

3 - Running amplify build function gomultiplefiles outputs the following error

# command-line-arguments
./main.go:10:46: undefined: MyEvent
🛑 go failed, error message was Command failed with exit code 1: go build -o ../bin/bootstrap main.go

If the command changed from go build -o ../bin/bootstrap main.go to go build -o ../bin/bootstrap ., it would work.
This same situation is mentioned in this issue #5269 from some years ago.

@ykethan
Copy link
Contributor

ykethan commented May 6, 2024

Hey @jo-su, thank you for the information. I was able to reproduce the issue. Marking a bug.
reproduced the issue with the following test

  1. created a go function
  2. modified the function according to Build go function fails if there are more than main.go in src #13725 (comment)
  3. changes directory into the function folder to and ran go build -o bootstrap main.go which errored out with ./main.go:13:46: undefined: MyEvent. but running go build -o bootstrap . built the function.

Noticed an error Error: fork/exec /var/task/bootstrap: exec format error Runtime.InvalidEntrypoint on the Lambda function when the function was pushed with an older version of Amplify CLI.
From the AWS documentation: https://docs.aws.amazon.com/lambda/latest/dg/golang-handler.html#golang-handler-naming
the function will refer to function code named bootstrap, which previously was handler in go1.x runtime. ensure the go.mod and the CloudFormation template are updated to use provided.al2023.

@ykethan ykethan added bug Something isn't working and removed pending-response Issue is pending response from the issue author pending-triage Issue is pending triage labels May 6, 2024
@ykethan ykethan added the p2 label May 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working functions Issues tied to the functions category p2
Projects
None yet
Development

No branches or pull requests

4 participants