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

fix: support self-contained executables #2909

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions src/Playwright/Helpers/Driver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;

namespace Microsoft.Playwright.Helpers;
Expand All @@ -48,19 +49,19 @@ internal static (string ExecutablePath, Func<string, string> GetArgs) GetExecuta
}
if (assemblyDirectory?.Exists != true || !File.Exists(Path.Combine(assemblyDirectory.FullName, "Microsoft.Playwright.dll")))
{
string assemblyLocation;
var assembly = typeof(Playwright).Assembly;
#pragma warning disable SYSLIB0012 // 'Assembly.CodeBase' is obsolete: 'Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility.
if (Uri.TryCreate(assembly.CodeBase, UriKind.Absolute, out var codeBase) && codeBase.IsFile)
#pragma warning restore SYSLIB0012 // 'Assembly.CodeBase' is obsolete: 'Assembly.CodeBase and Assembly.EscapedCodeBase are only included for .NET Framework compatibility.
if (TryGetCodeBase(assembly, out var codeBase) && codeBase.IsFile)
{
assemblyLocation = codeBase.LocalPath;
assemblyDirectory = new FileInfo(codeBase.LocalPath).Directory;
}
else if (!string.IsNullOrEmpty(assembly.Location))
{
assemblyDirectory = new FileInfo(assembly.Location).Directory;
}
else
{
assemblyLocation = assembly.Location;
assemblyDirectory = new FileInfo(AppContext.BaseDirectory).Directory;
}
assemblyDirectory = new FileInfo(assemblyLocation).Directory;
}

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

private static bool TryGetCodeBase(Assembly assembly, out Uri codeBase)
{
try
{
// assembly.CodeBase might throw with:
// System.NotSupportedException: CodeBase is not supported on assemblies loaded from a single-file bundle.
Uri.TryCreate(assembly.CodeBase, UriKind.Absolute, out codeBase);
return true;
}
catch (NotSupportedException)
{
codeBase = null;
return false;
}
}

private static (string ExecutablePath, Func<string, string> GetArgs) GetPath(string driversPath)
{
string platformId;
Expand Down
Loading