Skip to content

Commit 1561839

Browse files
authored
fix: support self-contained executables (#2909)
1 parent 98e3755 commit 1561839

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

src/Playwright/Helpers/Driver.cs

+24-7
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using System;
2727
using System.Collections.Generic;
2828
using System.IO;
29+
using System.Reflection;
2930
using System.Runtime.InteropServices;
3031

3132
namespace Microsoft.Playwright.Helpers;
@@ -48,19 +49,19 @@ internal static (string ExecutablePath, Func<string, string> GetArgs) GetExecuta
4849
}
4950
if (assemblyDirectory?.Exists != true || !File.Exists(Path.Combine(assemblyDirectory.FullName, "Microsoft.Playwright.dll")))
5051
{
51-
string assemblyLocation;
5252
var assembly = typeof(Playwright).Assembly;
53-
#pragma warning disable SYSLIB0012 // 'Assembly.CodeBase' is obsolete: 'Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility.
54-
if (Uri.TryCreate(assembly.CodeBase, UriKind.Absolute, out var codeBase) && codeBase.IsFile)
55-
#pragma warning restore SYSLIB0012 // 'Assembly.CodeBase' is obsolete: 'Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility.
53+
if (TryGetCodeBase(assembly, out var codeBase) && codeBase.IsFile)
5654
{
57-
assemblyLocation = codeBase.LocalPath;
55+
assemblyDirectory = new FileInfo(codeBase.LocalPath).Directory;
56+
}
57+
else if (!string.IsNullOrEmpty(assembly.Location))
58+
{
59+
assemblyDirectory = new FileInfo(assembly.Location).Directory;
5860
}
5961
else
6062
{
61-
assemblyLocation = assembly.Location;
63+
assemblyDirectory = new FileInfo(AppContext.BaseDirectory).Directory;
6264
}
63-
assemblyDirectory = new FileInfo(assemblyLocation).Directory;
6465
}
6566

6667
string executableFile;
@@ -95,6 +96,22 @@ internal static (string ExecutablePath, Func<string, string> GetArgs) GetExecuta
9596
throw new PlaywrightException($"Driver not found: {executableFile}");
9697
}
9798

99+
private static bool TryGetCodeBase(Assembly assembly, out Uri codeBase)
100+
{
101+
try
102+
{
103+
// assembly.CodeBase might throw with:
104+
// System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.
105+
Uri.TryCreate(assembly.CodeBase, UriKind.Absolute, out codeBase);
106+
return true;
107+
}
108+
catch (NotSupportedException)
109+
{
110+
codeBase = null;
111+
return false;
112+
}
113+
}
114+
98115
private static (string ExecutablePath, Func<string, string> GetArgs) GetPath(string driversPath)
99116
{
100117
string platformId;

0 commit comments

Comments
 (0)