diff --git a/main.go b/main.go index cc59ed22..c84aa7fd 100644 --- a/main.go +++ b/main.go @@ -344,6 +344,11 @@ func (s Step) InstallDeps(xcpretty bool) error { func (s Step) Run(cfg Config) (Result, error) { log.SetEnableDebugLog(cfg.Verbose) + err := resetLaunchServices() + if err != nil { + log.Warnf("Failed to apply simulator boot workaround, error: %s", err) + } + // Boot simulator if cfg.SimulatorDebug != never { log.Infof("Enabling Simulator verbose log for better diagnostics") diff --git a/simulator.go b/simulator.go index a96d043a..c044a9e8 100644 --- a/simulator.go +++ b/simulator.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "io/ioutil" + "path/filepath" "strings" "time" @@ -100,3 +101,37 @@ func simulatorCollectDiagnostics() (string, error) { return diagnosticsOutDir, nil } + +// Reset launch services database to avoid Big Sur's sporadic failure to find the Simulator App +// The following error is printed when this happens: "kLSNoExecutableErr: The executable is missing" +// Details: +// - https://stackoverflow.com/questions/2182040/the-application-cannot-be-opened-because-its-executable-is-missing/16546673#16546673 +// - https://ss64.com/osx/lsregister.html +func resetLaunchServices() error { + cmd := command.New("sw_vers", "-productVersion") + macOSVersion, err := cmd.RunAndReturnTrimmedCombinedOutput() + if err != nil { + return err + } + + if strings.HasPrefix(macOSVersion, "11.") { // It's Big Sur + cmd := command.New("xcode-select", "--print-path") + xcodeDevDirPath, err := cmd.RunAndReturnTrimmedCombinedOutput() + if err != nil { + return err + } + + simulatorAppPath := filepath.Join(xcodeDevDirPath, "Applications", "Simulator.app") + + cmdString := "/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregister" + cmd = command.New(cmdString, "-f", simulatorAppPath) + + log.Infof("Applying launch services reset workaround before booting simulator") + _, err = cmd.RunAndReturnTrimmedCombinedOutput() + if err != nil { + return err + } + } + + return nil +}