playwright-dotnet icon indicating copy to clipboard operation
playwright-dotnet copied to clipboard

[BUG] Exception thrown when using playwright in a single-file published program

Open jeroenpelgrims opened this issue 3 years ago • 16 comments

Context:

  • Playwright Version: 1.24.1
  • Operating System: Windows 10
  • .NET version: .NET 6
  • Browser: All
  • Extra:

Code Snippet

See code to reproduce this bug here: https://github.com/resurge/PlaywrightSingleFilePathError

Describe the bug When publishing a project as single file and running the published executable the following error will be shown:

Unhandled exception. System.ArgumentException: The path is empty. (Parameter 'path')
   at System.IO.Path.GetFullPath(String path)
   at System.IO.FileInfo..ctor(String originalPath, String fullPath, String fileName, Boolean isNormalized)
   at System.IO.FileInfo..ctor(String fileName)
   at Microsoft.Playwright.Helpers.Driver.GetExecutablePath() in /_/src/Playwright/Helpers/Driver.cs:line 41
   at Microsoft.Playwright.Transport.StdIOTransport.GetProcess() in /_/src/Playwright/Transport/StdIOTransport.cs:line 119
   at Microsoft.Playwright.Transport.StdIOTransport..ctor() in /_/src/Playwright/Transport/StdIOTransport.cs:line 44
   at Microsoft.Playwright.Playwright.CreateAsync() in /_/src/Playwright/Playwright.cs:line 44
   at Program.<Main>$(String[] args) in C:\Users\jeroe\Documents\repos\PlaywrightSingleFilePathError\Program.cs:line 3
   at Program.<Main>(String[] args)

I would expect the program to behave the same way as a regular Debug or Release build.
But choosing to publish as a single file causes the behaviour of the executable to change. (And not work)

The following tickets were about the same bug but were closed because of no code to reproduce:

  • https://github.com/microsoft/playwright-dotnet/issues/1952
  • https://github.com/microsoft/playwright-dotnet/issues/1527

EDIT: Some other issues I found relating to this bug:

  • https://github.com/microsoft/playwright-dotnet/issues/1069 Not sure how the solution here works because Playwright.CreateAsync doesn´t accept parameters.
  • https://github.com/microsoft/playwright-dotnet/issues/1274 This ticket is closed with comment "not supported", but no reason is given as to why not.

jeroenpelgrims avatar Aug 15 '22 16:08 jeroenpelgrims

My two cents here. I don't think an end-to-end testing library needs to support single-file publishing.

kblok avatar Aug 16 '22 13:08 kblok

While that seems to be the initial reason Playwright was created it is used for more that that.
I myself use it for scraping or transforming html to PDF. I would like to be able to share a single executable with users instead of having to make a zip file or installer as release.

I agree more with the description that is written in the README of this repo.
(Browser automation in general, not just specifically for testing)

Playwright for .NET is the official language port of Playwright, the library to automate Chromium, Firefox and WebKit with a single API.

The linked issues show that there is a need for this type of publishing.
EDIT: I found some more tickets about people using it this way, I added them above.

jeroenpelgrims avatar Aug 16 '22 14:08 jeroenpelgrims

Echoing @jeroenpelgrims comments. We're using Playwright for generating .png files as part of the Azure PlantUML project and it would be great if we could offer a single executable to users who wish to generate their own diagram sprites.

travisnielsen avatar Jan 01 '23 17:01 travisnielsen

@kblok do you still have a patch for the issue #2493 ? I think it's the same issue as this one.

ianis58 avatar Mar 15 '23 21:03 ianis58

@mxschmitt, the solution I was using was saving the driver in a shared location, LocalApplicationData. I don't know if that's what we'd want to do here.

kblok avatar Mar 20 '23 12:03 kblok

Hi @kblok ,

Is this issue referred in #2493 ... done?

vmeganathan81 avatar Mar 28 '23 15:03 vmeganathan81

@vmeganathan81 I bet so.

kblok avatar Mar 28 '23 15:03 kblok

Any ETA on this?

vmeganathan81 avatar Mar 28 '23 15:03 vmeganathan81

@vmeganathan81 I bet so.

Okay, let me test with latest version... thanks

vmeganathan81 avatar Mar 28 '23 15:03 vmeganathan81

@jeroenpelgrims ... Are we going to resolve this or it would take more time?

vmeganathan81 avatar Apr 04 '23 01:04 vmeganathan81

@jeroenpelgrims ... Are we going to resolve this or it would take more time?

I'll try to test tomorrow. I actually haven't worked on the project this was for since the creation of this issue.

You verified it working on your end?

EDIT: Still doesn't seem to work although I get a clearer error message now.

Unhandled exception. System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.
   at System.Reflection.RuntimeAssembly.get_CodeBase()
   at Microsoft.Playwright.Helpers.Driver.GetExecutablePath() in /_/src/Playwright/Helpers/Driver.cs:line 47
   at Microsoft.Playwright.Transport.StdIOTransport.GetProcess() in /_/src/Playwright/Transport/StdIOTransport.cs:line 114
   at Microsoft.Playwright.Transport.StdIOTransport..ctor() in /_/src/Playwright/Transport/StdIOTransport.cs:line 44
   at Microsoft.Playwright.Playwright.CreateAsync() in /_/src/Playwright/Playwright.cs:line 44
   at ResumeBuilder.Pdf.PdfBuilder.SavePdf(String htmlPath, String outputFile) in C:\Users\....\PdfBuilder.cs:line 9
   at Program.<Main>$(String[] args) in C:\Users\...\Program.cs:line 11
   at Program.<Main>(String[] args)

Same result when publishing self contained or framework dependent.

jeroenpelgrims avatar Apr 04 '23 08:04 jeroenpelgrims

@mxschmitt ,

The solution @kblok mentioned is that merged and available with latest version?

vmeganathan81 avatar Apr 05 '23 17:04 vmeganathan81

@mxschmitt any update on this open issue?

vmeganathan81 avatar Apr 07 '23 17:04 vmeganathan81

Same issue here. We publish a single-file self-contained executable in a docker image, alongside the playwright stuff.

When dealing with docker images that is significantly more convenient and eliminates many dependency problems (which is why it is such a popular approach in the docker world).

But playwright conflicts with that deployment model.

lonix1 avatar Sep 08 '23 23:09 lonix1

This issue still persist as of this comment (version 1.41.2)

jsoctocat avatar Feb 06 '24 00:02 jsoctocat

Starting from v1.44 (will be released in 3-4 weeks) this should be fixed: https://github.com/microsoft/playwright-dotnet/pull/2909

Browsers need to be installed via Microsoft.Playwright.Program.Main(["install"]);

mxschmitt avatar Apr 15 '24 10:04 mxschmitt

I still get this issue using v1.44 when running Microsoft.Playwright.Program.Main(["install"]).

Docker build error:

[final 3/4] RUN /app/PayPowerBill install: 1.565 Unhandled Exception: System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle. 1.565 at System.Reflection.Runtime.Assemblies.RuntimeAssemblyInfo.get_CodeBase() + 0x27 1.565 at Microsoft.Playwright.Helpers.Driver.GetExecutablePath() + 0xa8 1.565 at Microsoft.Playwright.Transport.StdIOTransport.GetProcess(String) + 0x36 1.565 at Microsoft.Playwright.Transport.StdIOTransport..ctor() + 0x62 1.565 at Microsoft.Playwright.Playwright.<CreateAsync>d__0.MoveNext() + 0x4f 1.565 --- End of stack trace from previous location --- 1.565 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c 1.565 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe 1.565 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4e 1.565 at Program.<<Main>$>d__0.MoveNext() + 0xe5 1.565 --- End of stack trace from previous location --- 1.565 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c 1.565 at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe 1.565 at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4e 1.565 at Program.<Main>(String[] args) + 0x24 1.565 at PayPowerBill!<BaseAddress>+0x4f86bc

Dockerfile:

FROM mcr.microsoft.com/dotnet/runtime:8.0-jammy AS base
USER app
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:8.0 as build
# Install NativeAOT build prerequisites
USER root
RUN apt-get update && apt-get install -y --no-install-recommends clang zlib1g-dev

WORKDIR /src
COPY ["PayPowerBill.csproj", "."]
RUN dotnet restore "./PayPowerBill.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "PayPowerBill.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "PayPowerBill.csproj" -c Release -o /app/publish -r linux-x64 

FROM base AS final
USER root
WORKDIR /app
COPY --from=publish /app/publish .
##Need to trigger Playwright install thru code to avoid dependency on sdk in final image - see www.meziantou.net/distributing-applications-that-depend-on-microsoft-playwright.htm
RUN /app/PayPowerBill install
RUN <<-DEPS
	apt-get update &&
	apt-get -y install libasound2 libx11-xcb1 libxcursor1 libgtk-3-0 libpangocairo-1.0-0 libcairo-gobject2 libgdk-pixbuf-2.0-0 libdbus-glib-1-2 &&
	rm -rf /var/lib/apt/lists/* &&
	echo "workaround"
DEPS
ENTRYPOINT ["PayPowerBill"]

How the program calls install at startup:

using var playwright = await Playwright.CreateAsync();
var b = playwright.Firefox;
if (args.Any() && args?[0] == "install")
{
    Environment.Exit(Microsoft.Playwright.Program.Main(new[] { "install" }));//, b.Name }));
}
...

matthewteeter avatar May 20 '24 04:05 matthewteeter

@matthewteeter we need a full repro, ideally via a small git repository.

mxschmitt avatar May 20 '24 07:05 mxschmitt

Here is a full repro: https://github.com/matthewteeter/Playwright1.44AotRepro Running the program as normal throws an exception on the first line in Program.cs. Trying to install the browsers by invoking it with "install" (as shown by the Dockerfile) fails with:

0.816 Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
0.816    at Microsoft.Playwright.Transport.Connection.<WrapApiCallAsync>d__52`1.MoveNext() + 0x223
0.816 --- End of stack trace from previous location ---
0.816    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
0.816    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe
0.816    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4e
0.816    at Microsoft.Playwright.Transport.Connection.<InitializePlaywrightAsync>d__43.MoveNext() + 0x11b
0.816 --- End of stack trace from previous location ---
0.816    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
0.816    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe
0.816    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4e
0.816    at Microsoft.Playwright.Playwright.<CreateAsync>d__0.MoveNext() + 0x21d
0.816 --- End of stack trace from previous location ---
0.816    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
0.816    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe
0.816    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4e
0.816    at Program.<<Main>$>d__0.MoveNext() + 0xdf
0.816 --- End of stack trace from previous location ---
0.816    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c
0.816    at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe
0.816    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4e
0.816    at Program.<Main>(String[] args) + 0x24
0.816    at PayPowerBill!<BaseAddress>+0x2c672c```

matthewteeter avatar May 27 '24 02:05 matthewteeter

@matthewteeter AOT is not supported yet see here: https://github.com/microsoft/playwright-dotnet/issues/2714

mxschmitt avatar May 27 '24 09:05 mxschmitt

I'll close this issue, since this issue (sigle-file published program) has been resolved. See https://github.com/microsoft/playwright-dotnet/issues/2255#issuecomment-2056544386 for notes.

mxschmitt avatar May 27 '24 09:05 mxschmitt