2

While deploying to google cloud function, I am getting this error:

ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Build failed: # projectname/vendor/golang.org/x/sys/unix
src/projectname/vendor/golang.org/x/sys/unix/syscall.go:83:16: undefined: unsafe.Slice
src/projectname/vendor/golang.org/x/sys/unix/syscall_linux.go:2255:9: undefined: unsafe.Slice
src/projectname/vendor/golang.org/x/sys/unix/syscall_unix.go:118:7: undefined: unsafe.Slice
src/projectname/vendor/golang.org/x/sys/unix/sysvshm_unix.go:33:7: undefined: unsafe.Slice; Error ID: 2f5e35a0

Here's my command:

gcloud functions deploy servicename --region=us-central1 --entry-point=gofunctionname --runtime=go116 --source=.

I am using vendoring to package my dependencies. It's been a while I have updated this function. And first time I noticed this error.

Any help would be much appreciated.

Purusottam
  • 611
  • 6
  • 19
  • 4
    [`unsafe.Slice`](https://pkg.go.dev/unsafe@go1.17#Slice) was added in Go 1.17 and Cloud Functions (frustratingly) hasn't evolved beyond Go 1.16. `unsafe.Slice` is returned by [syscall.go:83.16](https://cs.opensource.google/go/x/sys/+/master:unix/syscall.go;l=83) so, I suspect you've bumped that package or another that uses it and you'll need to either revert or determine what introduced it. – DazWilkin Oct 13 '22 at 01:35
  • Thanks @DazWilkin. It was a compatibility issue i guess. I reverted back the reference to `golang.org/x/sys` and it fixed the deployment issue for me. – Purusottam Oct 13 '22 at 02:36
  • I'm pleased to hear it! – DazWilkin Oct 13 '22 at 02:45

2 Answers2

10

As DazWilkin suggested above, unsafe.Slice was added as part of Go 1.17 and GCP Functions support Go 1.16 as of now.

I had to revert back the golang.org/x/sys module in the go.mod file and it worked for me.

From

golang.org/x/sys v0.0.0-20221010170243-090e33056c14 // indirect

To

golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect

With this change, I am able to build and deploy the code to Google Cloud Functions.

Purusottam
  • 611
  • 6
  • 19
  • 4
    run this command at your project root should be easier. `go mod edit -go='1.16' -replace='golang.org/x/sys'='golang.org/x/sys@v0.0.0-20220811171246-fbc7d0a398ab'` – David S. Nov 05 '22 at 08:46
1

As of the time of writing this, Google Cloud Functions now supports Go 1.18 and Go 1.19.

Update your project to go119 and you shouldn't have this issue anymore. For example:

gcloud functions deploy servicename --runtime=go119 --region=us-central1 
rclod
  • 111
  • 1
  • 2