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

Do not return nulls when getting documents by path #2233

Merged
merged 4 commits into from
Sep 11, 2021

Conversation

JoeRobich
Copy link
Member

@JoeRobich JoeRobich commented Sep 8, 2021

Resolves #2125

The IEnumerable returned by workspace.GetDocuments(string filePath) can contain nulls if the filepath isn't present in any Project.Documents. In this case .cshtml files are not part of the compilation and are AdditionalDocuments in the workspace. The added OfType<>() will filter out the null values and allow an empty enumerable to be returned.

Using the repro provided at https://github.com/jsheely/dotnet6-omni-bug1, we get results instead of an NRE.

image

#line default
#line hidden")
#line hidden"),
new TestFile(mappedFilePath,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding the mapped file as an additional document will create a valid DocumentId that an be found by file path but does not exist in the Project.Documents.

@@ -144,7 +144,8 @@ public IEnumerable<ProjectId> AddFilesToWorkspace(string folderPath, params Test
Workspace,
Path.Combine(folderPath, "project.csproj"),
new[] { "net472" },
testFiles.Where(f => f.FileName.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)).ToArray());
testFiles.Where(f => f.FileName.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)).ToArray(),
testFiles.Where(f => !f.FileName.EndsWith(".cs", StringComparison.OrdinalIgnoreCase) && !f.FileName.EndsWith(".csx", StringComparison.OrdinalIgnoreCase)).ToArray());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All non-C# files will be added as additional documents to the project.

@@ -340,7 +340,8 @@ public IEnumerable<Document> GetDocuments(string filePath)
{
return CurrentSolution
.GetDocumentIdsWithFilePath(filePath)
.Select(id => CurrentSolution.GetDocument(id));
.Select(id => CurrentSolution.GetDocument(id))
.OfType<Document>();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the .cshtml files will have valid DocumentIds but not be part of any Project's Documents null can be returned, the OfType will filter out the null values.

@JoeRobich
Copy link
Member Author

@nohwnd @filipw PTAL

Copy link
Member

@filipw filipw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good, thanks.
do we need to prevent them from being discovered as misc files?
according to the logs here it was additionally discovered as misc file dotnet/vscode-csharp#4746 (comment)

@JoeRobich
Copy link
Member Author

according to the logs here it was additionally discovered as misc file

Good point. I had made an assumption they were added as additional documents. I'll add a test which validates the document in misc workspace.

@JoeRobich JoeRobich merged commit 5cd3e6d into OmniSharp:master Sep 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Omnisharp Server crashing with a NullReferenceException
2 participants