Skip to content

Commit 7ad3f59

Browse files
committed
Fix vendoring for golang-http
Tested with a private Go module which was vendored, setting GO111MODULE: off in build_args in stack.yml I also created a nested package called pkg and referenced it as "handler/function/pkg" from handler.go which worked as expected. Closes: #78 Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 1416b6f commit 7ad3f59

File tree

5 files changed

+154
-6
lines changed

5 files changed

+154
-6
lines changed

go.work

-6
This file was deleted.

template/golang-http/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ARG GOFLAGS=""
2222
ARG CGO_ENABLED=0
2323
ENV CGO_ENABLED=${CGO_ENABLED}
2424

25+
RUN sh modules-cleanup.sh
2526

2627
# Run a gofmt and exclude all vendored code.
2728
RUN test -z "$(gofmt -l $(find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./function/vendor/*"))" || { echo "Run \"gofmt -s -w\" on your Golang code"; exit 1; }

template/golang-http/function/go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
module handler/function
22

33
go 1.18
4+
5+
require github.com/openfaas/templates-sdk/go-http v0.0.0-20220408082716-5981c545cb03

template/golang-http/function/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/openfaas/templates-sdk/go-http v0.0.0-20220408082716-5981c545cb03 h1:wMIW4ddCuogcuXcFO77BPSMI33s3QTXqLTOHY6mLqFw=
2+
github.com/openfaas/templates-sdk/go-http v0.0.0-20220408082716-5981c545cb03/go.mod h1:2vlqdjIdqUjZphguuCAjoMz6QRPm2O8UT0TaAjd39S8=
+149
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
#!/usr/bin/env sh
2+
3+
set -e
4+
5+
GO111MODULE=$(go env GO111MODULE)
6+
7+
# move_vendor will copy the function's vendor folder,
8+
# if it exists.
9+
move_vendor() {
10+
if [ ! -d ./function/vendor ]; then
11+
echo "vendor not found"
12+
return
13+
fi
14+
15+
echo "moving function vendor"
16+
mv -f ./function/vendor .
17+
}
18+
19+
20+
# cleanup_gomod will move the function's go module
21+
cleanup_gomod() {
22+
23+
# Nothing to do when modules is explicitly off
24+
# the z prefix protects against any SH wonkiness
25+
# see https://stackoverflow.com/a/18264223
26+
if [ "z$GO111MODULE" = "zoff" ]; then
27+
echo "modules disabled, skipping go.mod cleanup"
28+
return;
29+
fi
30+
31+
if [ ! -f ./function/go.mod ]; then
32+
echo "module not initialized, skipping go.mod cleanup"
33+
return;
34+
fi
35+
36+
echo "cleaning up go.mod"
37+
38+
# Copy the user's go.mod
39+
mv -f ./function/go.mod .
40+
mv -f ./function/go.sum .
41+
42+
# Clean up the go.mod
43+
44+
# Cleanup any sub-module replacements.
45+
# This requires modifying any replace that points to "./*",
46+
# the user has will use this to reference sub-modules instead
47+
# of sub-packages, which we cleanup below.
48+
echo "cleanup local replace statements"
49+
# 1. Replace references to the local folder with `./function`
50+
sed -i 's/=> \.\//=> \.\/function\//' go.mod
51+
52+
53+
# Remove any references to the handler/function module.
54+
# It is ok to just remove it because we will replace it later.
55+
#
56+
# Note that these references may or may not exist. We expect the
57+
# go.mod to have a replace statement _if_ developer has subpackages
58+
# in their handler. In this case they will need a this replace statement
59+
#
60+
# replace handler/function => ./
61+
#
62+
# `go mod` will then add a line that looks like
63+
#
64+
# handler/function v0.0.0-00010101000000-000000000000
65+
#
66+
# both of these lines need to be replaced, this grep selects everything
67+
# _except_ those offending lines.
68+
grep -v "\shandler/function" go.mod > gomod2; mv gomod2 go.mod
69+
70+
# Now update the go.mod
71+
#
72+
# 1. use replace so that imports of handler/function use the local code
73+
# 2. we need to rename the module to handler because our main.go assumes
74+
# this is the package name
75+
go mod edit \
76+
-replace=handler/function=./function \
77+
-module handler
78+
79+
80+
81+
if [ "$DEBUG" -eq 1 ]; then
82+
cat go.mod
83+
echo ""
84+
fi
85+
}
86+
87+
88+
# cleanup_vendor_modulestxt will cleanup the modules.txt file in the vendor folder
89+
# this file is needed when modules are enabled and it must be in sync with the
90+
# go.mod. To function correctly we need to modify the references to handler/function,
91+
# if they exist.
92+
cleanup_vendor_modulestxt() {
93+
if [ ! -d ./vendor ]; then
94+
echo "no vendor found, skipping modules.txt cleanup"
95+
return
96+
fi
97+
98+
# Nothing to do when modules is explicitly off
99+
# the z prefix protects against any SH wonkiness
100+
# see https://stackoverflow.com/a/18264223
101+
if [ "z$GO111MODULE" = "zoff" ]; then
102+
echo "modules disabled, skipping modules.txt cleanup"
103+
return;
104+
fi
105+
106+
echo "cleanup vendor/modules.txt"
107+
108+
# just in case
109+
touch "./vendor/modules.txt"
110+
111+
# when vendored, we need to do similar edits to the vendor/modules.txt
112+
# as we did to the go.mod
113+
114+
# 1. we need to replace any possible copy of the handler code
115+
rm -rf vendor/handler && \
116+
117+
# 2. in modules.txt, we remove existing references to the handler/function
118+
# we reconstruct these in the last step
119+
grep -v "\shandler/function" ./vendor/modules.txt> modulestext; mv modulestext ./vendor/modules.txt
120+
121+
# 3. Handle any other local replacements.
122+
# any replace that points to `./**` needs to be udpat echo "cleanup local replace statements"
123+
sed -i 's/=> \.\//=> \.\/function\//' ./vendor/modules.txt
124+
125+
# 4. To make the modules.txt consistent with the new go.mod,
126+
# we add the mising replace to the vendor/modules.txt
127+
echo "## explicit" >> ./vendor/modules.txt
128+
echo "# handler/function => ./function" >> ./vendor/modules.txt
129+
130+
if [ "$DEBUG" -eq 1 ]; then
131+
cat ./vendor/modules.txt;
132+
echo ""
133+
fi
134+
}
135+
136+
# has_local_replacement checks if the file contains local go module replacement
137+
has_local_replacement() {
138+
return "$(grep -E -c '=> \./\S+' "$1")"
139+
}
140+
141+
142+
################
143+
# main
144+
################
145+
move_vendor
146+
147+
cleanup_gomod
148+
149+
cleanup_vendor_modulestxt

0 commit comments

Comments
 (0)