diff --git a/lib/BrowserStack/Local.pm b/lib/BrowserStack/Local.pm index 0e5d837..655b770 100644 --- a/lib/BrowserStack/Local.pm +++ b/lib/BrowserStack/Local.pm @@ -8,6 +8,7 @@ use IO::Socket; use LWP::Simple; use File::Temp; use Config; +use POSIX (); use Cwd; use File::Temp qw(tempdir); use File::Path qw(make_path); @@ -200,6 +201,9 @@ sub get_available_path { sub platform_url { if ($^O =~ "darwin") { + if ((POSIX::uname())[4] =~ /arm64|aarch64/) { + return "http://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-darwin-arm64"; + } return "http://s3.amazonaws.com/browserStack/browserstack-local/BrowserStackLocal-darwin-x64"; } elsif ($^O =~ /^Win/) { @@ -218,7 +222,21 @@ sub platform_url { sub download_binary { my ($self) = @_; my $url = $self->platform_url(); - getstore($url, $self->{binary_path}); + my $status = getstore($url, $self->{binary_path}); + # If the arm64 binary is not published to the legacy bucket yet, fall back + # to the x64 binary, which still works on Apple Silicon via Rosetta 2 — + # releasing must not turn a working download into a 404. + if (!is_success($status) && $url =~ /-darwin-arm64$/) { + $url =~ s/-darwin-arm64$/-darwin-x64/; + $status = getstore($url, $self->{binary_path}); + } + if (!is_success($status)) { + # don't leave the HTTP error body behind as an "installed" binary — it + # would satisfy the -x check in get_binary_path on every later run and + # stick until the user deletes it by hand + unlink $self->{binary_path}; + die "Failed to download BrowserStackLocal binary (HTTP $status) from $url\n"; + } chmod 0777, $self->{binary_path}; }