Skip to content
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

SERVERLESS-2296 | Support private dependencies in Go functions by supporting vendoring #30

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 23 additions & 11 deletions golang1.17/bin/compile
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,44 @@ def build(source_dir, target_dir):
}

gomod = "%s/go.mod" % source_dir
vendor = "%s/vendor" % source_dir

has_gomod = exists(gomod)
deps_vendored = exists(vendor)

if has_gomod:
print("Detected go.mod file.")
if deps_vendored:
print("Detected vendor directory. Will skip downloading modules.")

if os.environ.get("__NIM_REMOTE_BUILD"):
if exists(gomod):
print("downloading modules")
ret = subprocess.call(["go", "mod", "download"], cwd=source_dir, env=env)
if ret != 0:
sys.exit(ret)
if has_gomod:
if not deps_vendored:
mattwelke marked this conversation as resolved.
Show resolved Hide resolved
print("downloading modules")
ret = subprocess.call(["go", "mod", "download"], cwd=source_dir, env=env)
if ret != 0:
sys.exit(ret)
mattwelke marked this conversation as resolved.
Show resolved Hide resolved
else:
print("initializing modules")
ret = subprocess.call(["go", "mod", "init", "exec"], cwd=source_dir, env=env)
if ret != 0:
sys.exit(ret)
else:
with open(os.devnull, "w") as dn:
if exists(gomod):
ret = subprocess.call(["go", "mod", "download"], cwd=source_dir, env=env, stderr=dn, stdout=dn)
if ret != 0:
print("cannot download modules")
return
if has_gomod:
if not deps_vendored:
ret = subprocess.call(["go", "mod", "download"], cwd=source_dir, env=env, stderr=dn, stdout=dn)
if ret != 0:
print("cannot download modules")
return
else:
ret = subprocess.call(["go", "mod", "init", "exec"], cwd=source_dir, env=env, stdout=dn, stderr=dn)
if ret != 0:
print("cannot init modules")
return

ldflags = "-s -w"
gobuild = ["go", "build", "-o", target, "-ldflags", ldflags]
gobuild = ["go", "build"] + (["-mod=vendor"] if deps_vendored else []) + ["-o", target, "-ldflags", ldflags]
if os.environ.get("__OW_EXECUTION_ENV"):
ldflags += " -X main.OwExecutionEnv=%s" % os.environ["__OW_EXECUTION_ENV"]
if os.environ.get("__NIM_REMOTE_BUILD"):
Expand Down