Skip to content

Commit

Permalink
Fsdk/Misc: fix NRE if legacy
Browse files Browse the repository at this point in the history
When running TSV unit tests in legacy framework (e.g. Mono),
a NRE was being thrown; I guess because in that case the
function GetEntryAssembly() was returning null.

This fix is kinda ugly but is anyway affecting a part of the
code that I plan to retire soon (cause it's API wrapped under
LEGACY_FRAMEWORK ifdef).
  • Loading branch information
knocte committed Aug 23, 2023
1 parent f6287f7 commit 196e1ef
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions Fsdk/Misc.fs
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,29 @@ module Misc =
#if LEGACY_FRAMEWORK
// this below is crazy but is to avoid # char being ignored in Uri.LocalPath property, see https://stackoverflow.com/a/41203269
let private currentExeUri =
Uri(Uri.EscapeUriString(Assembly.GetEntryAssembly().CodeBase))
let entryAssembly = Assembly.GetEntryAssembly()
if isNull entryAssembly then
None
else
Uri(Uri.EscapeUriString(entryAssembly.CodeBase)) |> Some

let private currentExe =
FileInfo(
sprintf
"%s%s"
(Uri.UnescapeDataString(currentExeUri.PathAndQuery))
(Uri.UnescapeDataString(currentExeUri.Fragment))
)
match currentExeUri with
| None -> None
| Some currentExeUri ->
FileInfo(
sprintf
"%s%s"
(Uri.UnescapeDataString(currentExeUri.PathAndQuery))
(Uri.UnescapeDataString(currentExeUri.Fragment))
) |> Some

let rec private FsxOnlyArgumentsInternalFsx(args: list<string>) =
let currentExe =
if currentExe.IsNone then
failwith "Could not get EntryAssembly"
else
currentExe.Value
match args with
| [] -> []
| head :: tail ->
Expand Down Expand Up @@ -96,6 +108,11 @@ module Misc =
// likely running a fsxc-ed assembly from an .fsx script (so 'dotnet someFile.fsx.dll someArg1' instead of 'dotnet fsi someFile.fsx someArg1')
List.skip 1 cmdLineArgs
#else
let currentExe =
if currentExe.IsNone then
failwith "Could not get EntryAssembly"
else
currentExe.Value
let isFsi =
String.Equals(
currentExe.Name,
Expand Down

0 comments on commit 196e1ef

Please sign in to comment.