Skip to content

Commit 828755c

Browse files
authored
Check if file exists before trying to open it (#16315)
* Check if file exists before trying to open it * Fantomas refactoring white-spaces
1 parent e531233 commit 828755c

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/Compiler/Utilities/range.fs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -548,19 +548,22 @@ module Range =
548548

549549
let mkFirstLineOfFile (file: string) =
550550
try
551-
let lines = FileSystem.OpenFileForReadShim(file).ReadLines() |> Seq.indexed
551+
if not (FileSystem.FileExistsShim file) then
552+
mkRange file (mkPos 1 0) (mkPos 1 80)
553+
else
554+
let lines = FileSystem.OpenFileForReadShim(file).ReadLines() |> Seq.indexed
552555

553-
let nonWhiteLine =
554-
lines |> Seq.tryFind (fun (_, s) -> not (String.IsNullOrWhiteSpace s))
556+
let nonWhiteLine =
557+
lines |> Seq.tryFind (fun (_, s) -> not (String.IsNullOrWhiteSpace s))
555558

556-
match nonWhiteLine with
557-
| Some (i, s) -> mkRange file (mkPos (i + 1) 0) (mkPos (i + 1) s.Length)
558-
| None ->
559+
match nonWhiteLine with
560+
| Some (i, s) -> mkRange file (mkPos (i + 1) 0) (mkPos (i + 1) s.Length)
561+
| None ->
559562

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

562-
match nonEmptyLine with
563-
| Some (i, s) -> mkRange file (mkPos (i + 1) 0) (mkPos (i + 1) s.Length)
564-
| None -> mkRange file (mkPos 1 0) (mkPos 1 80)
565+
match nonEmptyLine with
566+
| Some (i, s) -> mkRange file (mkPos (i + 1) 0) (mkPos (i + 1) s.Length)
567+
| None -> mkRange file (mkPos 1 0) (mkPos 1 80)
565568
with _ ->
566569
mkRange file (mkPos 1 0) (mkPos 1 80)

0 commit comments

Comments
 (0)