Skip to content

Cannot build with go modules #64

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

Closed
3 tasks done
lapwat opened this issue Feb 25, 2022 · 7 comments
Closed
3 tasks done

Cannot build with go modules #64

lapwat opened this issue Feb 25, 2022 · 7 comments
Labels
question Further information is requested support

Comments

@lapwat
Copy link

lapwat commented Feb 25, 2022

My actions before raising this issue

I would like to use go modules in my openfaas function.

Expected Behaviour

I whould be able to build my function by enabling GO111MODULE.

Current Behaviour

Building fail with this error:

handler.go:8:2: no required module provides package golang.org/x/example/stringutil; to add it:
        go get golang.org/x/example/stringutil

Steps to Reproduce (for bugs)

We are going to build a function with "golang.org/x/example/stringutil" module

  1. faas new --lang=golang-middleware test-module
  2. Edit test-module/handler.go
package function

import (
	"fmt"
	"io/ioutil"
	"net/http"

	"golang.org/x/example/stringutil"
)

func Handle(w http.ResponseWriter, r *http.Request) {
	var input []byte

	if r.Body != nil {
		defer r.Body.Close()

		body, _ := ioutil.ReadAll(r.Body)

		input = body
	}

	w.WriteHeader(http.StatusOK)
	w.Write([]byte(stringutil.Reverse("!selpmaxe oG ,olleH")))
}
  1. faas-cli build -f test-module.yml
Step 21/36 : RUN GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover
 ---> Running in e673616d5614
handler.go:8:2: no required module provides package golang.org/x/example/stringutil; to add it:
        go get golang.org/x/example/stringutil
The command '/bin/sh -c GOOS=${TARGETOS} GOARCH=${TARGETARCH} go test ./... -cover' returned a non-zero code: 1

Context

Pulled latest golang-middleware template.

GO111MODULE is now enabled by default.

I tried with build-args option on the cli and in test-module.yml without success.

Your Environment

  • FaaS-CLI version ( Full output from: faas-cli version ):

CLI:
commit: 72816d486cf76c3089b915dfb0b66b85cf096634
version: 0.13.13

  • Docker version docker version (e.g. Docker 17.0.05 ): 20.10.12

  • Are you using Docker Swarm or Kubernetes (FaaS-netes)? Irrelevant

  • Operating System and version (e.g. Linux, Windows, MacOS): Linux

  • Code example or link to GitHub repo or gist to reproduce problem: Irrelevant

  • Other diagnostic information / logs from troubleshooting guide

Next steps

You may join Slack for community support.

@alexellis
Copy link
Member

@lapwat my guess is that you haven't read the docs for this, you should be setting --build-arg GO111MODULE=on

https://docs.openfaas.com/cli/templates/

We also cover this extensively in my eBook on OpenFaaS and Go -> https://gumroad.com/l/everyday-golang

@alexellis
Copy link
Member

/add label: support,question

@derek derek bot added question Further information is requested support labels Feb 25, 2022
@lapwat
Copy link
Author

lapwat commented Feb 25, 2022

I guess that this option is now enabled by default: #63

I also said in the context that I have tried both options:

  • faas-cli build -f test-module.yml --build-arg GO111MODULE=on
  • setting GO111MODULE=on in the yml file

What did I miss ?

@lapwat
Copy link
Author

lapwat commented Feb 25, 2022

Can we reopen the issue ?

@welteki
Copy link
Member

welteki commented Feb 25, 2022

Judging by the error you get, your function is not a go module.

As mentioned in the docs you should run go mod init function to initialize your function and than use go get to manage your dependencies.

@lapwat
Copy link
Author

lapwat commented Feb 25, 2022

I think that this process of 'go getting' packages is handled by the Dockerfile.

Note that I don't have this error when using the original go template.

@lapwat
Copy link
Author

lapwat commented Feb 26, 2022

I have succeeded to build the image in this issue. I had to go mod init function and go get inside the handler folder like you advised.

I do not have to type those commands when working with the go template which was misleading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested support
Projects
None yet
Development

No branches or pull requests

3 participants