Skip to content

Commit

Permalink
Check if file exists before trying to open it (#16315)
Browse files Browse the repository at this point in the history
* Check if file exists before trying to open it

* Fantomas refactoring white-spaces
  • Loading branch information
Thorium authored Nov 21, 2023
1 parent e531233 commit 828755c
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/Compiler/Utilities/range.fs
Original file line number Diff line number Diff line change
Expand Up @@ -548,19 +548,22 @@ module Range =

let mkFirstLineOfFile (file: string) =
try
let lines = FileSystem.OpenFileForReadShim(file).ReadLines() |> Seq.indexed
if not (FileSystem.FileExistsShim file) then
mkRange file (mkPos 1 0) (mkPos 1 80)
else
let lines = FileSystem.OpenFileForReadShim(file).ReadLines() |> Seq.indexed

let nonWhiteLine =
lines |> Seq.tryFind (fun (_, s) -> not (String.IsNullOrWhiteSpace s))
let nonWhiteLine =
lines |> Seq.tryFind (fun (_, s) -> not (String.IsNullOrWhiteSpace s))

match nonWhiteLine with
| Some (i, s) -> mkRange file (mkPos (i + 1) 0) (mkPos (i + 1) s.Length)
| None ->
match nonWhiteLine with
| Some (i, s) -> mkRange file (mkPos (i + 1) 0) (mkPos (i + 1) s.Length)
| None ->

let nonEmptyLine = lines |> Seq.tryFind (fun (_, s) -> not (String.IsNullOrEmpty s))
let nonEmptyLine = lines |> Seq.tryFind (fun (_, s) -> not (String.IsNullOrEmpty s))

match nonEmptyLine with
| Some (i, s) -> mkRange file (mkPos (i + 1) 0) (mkPos (i + 1) s.Length)
| None -> mkRange file (mkPos 1 0) (mkPos 1 80)
match nonEmptyLine with
| Some (i, s) -> mkRange file (mkPos (i + 1) 0) (mkPos (i + 1) s.Length)
| None -> mkRange file (mkPos 1 0) (mkPos 1 80)
with _ ->
mkRange file (mkPos 1 0) (mkPos 1 80)

0 comments on commit 828755c

Please sign in to comment.