-
Notifications
You must be signed in to change notification settings - Fork 57
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
feat: automatically configure vendor mode #74
feat: automatically configure vendor mode #74
Conversation
9a81df8
to
894296a
Compare
When a vendor folder is seen, we need to disable the go module mode, instead we need to use GOPATH mode. This is a simple toggle of one env variable, using a script means that users do not need to set any build args. Signed-off-by: Lucas Roesler <roesler.lucas@gmail.com>
894296a
to
6ae0208
Compare
- To use Go modules without vendoring, the default already is set `GO111MODULE=on` but you also can make that explicit by adding `--build-arg GO111MODULE=on` to `faas-cli up`, you can also use `--build-arg GOPROXY=https://` if you want to use your own mirror for the modules | ||
- You can also Go modules with vendoring, run `go mod vendor` in your function folder and add `--build-arg GO111MODULE=off --build-arg GOFLAGS='-mod=vendor'` to `faas-cli up` | ||
- To use Go modules without vendoring, the default already is set `GO111MODULE=on` but you also can make that explicit by adding `--build-arg GO111MODULE=on` to `faas-cli up`, you can also use `--build-arg GOPROXY=https://` if you want to use your own mirror for the modules. | ||
- You can also Go modules with vendoring, run `go mod vendor`, the build process will automatically detect the vendor folder and configure the Go environment correctly. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should still mention that GO111MODULE
will be forced to off
.
auto-magic can end up being more confusing than setting explicit values.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you run me through this on the next community call please @LucasRoesler ?
# automatically set the required module related flags | ||
# when a vendor folder is detected. | ||
# | ||
# Currently, in Go 1.18, Go Workspaces are incompatible |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will be a dumb question but if this was 1.17 would we need this tweak?
I am kinda thinking how all sh scripts can be avoided but I thought vendor files were compatible with workspaces, it is intriguing. I think http templates are super simple compared to past sh scripts before 1.18 and this amount seems OK, I will chase this incompatibility issue. Looks good to me in this way tho, it is more clear than old shell scripts
I will be kinda confused again but I thought -mod=vendor was the default flag when Go detects vendor folder since 1.13, when did this actually change so we had to specify it explicitly? I am very bad vendor user(not using at all since 1.13) If you have a look at the footnotes, it says -mod=vendor is the default here https://www.ardanlabs.com/blog/2020/04/modules-06-vendoring.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actually -mod readonly also reads the vendor as far as I understand from this thread golang/go#30404, so I think shell script may no longer needed? the end user needs to do -mod=readonly as you pointed out here Lucas https://github.com/LucasRoesler/openfaas-golang-template-workspace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrwormhole i don't 100% follow. Were you able to get a workspaces project to build with the vendor by using -mod=vendor -mod=readonly
?
The main reason for the script, is that when using workspaces, go build
ignores the vendor folder and attempts to download the modules again. When a vendor folder is detected, disabling modules forces Go to use the GOPATH, which then, correctly, detects and uses the vendor folder as expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-mod=readonly should use vendor if vendor exists, I also don't know why this config needs to specify -mod=vendor to get vendor files, it is automatic when not specified, default is -mod=readonly, -mod=mod is the one who only looks up for go modules and ignores vendor completely
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand what you are saying, but
- this code does not use or require
-mod=vendor
- you are 100% right, we normally expect
go build
to automatically detect thevendor
folder, however, the current behavior of workspaces seems to ignore this. I tried it (I had exactly the same hope that you did) but it was not building in the correct way. Even with avendor
folder present it was still downloading the modules.
Perhaps you can share a working example repo with the changes you would like to see?
When I tried to use vendor with the go.work
, the only time I could get it to detect the vendor
folder was by disabling modules. This is what the script does. It automates detecting this situation instead of requiring the developer to pass an additional build arg.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so I actually created the same repository but without -mod vendor flag, I vendored my dependencies (which is the user responsibility) and deleted -mod=vendor and made go modules ON, it actually detected and built with vendor folder but the key here was I deleted the repository of my module, I do think -mod vendor is completely unneeded and shouldn't be used.
https://github.com/MrWormHole/openfaas-golang-template-workspace
the end-user should be able to know as a Go dev, what could be the use case for enabling go modules and doing -mod vendor. Vendor folder should exist only for disaster recovery, else it will download all deps with go.mod and not use vendor folder. Also go mod vendor should be called explicitly by developers and stored in the repository, else it is a problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we can move forward without using additional bash and if statements, I'd be interested to see that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Lucas is right @LucasRoesler depending on your Go version it gets set to default to -mod=readonly, I would keep it and resolve these comments. It will enhance the dev usage of these templates even though it sets it internally.
I am on version 1.17.11 and -mod=mod always runs when I don't specify -mod=vendor with go modules "OFF" or "ON", sorry for the confusion I had earlier. One theory from my side is that alpine images(Dockerfile this uses) of 1.18 sets it to -mod=mod, I didn't confirm yet today but if you fork it, you will see this one builds https://github.com/MrWormHole/openfaas-golang-template-workspace without on or off specification because -mod flag figures it out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrwormhole can you clarify your recommendations in a few bullets or steps, or are you retracting what you said?
Go workspaces are working for me in VSCode, and I believe for @LucasRoesler too for highlighting and intellisense. |
I think I have found the issue with #73, we seem to have assumed that @kevin-lindsay-1 is using the golang-middleware template and have examples that work with it. He's actually must be using the golang-http template. Now, we did ask for this information and a repro several times in the issue, then yesterday what he said revealed it was actuallt the golang-http template, and not golang-middleware. When I run Lucas' example, I am getting a This is probably what Kevin is referring to when he says that an issue hasn't been fixed for 4 months. His code sample that he sent us via Discord, will not work for the community because it always assumes a vendor folder, and we know that not everyone uses vendoring, furthermore, this PR automatically turns on vendoring whenever a vendor folder is present. Excerpt:
Here's how I reproduced the error.
Then:
|
@LucasRoesler @alexellis I feel like we can remove the shell scripts, there is really nothing that will go wrong if GOFLAGs are not specified by someone else if the Go version of that person is +v1.14. I did check both templates Here is what I got from official docs (https://go.dev/ref/mod#build-commands)
Technically I do go mod vendor as a separate process. I also avoid using -mod flag because go figures its own. Some common cases that go wrong when GOFLAGS is specified (this fails if you have a vendor folder or you don't have a vendor folder, best to not specify(GOFLAGS) here and let it handle its own) (this also fails if there is no vendor folder, will not fail if you have a vendor folder, will change to -mod=vendor) Also as a second note, I have seen this when I opened up my IDE, I think IDEs do set -mod flag sometimes, when I created go.work file, it disappeared, all the go installations don't have global GOfLAGS for -mod flag. Check with Last note: @kevin-lindsay-1 I don't set GOFLAGS as well and enable vendoring by doing go mod vendor(internally Go handles it, not GOFLAGS), it works fine for the private repos with golang-middlewares template |
@mrwormhole try it with the golang-http template. The middleware template has no issue at present. |
No longer needed because of #83 |
When a vendor folder is seen, we need to disable the go module mode,
instead we need to use GOPATH mode. This is a simple toggle of one
env variable, using a script means that users do not need to set any
build args.
How Has This Been Tested?
I have updated my example repo to include a function using vendor and with no build args, see the
jules
function here https://github.com/LucasRoesler/golang-http-template-examplesHow are existing users impacted? What migration steps/scripts do we need?
This will remove any manual steps to enable vendor mode in the templates, it should improve the DX
Checklist:
I have:
git commit -s