git: d71b4ca77528 - main - Mk/Uses/go.mk: Block modules download when CLEAN_FETCH_ENV is defined

From: Dmitri Goutnik <dmgk_at_FreeBSD.org>
Date: Fri, 13 May 2022 18:39:35 UTC
The branch main has been updated by dmgk:

URL: https://cgit.FreeBSD.org/ports/commit/?id=d71b4ca7752864918dea9b92e7f65c15deb3d290

commit d71b4ca7752864918dea9b92e7f65c15deb3d290
Author:     Dmitri Goutnik <dmgk@FreeBSD.org>
AuthorDate: 2022-05-13 18:34:15 +0000
Commit:     Dmitri Goutnik <dmgk@FreeBSD.org>
CommitDate: 2022-05-13 18:38:01 +0000

    Mk/Uses/go.mk: Block modules download when CLEAN_FETCH_ENV is defined
    
    Additionally, stop overriding post-fetch and post-extract targets.
    
    PR:             258474
    Differential Revision:  https://reviews.freebsd.org/D35145
---
 Mk/Uses/go.mk | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/Mk/Uses/go.mk b/Mk/Uses/go.mk
index b73e198e29fe..39585e0c9946 100644
--- a/Mk/Uses/go.mk
+++ b/Mk/Uses/go.mk
@@ -128,6 +128,8 @@ GO_ENV+=	GOPATH="${GO_GOPATH}" \
 .    if defined(GO_MODULE)
 GO_MODNAME=	${GO_MODULE:C/^([^@]*)(@([^@]*)?)/\1/}
 .      if empty(DISTFILES:Mgo.mod\:*) && empty(DISTFILES:Mgo.mod)
+# Unless already setup for download by other means,
+# arrange to pull go.mod and distribution archive from GOPROXY.
 GO_MODVERSION=	${GO_MODULE:C/^([^@]*)(@([^@]*)?)/\2/:M@*:S/^@//:S/^$/${DISTVERSIONFULL}/}
 GO_MODFILE=	${GO_MODVERSION}.mod
 GO_DISTFILE=	${GO_MODVERSION}.zip
@@ -162,26 +164,37 @@ _USES_POST+=	go
 .if defined(_POSTMKINCLUDED) && !defined(_INCLUDE_USES_GO_POST_MK)
 _INCLUDE_USES_GO_POST_MK=	yes
 
-.  if !target(post-fetch) && ${go_ARGS:Mmodules} && defined(GO_MODULE)
-post-fetch:
+.  if ${go_ARGS:Mmodules} && defined(GO_MODULE)
+_USES_fetch+=	200:go-pre-fetch 800:go-post-fetch
+# Check that pkg can be installed or is already available,
+# otherwise it will be impossible to install go and fetch dependencies.
+go-pre-fetch:
+.    if defined(CLEAN_FETCH_ENV) && !exists(${PKG_BIN})
+	@${ECHO_MSG} "===> CLEAN_FETCH_ENV is defined, cannot download Go modules (pkg and go are required)"; \
+	exit 1
+.    endif
+# Download all required build dependencies to GOMODCACHE.
+go-post-fetch:
 	@${ECHO_MSG} "===> Fetching ${GO_MODNAME} dependencies";
 	@(cd ${DISTDIR}/${DIST_SUBDIR}; \
 		[ -e go.mod ] || ${RLN} ${GO_MODFILE} go.mod; \
 		${SETENV} ${GO_ENV} GOPROXY=${GO_GOPROXY} ${GO_CMD} mod download -x all)
 .  endif
 
-.  if !target(post-extract)
-.    if empty(go_ARGS)
-post-extract:
+_USES_extract+=	800:go-post-extract
+.  if empty(go_ARGS)
+# Legacy (GOPATH) build mode, setup directory structure expected by Go for the main module.
+go-post-extract:
 	@${MKDIR} ${GO_WRKSRC:H}
 	@${LN} -sf ${WRKSRC} ${GO_WRKSRC}
-.    elif ${go_ARGS:Mmodules} && defined(GO_MODULE)
-post-extract:
+.  elif ${go_ARGS:Mmodules} && defined(GO_MODULE)
+# Module-aware build mode. Although not strictly necessary (all build dependencies should be
+# already in MODCACHE), vendor them so we can patch them if needed.
+go-post-extract:
 	@${ECHO_MSG} "===> Tidying ${GO_MODNAME} dependencies";
 	@(cd ${GO_WRKSRC}; ${SETENV} ${GO_ENV} GOPROXY=${GO_MODCACHE} ${GO_CMD} mod tidy -e)
 	@${ECHO_MSG} "===> Vendoring ${GO_MODNAME} dependencies";
 	@(cd ${GO_WRKSRC}; ${SETENV} ${GO_ENV} GOPROXY=${GO_MODCACHE} ${GO_CMD} mod vendor -e)
-.    endif 
 .  endif
 
 .  if !target(do-build) && empty(go_ARGS:Mno_targets)