Skip to content

Commit 40f7179

Browse files
committed
prevent a crash
1 parent c9d5556 commit 40f7179

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

src/CSharpLanguageServer/Roslyn/Solution.fs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,7 @@ let solutionGetRazorDocumentForUri
403403
: Async<(Project * Compilation * SyntaxTree) option> =
404404
async {
405405
let cshtmlPath = uri |> Uri.toPath
406-
let cshtmlDirectory = Path.GetDirectoryName(cshtmlPath)
407-
let normalizedTargetDir = Path.GetFullPath(cshtmlDirectory)
406+
let normalizedTargetDir = cshtmlPath |> Path.GetDirectoryName |> Path.GetFullPath
408407

409408
let projectForPath =
410409
solution.Projects
@@ -417,28 +416,31 @@ let solutionGetRazorDocumentForUri
417416
StringComparison.OrdinalIgnoreCase
418417
))
419418

420-
let projectBaseDir = Path.GetDirectoryName(projectForPath.Value.FilePath)
419+
match projectForPath with
420+
| None -> return None
421+
| Some project ->
422+
let projectBaseDir = Path.GetDirectoryName(project.FilePath)
421423

422-
let! compilation = projectForPath.Value.GetCompilationAsync() |> Async.AwaitTask
424+
let! compilation = project.GetCompilationAsync() |> Async.AwaitTask
423425

424-
let mutable cshtmlTree: SyntaxTree option = None
426+
let mutable cshtmlTree: SyntaxTree option = None
425427

426-
let cshtmlPathTranslated =
427-
Path.GetRelativePath(projectBaseDir, cshtmlPath)
428-
|> _.Replace(".", "_")
429-
|> _.Replace(Path.DirectorySeparatorChar, '_')
430-
|> (fun s -> s + ".g.cs")
428+
let cshtmlPathTranslated =
429+
Path.GetRelativePath(projectBaseDir, cshtmlPath)
430+
|> _.Replace(".", "_")
431+
|> _.Replace(Path.DirectorySeparatorChar, '_')
432+
|> (fun s -> s + ".g.cs")
431433

432-
for tree in compilation.SyntaxTrees do
433-
let path = tree.FilePath
434+
for tree in compilation.SyntaxTrees do
435+
let path = tree.FilePath
434436

435-
if path.StartsWith(projectBaseDir) then
436-
let relativePath = Path.GetRelativePath(projectBaseDir, path)
437+
if path.StartsWith(projectBaseDir) then
438+
let relativePath = Path.GetRelativePath(projectBaseDir, path)
437439

438-
if relativePath.EndsWith(cshtmlPathTranslated) then
439-
cshtmlTree <- Some tree
440+
if relativePath.EndsWith(cshtmlPathTranslated) then
441+
cshtmlTree <- Some tree
440442

441-
return cshtmlTree |> Option.map (fun cst -> (projectForPath.Value, compilation, cst))
443+
return cshtmlTree |> Option.map (fun cst -> (project, compilation, cst))
442444
}
443445

444446

0 commit comments

Comments
 (0)