1

Project looks something like this

project
- internal
  - package
    code.go
- cmd
  - function
    main.go

In cmd/function, the go.mod looks like this:

module github.com/private/repo/cmd/function

go 1.13

require (
    github.com/private/repo/internal/package v0.0.0-00010101000000-000000000000
)

replace github.com/private/repo/internal/package => ../../internal/package

Which works fine locally, however, when deploying as a Cloud Function, it doesn't work, as Cloud Function only contains the "cmd/function" directory.

Instead, I try to copy the module into the project directory and the replace.

project
- cmd
  - function
    main.go
    - internal
      - package
        code.go

replace github.com/private/repo/internal/package => ./internal/package

But this replace seems to be ignored as the Cloud Function deploy build still tries to download this package.

Next, I try using go mod vendor instead, and ignore the go.mod and go.sum files. This seems to do the trick, but not fully, since removing the go.mod, I can't use the internal package anymore.

use of internal package function/vendor/github.com/private/repo/internal/package not allowed

Not sure how I would go about and solve this and still being able to use the internal package name.

Edit: Added a repository showing the error and a simple fix by not using the internal keyword in the library name. https://github.com/lobbin/gcloud-function-error

lobbin
  • 127
  • 1
  • 12
  • How do you deploy your code? Can you paste the command that you use? – guillaume blaquiere Mar 05 '21 at 16:46
  • @guillaumeblaquiere From cmd/function, gcloud functions deploy --region= --source . --trigger-http --runtime=go113 – lobbin Mar 07 '21 at 20:37
  • After lot of tests, my solution doesn't work in Go. It works in Python but not in Go. Did you consider Cloud Run to replace Cloud Functions? – guillaume blaquiere Mar 10 '21 at 20:43
  • Cloud Run is certainly a viable option and would solve this. However, I got stuck in this and wanted to see if there was a solution. Until Google allows for adding go.mod replacements, I don't think there is a solution at the moment. – lobbin Mar 12 '21 at 07:14
  • 1
    Pro tips: Cloud Run and Cloud Functions share the same underlying infrastructure. You can package & deploy source without a docker file like this `gcloud beta run deploy --source=. ...` and your container is built with Buildpack, like your Cloud Functions. Set the concurrency to 1, to process only one request at a time with Cloud Run, as Cloud Functions do. It's closer that you can think! – guillaume blaquiere Mar 12 '21 at 08:01
  • I had similar problem and this answer - https://stackoverflow.com/a/54255486/950131 solved it for me. I needed to deploy Cloud Function (without using vendor for simplicity) while sharing its code with local test app. Parts that deploy to Cloud Function don't use "replace" (it won't work in Cloud Function build environment), only the test app uses it. The "package/" doesn't have go.mod. I don't use "internal/". The packages use proper naming as described in linked answer. – stoper Feb 09 '23 at 12:41

1 Answers1

1

Thank you for reporting this issue.

I filed a Feature Request for this improvement [1].

I suggest you to star the FR to give it more visibility and every time there is an update you will be notified through your email.

Please note there is no ETA for this request at this moment.


[1]. https://issuetracker.google.com/184141587

marian.vladoi
  • 7,663
  • 1
  • 15
  • 29