Skip to content

Commit

Permalink
Avoid using syscall.Sysctl to get macOS version
Browse files Browse the repository at this point in the history
Looks like `syscall.Sysctl` is not reliable when it comes to get macOS
version on different arch (Intel vs silicon) because of [0]. This PR
uses `sw_vers` cmd result which seems to be consistent on both platform.

[0] golang/go#58722

fixes: #4086
  • Loading branch information
praveenkumar committed Apr 3, 2024
1 parent a229bd6 commit 2331271
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pkg/os/darwin/release_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ package darwin
import (
"fmt"
"sync"
"syscall"

"github.com/Masterminds/semver/v3"
"github.com/crc-org/crc/v2/pkg/crc/logging"
"github.com/crc-org/crc/v2/pkg/os"
"github.com/crc-org/crc/v2/pkg/strings"
"github.com/pkg/errors"
)

Expand All @@ -20,8 +21,12 @@ var (

func AtLeast(targetVersion string) (bool, error) {
once.Do(func() {
macCurrentVersion, errGettingVersion = syscall.Sysctl("kern.osproductversion")
logging.Debugf("kern.osproductversion is: %s", macCurrentVersion)
// syscall.Sysctl("kern.osproductversion") is not providing the consistent version info for intel and silicon macs
// so using sw_vers -productVersion to get the version info consistently
// https://github.com/golang/go/issues/58722
macCurrentVersion, _, errGettingVersion = os.RunWithDefaultLocale("sw_vers", "-productVersion")
macCurrentVersion = strings.TrimTrailingEOL(macCurrentVersion)
logging.Debugf("sw_vers -productVersion is: %s", macCurrentVersion)
})
if errGettingVersion != nil {
return false, errGettingVersion
Expand Down

0 comments on commit 2331271

Please sign in to comment.