Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error launching Chrome on MacOs : Permission denied #2337

Closed
borjafdezgauna opened this issue Oct 9, 2023 · 8 comments
Closed

Error launching Chrome on MacOs : Permission denied #2337

borjafdezgauna opened this issue Oct 9, 2023 · 8 comments

Comments

@borjafdezgauna
Copy link

borjafdezgauna commented Oct 9, 2023

Hi there,

I am using PuppeteerSharp 12 on a MacBook Air running MacOs Ventura 13.4

Description

It runs perfectly on Windows, but on Mac, the LaunchAsync() methods throws a Permission denied exception

Complete minimal example reproducing the issue

string downloadDir = "..."; //a folder inside the local app data folder, also fails without setting it
if (!Directory.Exists(downloadDir))
    Directory.CreateDirectory(downloadDir);

BrowserFetcherOptions options = new BrowserFetcherOptions() { Path = downloadDir };
BrowserFetcher browserFetcher = new BrowserFetcher(options);

InstalledBrowser installedBrowser = await browserFetcher.DownloadAsync(Chrome.DefaultBuildId);

IEnumerable<InstalledBrowser> installedBrowsers = browserFetcher.GetInstalledBrowsers();
string exePath = installedBrowsers.First(browser => browser.BuildId == Chrome.DefaultBuildId).GetExecutablePath();

var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
        Headless = useHeadless,
        DefaultViewport = new ViewPortOptions() { Width = 800, Height = 600 },
        WebSocketFactory = CreateWebSocket,
        ExecutablePath = exePath,
        Timeout = defaultTimeout
});

Expected behavior:

It should launch Chrome

Actual behavior:

It throws an exception: System.ComponentModel.Win32Exception (13) Permission denied (ForkAndExecProcess)

Versions

I am using PuppeteerSharp 12, but I have also tried it with 11.0.4

I am using .Net Core 3.1. Edit: With NET6 it works properly

Additional info

Edit 1:
_If I try to run the downloaded copy of "Google Chrome for Testing.app" from a terminal, then I get the error: "The application cannot be opened for an unexpected reason, error=Error Domain=NSOSStatusErrorDomain Code=-10661 "(null)" UserInfo={_LSLine=4106, _LSFunction=LSOpenStuffCallLocal}

Edit 2:
I tried to run directly the executable inside the Contents/MacOS and then it said "The use of Rosetta to run the x64 version of Chromium on Arm is neither tested nor maintained, and unexpected behavior will likely result. Please check that all tools that spawn Chromium are Arm-native". I am running on a MacBook Air. I downloaded the arm version of Google Chrome for Testing and it ran after doing "sudo xattr -cr 'Google Chrome for Testing.app'".
System.Runtime.InteropServices.RuntimeInformation.OSArchitecture returns x64. I think it should be Arm64, should'nt it?

Any help is appreciated

Cheers

@jnyrup
Copy link
Contributor

jnyrup commented Oct 19, 2023

What does browserFetcher.Platform returns?
When BrowserFetcherOptions.Platform is not specified, BrowserFetcher tries to detect the current platform.

@jnyrup
Copy link
Contributor

jnyrup commented Oct 19, 2023

It might be because we check for OSX, but the newer mac OS identifies itself as MACOS.
See dotnet/runtime#44120

I don't have a Mac at hand to test this out, but a guess is to change

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))

to

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Create("MACOS")))

@borjafdezgauna what does RuntimeInformation.IsOSPlatform(OSPlatform.OSX) and RuntimeInformation.IsOSPlatform(OSPlatform.Create("MACOS")) return on your Mac?

@borjafdezgauna
Copy link
Author

@jnyrup, this is what my mac returns:

BrowserFetcher.Platform => MacOS
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) => true
RuntimeInformation.IsOSPlatform(OSPlatform.Create("MACOS")) => false

Thanks!

@jnyrup
Copy link
Contributor

jnyrup commented Oct 19, 2023

I missed your

System.Runtime.InteropServices.RuntimeInformation.OSArchitecture returns x64. I think it should be Arm64, should'nt it?

Does it return that on both net core 3.1 and net6?
What does RuntimeInformation.ProcessArchitecture return for you on those two .net versions
From dotnet/runtime#63865 it seems the culprit could be that older .net versions use emulation.

As a workaround, try explicitly setting the platform.

BrowserFetcherOptions options = new BrowserFetcherOptions() { Path = downloadDir, Platform = Platform.MacOSArm64 };

@borjafdezgauna
Copy link
Author

borjafdezgauna commented Oct 19, 2023

On Net Core 3.1:
BrowserFetcher.Platform => MacOS
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) => true
RuntimeInformation.IsOSPlatform(OSPlatform.Create("MACOS")) => false
RuntimeInformation.ProcessArchitecture => X64

On Net6:
BrowserFetcher.Platform => MacOS
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) => true
RuntimeInformation.IsOSPlatform(OSPlatform.Create("MACOS")) => true
RuntimeInformation.ProcessArchitecture => X64

Oh my!! I am quite sure I tested it with Net6 and that it didn't work, but I did it again now, and it launched Chrome properly. I don't know how, but I must have done something wrong before, and it works with Net6. I will edit my post now

Any thoughts on how I could fix it for NetCore 3.1? Some of the users cannot update their MacOS and we're basically stuck with NetCore3.1 to support their OS

Thank you very much!

@jnyrup
Copy link
Contributor

jnyrup commented Oct 19, 2023

I don't have any good solutions, but maybe something like this hack would be enough for you?

Platform? platform = null;
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
    platform = Platform.MacOSArm64;
}

BrowserFetcherOptions options = new BrowserFetcherOptions() { Path = downloadDir, Platform = platform };

@kblok Have you considered adding macos-latest or macos-13 to the the test matrix?

@kblok
Copy link
Member

kblok commented Oct 19, 2023

@jnyrup, it's on my to-do list. The only tricky part there is the certificates we need to install.

@borjafdezgauna
Copy link
Author

@jnyrup I have done some tests with your hack and it seems that the problem is not related with the X64/XARM64 issue. Changing the platform manually to ARM64 downloaded the arm64 version correctly, but it wouldn't run from the NetCore3.1 app. I believe this is because NetCore3.1 emulates x64 and cannot execute the arm64 version of Chrome in emulated mode.

I managed to make it work after running these two commands from the app (using a Process with FileName="/bin/bash" and Arguments=-c "[COMMAND]"):

xattr -cr [DOWNLOAD_DIR]
chmod -R +x [DOWNLOAD_DIR]

I think this should have no side effect in Net6 or older versions of Mac, so I think it's quite safe

Thank you so much!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants