Skip to content

Commit ecb3282

Browse files
committed
Fix Go version discovery on Ubuntu versions
1 parent 4433e8c commit ecb3282

1 file changed

Lines changed: 31 additions & 10 deletions

File tree

uyuni-tools.spec

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ BuildRequires: golang(API) >= 1.25
8282
# suse_version
8383

8484
%if 0%{?ubuntu}
85-
%if 0%{?ubuntu} >= 2404
86-
# Will use the default golang of the OS which is higher than minimal required go_version
87-
BuildRequires: golang
88-
%else
89-
# Overwrite with new go version the OS default
9085
%define go_version 1.22
91-
BuildRequires: golang-%{go_version}
86+
# On Ubuntu 22.04, the metapackage golang is too old (1.18) and
87+
# we must use the versioned package name specifically for this OS version.
88+
%if 0%{?ubuntu} == 2204
89+
BuildRequires: golang-%{go_version}-go
90+
%else
91+
BuildRequires: golang >= %{go_version}
9292
%endif
9393
%endif
9494
# ubuntu
@@ -330,15 +330,36 @@ go_tags=""
330330

331331
go_path=""
332332
%if 0%{?ubuntu}
333-
if test -d /usr/lib/go-%{go_version}/bin/; then
334-
go_path=/usr/lib/go-%{go_version}/bin/
333+
# Pinpoint the highest binary
334+
go_path=$(find /usr/lib/go-*/bin/go 2>/dev/null | sort | tail -n 1)
335+
[ -z "$go_path" ] && go_path="/usr/bin/go"
336+
337+
if [ -x "$go_path" ]; then
338+
# Query GOVERSION
339+
v_str=$("$go_path" env GOVERSION 2>/dev/null)
340+
# Strip the leading 'go' prefix
341+
v_str="${v_str#go}"
342+
# Parse required and found versions
343+
echo "%{go_version}" | { IFS="." read -r req_major req_minor req_patch;
344+
echo "$v_str" | { IFS="." read -r max_major max_minor max_patch;
345+
# Fallbacks to guarantee whole integers if trailing elements are empty
346+
[ -z "$max_patch" ] && max_patch=0
347+
[ -z "$max_minor" ] && max_minor=0
348+
if [ "$max_major" -lt "$req_major" ] || { [ "$max_major" -eq "$req_major" ] && [ "$max_minor" -lt "$req_minor" ]; }; then
349+
echo "ERROR: Found Go version ${v_str} at ${go_path}, but version >= %{go_version} is required."
350+
exit 1
351+
fi
352+
}; }
353+
# Format the path variable suffix for the rest of the spec compilation steps
354+
go_path="${go_path%go}"
355+
else
356+
echo "ERROR: No Go compiler found at ${go_path} or /usr/bin/go"
357+
exit 1
335358
fi
336359
%else
337360
%if "%{?_go_bin}" != ""
338361
go_path='%{_go_bin}/'
339362
%endif
340-
# "_go_bin" != ""
341-
342363
%endif
343364
# ubuntu
344365

0 commit comments

Comments
 (0)