Unwanted Glide imports - go

I am working on an App Engine app and am coming across this error running goapp serve:
Failed parsing input: parser: bad import "syscall" in vendor/golang.org/x/net/icmp/message.go
Which is apparently from Glide importing the entire golang.org/x/net package when I only wanted golang.org/x/net/context.
The import in my glide.yaml was:
- package: golang.org/x/net
subpackages:
- context
And the glide.lock file says
- name: golang.org/x/net
version: 07b51741c1d6423d4a6abab1c49940ec09cb1aaf
subpackages:
- context
How would I keep net/icmp out of my build in goapp serve?

I often had extra imports when using glide (a bit like in issue 101)
Just for testing, try re-populate your vendor folder using govendor
govendor list
govendor fetch +m
In my experience, the resulting list of dependencies is cleaner.

Related

Why can't Heroku build my app with dependencies?

I have been working on a Go project and deploying it successfully with Heroku up until I added more packages. My Heroku app is set up to deploy the main branch of my github repo, but it fails to build the app each time I push. This is the build log:
-----> Using buildpack: heroku/go
-----> Go app detected
-----> Fetching stdlib.sh.v8... done
----->
Detected go modules via go.mod
----->
Detected Module Name: my-heroku-app
----->
-----> New Go Version, clearing old cache
-----> Installing go1.17.3
-----> Fetching go1.17.3.linux-amd64.tar.gz... done
-----> Determining packages to install
Detected the following main packages to install:
my-heroku-app
-----> Running: go install -v -tags heroku -mod=vendor my-heroku-app
webapp.go:6:2: cannot find package "." in:
/tmp/build_1bdd9ef1/vendor/github.com/go-sql-driver/mysql
webapp.go:7:2: cannot find package "." in:
/tmp/build_1bdd9ef1/vendor/github.com/gorilla/context
webapp.go:8:2: cannot find package "." in:
/tmp/build_1bdd9ef1/vendor/github.com/gorilla/sessions
webapp.go:9:2: cannot find package "." in:
/tmp/build_1bdd9ef1/vendor/golang.org/x/crypto/bcrypt
! Push rejected, failed to compile Go app.
! Push failed
Here is what my import statement looks like
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/context"
"github.com/gorilla/sessions"
"golang.org/x/crypto/bcrypt"
"html/template"
"log"
"net/http"
"time"
"unicode"
)
I have tried reading through the guides and documentation about govendor both at Heroku and on their github page, and as far as I can tell, I have done my setup correctly. I am currently using govendor and I have a vendor folder containing a json file in my repo that has the info for the dependencies. Everything works when I run it locally
update: I made a fresh repo with minimal code but as soon as I add a dependency I got the same error. I also tried deploying on Digital Ocean as well but I think they use the open source Heroku buildpack so I got the same error
update 2: I got a pared down version of the app working. The repo has no vendor directory or vendor json whatsoever despite using an external package (build log shows successful install of external package)

Can't install go modules

I'm trying to follow a tutorial on how to create a Go microservice but I keep getting this errors whenever I run go mod tidy.
$ go mod tidy
go: finding module for package github.com/velotiotech/watermark-service/internal
go: finding module for package github.com/lithammer/shortuuid/v3
go: finding module for package github.com/go-kit/kit/log
go: downloading github.com/go-kit/kit v0.12.0
go: downloading github.com/lithammer/shortuuid/v3 v3.0.7
go: downloading github.com/velotiotech/watermark-service v0.0.0-20200604060255-debbccd8157b
github.com/chococascante/focus-archery-api/pkg imports
github.com/go-kit/kit/log: github.com/go-kit/kit#v0.12.0: verifying module: github.com/go-kit/kit#v0.12.0: initializing sumdb.Client: reading tree note: malformed note
note:
github.com/chococascante/focus-archery-api/pkg imports
github.com/lithammer/shortuuid/v3: github.com/lithammer/shortuuid/v3#v3.0.7: verifying module: github.com/lithammer/shortuuid/v3#v3.0.7: initializing sumdb.Client: reading tree note: malformed note
note:
github.com/chococascante/focus-archery-api/pkg imports
github.com/velotiotech/watermark-service/internal: github.com/velotiotech/watermark-service#v0.0.0-20200604060255-debbccd8157b: verifying module: github.com/velotiotech/watermark-service#v0.0.0-20200604060255-debbccd8157b: initializing sumdb.Client: reading tree note: malformed note
note:
Try disable the integrity check. Or try to change sum.golang.org to proxy.golang.org.
go env -w GOSUMDB=off

What to do when `go list` doesn't show the newest version of a package?

go list -m -versions gopkg.in/urfave/cli.v1 gives me the following version listing, that has 1.20.0 as the newest version:
gopkg.in/urfave/cli.v1 v1.0.0 v1.1.0 v1.2.0 v1.3.0 v1.3.1 v1.4.0 v1.4.1 v1.5.0 v1.6.0 v1.7.0 v1.7.1 v1.8.0 v1.9.0 v1.10.0 v1.10.1 v1.10.2 v1.11.0 v1.11.1 v1.12.0 v1.13.0 v1.14.0 v1.15.0 v1.16.0 v1.16.1 v1.17.0 v1.17.1 v1.18.0 v1.18.1 v1.19.0 v1.19.1 v1.20.0
However, according gopkg.in, 1.22.4 should be the newest version. 1.22.4 is also the newest release on the package's GitHub repo.
How should I go troubleshooting this problem? Seems like it could be a cache issue.
For background info: the repository originally had dep-based dependency listing (Gopkg.toml), that had version 1.20. I prefer modules, so I initialised go.mod by go mod init $REPO_URL. I later pulled the latest commits from the repo, which had the package version updated to 1.22. I tried re-creating go.mod; it first indicated a problem at that step:
go: converting Gopkg.lock: stat gopkg.in/urfave/cli.v1#v1.22.0: gopkg.in/urfave/cli.v1#v1.22.0: invalid version: go.mod has non-....v1 module path "github.com/urfave/cli" at revision v1.22.0
In the end, I ended up with code that expects version 1.22, but the tools fetch the version 1.20 which isn't forwards compatible, so it doesn't compile.
There is nothing you can do to fix it. The package is broken, so the package author/maintainer needs to fix it.
In short, the project has an invalid go.mod file, which was apparently added after v1.20.0 was released--which is why up to that version is available.
The go.mod file refers to "github.com/urfave/cli", but the package is imported as "gopkg.in/urfave/cli.v1". The go.mod file needs to be updated to reflect the proper import path.
As a work-around, it might work to import the package as "github.com/urfave/cli", but this will break many packages that have "sub" packages that refer to the alternate import path. I'd say it's worth a try in this case, though.

go mod update dependencies and leave local

I have a package that uses some local packages
module mycompany.com/clientname/server
go 1.14
require (
github.com/lib/pq v1.7.0
github.com/99designs/gqlgen v0.11.3
github.com/vektah/gqlparser/v2 v2.0.1
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
mycompany.com/clientname/models v0.0.0-00010101000000-000000000000
mycompany.com/common/utils v0.0.0-00010101000000-000000000000
)
replace mycompany.com/common/utils => ../../common/utils
replace mycompany.com/clientname/models => ../models
the mycompany.com domain doesn't serve the package, it is just a fictional path (and I use it in my gopath ~/go/src/mycompany.com/...)
the problem is that when I run go get -u all, I want the 'valid' packages to be updated (like the github.com/lib/pq or github.com/99designs/gqlgen), but to leave the local (fictional) packages as they are.
but go get -u all just prints out the new versions, then prints the errors on the local packages and then doesn't change go.mod.
go: google.golang.org/grpc upgrade => v1.30.0
go: go.opencensus.io upgrade => v0.22.4
go: github.com/mattn/go-runewidth upgrade => v0.0.9
go: github.com/gogs/chardet upgrade => v0.0.0-20191104214054-4b6791f73a28
go: golang.org/x/text upgrade => v0.3.3
go: github.com/mitchellh/mapstructure upgrade => v1.3.2
go get all: unrecognized import path "mycompany.com/clientname/models": reading https://mycompany.com/clientname/models?go-get=1: 404 Not Found
go get all: unrecognized import path "mycompany.com/common/utils": reading https://mycompany.com/common/utils?go-get=1: 404 Not Found
The error from go get -u was a bug in the go command, fixed in the upcoming Go 1.16 release. See https://golang.org/issue/32567 for detail.
(That said, ideally you should be hosting the modules in version control and using GOPRIVATE=mycompany.com instead of slotting things in locally via replace directives.)

Build dependencis for aws-sdk-go fails on Go version 1.13

I have been trying to build a module on Go v1.13 with dependencies on github.com/aws/aws-lambda-go and github.com/aws/aws-sdk-go which fail on the two imports:
"github.com/aws/aws-sdk-go/aws/service/s3"
"github.com/aws/aws-sdk-go/aws/service/s3/s3manager"
The stderr is as follows for GOOS=linux GOARCH=amd64 go build -o dist/api ./api where the api directory contains my module definition:
api/main.go:11:2: cannot find package "github.com/aws/aws-sdk-go/aws/service/s3" in any of:
/usr/local/go/src/github.com/aws/aws-sdk-go/aws/service/s3 (from $GOROOT)
/u/go/src/github.com/aws/aws-sdk-go/aws/service/s3 (from $GOPATH)
api/main.go:12:2: cannot find package "github.com/aws/aws-sdk-go/aws/service/s3/s3manager" in any of:
/usr/local/go/src/github.com/aws/aws-sdk-go/aws/service/s3/s3manager (from $GOROOT)
/u/go/src/github.com/aws/aws-sdk-go/aws/service/s3/s3manager (from $GOPATH)
Honestly, I have no clue why this is happening and any inputs would be appreciated.
Already tried using go get to ensure that the dependencies have been pulled:
$ go get github.com/aws/aws-sdk-go
and the requested import paths are present under $GOPATH/src/github.com/aws/aws-sdk/go/aws/service/s3 and $GOPATH/src/github.com/aws/aws-sdk/go/aws/service/s3/s3manager
Also, tried clearing the cache using go clean --cache --modcache whilst removing previously pulled modules.
On closer inspection, something that I completely overlooked, the import path is /u/go/src/github.com/aws/aws-sdk-go/aws/service/s3 instead of /u/go/src/github.com/aws/aws-sdk-go/service/s3 with the former having an additional aws subpath inside aws-sdk-go.
Just realized the copy/paste error I had made in the code.
import (
"github.com/aws/aws-sdk-go/aws/service/s3"
"github.com/aws/aws-sdk-go/aws/service/s3/s3manager"
)
instead of
import (
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)

Resources