dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.84k stars 663 forks source link

Error when creating new files #4181

Open raafamaia opened 3 years ago

raafamaia commented 3 years ago

Issue Description

Hello!

I'm having some trouble using VSCode and the C# extension. Everytime I create a new file, the Intellisense doens't work, not showing any errors (red underline) on the file. The OmniSharp Log output shows the following message:

    Could not find document for file /Users/rafaelmaia/source/personal/dotnet/Commander/Data/Mock.cs

I reinstalled VSCode and deleted all my extensions folder rm -rf ~/.vscode*, just installed the C# extension and the error persists.

I'm using VSCode version 1.50.1 on a macOS Cataline 10.15.7, dotnet sdk 3.1.403

Steps to Reproduce

Clean Install of VSCode Install C# extension Create a dotnet webapi project dotnet new webapi -n Test Create a new file Write something ex: aaaaaa

Expected Behavior

The expect behavior would be the line written with a red underscore, indicating an error.

Actual Behavior

Nothing happens, and you can check errors popping on the OmniSharp Log

[info]: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionService
        Could not find document for file /Users/rafaelmaia/source/personal/dotnet/Teste/Controllers/teste.cs

Logs

OmniSharp log

Here's the full OmniSharp Log, the error lines at the end start appearing as I create a new file and start to type on it. Starting OmniSharp server at 11/5/2020, 4:28:59 AM Target: /Users/rafaelmaia/source/personal/dotnet/Commander OmniSharp server started. Path: /Users/rafaelmaia/.vscode/extensions/ms-dotnettools.csharp-1.23.5/.omnisharp/1.37.3/run PID: 94751 [info]: OmniSharp.Stdio.Host Starting OmniSharp on MacOS 10.15.7 (x64) [info]: OmniSharp.Services.DotNetCliService DotNetPath set to dotnet [info]: OmniSharp.MSBuild.Discovery.MSBuildLocator Located 1 MSBuild instance(s) 1: StandAlone 16.8.0 - "/Users/rafaelmaia/.vscode/extensions/ms-dotnettools.csharp-1.23.5/.omnisharp/1.37.3/omnisharp/.msbuild/Current/Bin" [info]: OmniSharp.MSBuild.Discovery.MSBuildLocator MSBUILD_EXE_PATH environment variable set to '/Users/rafaelmaia/.vscode/extensions/ms-dotnettools.csharp-1.23.5/.omnisharp/1.37.3/omnisharp/.msbuild/Current/Bin/MSBuild.exe' [info]: OmniSharp.MSBuild.Discovery.MSBuildLocator Registered MSBuild instance: StandAlone 16.8.0 - "/Users/rafaelmaia/.vscode/extensions/ms-dotnettools.csharp-1.23.5/.omnisharp/1.37.3/omnisharp/.msbuild/Current/Bin" CscToolExe = csc.exe MSBuildToolsPath = /Users/rafaelmaia/.vscode/extensions/ms-dotnettools.csharp-1.23.5/.omnisharp/1.37.3/omnisharp/.msbuild/Current/Bin CscToolPath = /Users/rafaelmaia/.vscode/extensions/ms-dotnettools.csharp-1.23.5/.omnisharp/1.37.3/omnisharp/.msbuild/Current/Bin/Roslyn BypassFrameworkInstallChecks = true MSBuildExtensionsPath = /Users/rafaelmaia/.vscode/extensions/ms-dotnettools.csharp-1.23.5/.omnisharp/1.37.3/omnisharp/.msbuild [info]: OmniSharp.Cake.CakeProjectSystem Detecting Cake files in '/Users/rafaelmaia/source/personal/dotnet/Commander'. [info]: OmniSharp.Cake.CakeProjectSystem Could not find any Cake files [info]: OmniSharp.MSBuild.ProjectSystem No solution files found in '/Users/rafaelmaia/source/personal/dotnet/Commander' [info]: OmniSharp.MSBuild.ProjectManager Queue project update for '/Users/rafaelmaia/source/personal/dotnet/Commander/Commander.csproj' [info]: OmniSharp.Script.ScriptProjectSystem Detecting CSX files in '/Users/rafaelmaia/source/personal/dotnet/Commander'. [info]: OmniSharp.Script.ScriptProjectSystem Could not find any CSX files [info]: OmniSharp.WorkspaceInitializer Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpFormattingWorkspaceOptionsProvider, Order: 0 [info]: OmniSharp.MSBuild.ProjectManager Loading project: /Users/rafaelmaia/source/personal/dotnet/Commander/Commander.csproj [info]: OmniSharp.WorkspaceInitializer Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionOptionsProvider, Order: 0 [info]: OmniSharp.WorkspaceInitializer Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.RenameWorkspaceOptionsProvider, Order: 100 [info]: OmniSharp.WorkspaceInitializer Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.ImplementTypeWorkspaceOptionsProvider, Order: 110 [info]: OmniSharp.WorkspaceInitializer Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.BlockStructureWorkspaceOptionsProvider, Order: 140 [info]: OmniSharp.WorkspaceInitializer Configuration finished. [info]: OmniSharp.Stdio.Host Omnisharp server running using Stdio at location '/Users/rafaelmaia/source/personal/dotnet/Commander' on host 94627. [info]: OmniSharp.MSBuild.ProjectManager Successfully loaded project file '/Users/rafaelmaia/source/personal/dotnet/Commander/Commander.csproj'. [info]: OmniSharp.MSBuild.ProjectManager Adding project '/Users/rafaelmaia/source/personal/dotnet/Commander/Commander.csproj' [info]: OmniSharp.MSBuild.ProjectManager Update project: Commander [info]: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionService Could not find document for file /Users/rafaelmaia/source/personal/dotnet/Commander/Data/teste1.cs [info]: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionService Could not find document for file /Users/rafaelmaia/source/personal/dotnet/Commander/Data/teste1.cs [info]: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionService Could not find document for file /Users/rafaelmaia/source/personal/dotnet/Commander/Data/teste1.cs [info]: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionService Could not find document for file /Users/rafaelmaia/source/personal/dotnet/Commander/Data/teste1.cs [info]: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionService Could not find document for file /Users/rafaelmaia/source/personal/dotnet/Commander/Data/teste1.cs [info]: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionService Could not find document for file /Users/rafaelmaia/source/personal/dotnet/Commander/Data/teste1.cs [info]: OmniSharp.Roslyn.CSharp.Services.Completion.CompletionService Could not find document for file /Users/rafaelmaia/source/personal/dotnet/Commander/Data/teste1.cs

C# log

Nothing on C# Log.

Environment information

VSCode version: 1.50.1 C# Extension: 1.23.5

Mono Information OmniSharp using built-in mono
Dotnet Information .NET Core SDK (reflecting any global.json): Version: 3.1.403 Commit: 9e895200cd Runtime Environment: OS Name: Mac OS X OS Version: 10.15 OS Platform: Darwin RID: osx.10.15-x64 Base Path: /usr/local/share/dotnet/sdk/3.1.403/ Host (useful for support): Version: 3.1.9 Commit: 774fc3d6a9 .NET Core SDKs installed: 2.0.3 [/usr/local/share/dotnet/sdk] 2.1.3 [/usr/local/share/dotnet/sdk] 3.1.403 [/usr/local/share/dotnet/sdk] .NET Core runtimes installed: Microsoft.AspNetCore.App 3.1.9 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.0.3 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 2.0.4 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 2.1.23 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.9 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App] To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download
Visual Studio Code Extensions |Extension|Author|Version| |---|---|---| |csharp|ms-dotnettools|1.23.5|;
filipw commented 3 years ago

this is fixed in https://github.com/OmniSharp/omnisharp-vscode/pull/4178

duartegjmiguel commented 3 years ago

I am having this issue as well.

RomainHautefeuille commented 3 years ago

Are you sure @filipw ? I am using 1.23.6 and I still need to manually restart Omnisharp after creating a file. No completion in the file, nor can I use the new class elsewhere otherwise.

filipw commented 3 years ago

I think you are right, this still has a problem.

@JoeRobich should this be fixed now? I can see that after a new file is created, and after the save it is reported to the server but then the completion request throws. I haven't debugged further but looks like the server sees the file as empty (?).

[dbug]: OmniSharp.Stdio.Host
        ************ Request ************
{
  "Type": "request",
  "Seq": 663,
  "Command": "/completion",
  "Arguments": {
    "FileName": "/Users/filipw/Documents/dev/personal/repro/net50withnet48/app/Foo.cs",
    "Line": 8,
    "Column": 15,
    "CompletionTrigger": 2,
    "TriggerCharacter": "."
  }
}
[fail]: OmniSharp.Stdio.Host
        ************  Response ************ 
{
  "Request_seq": 663,
  "Command": "/completion",
  "Running": true,
  "Success": false,
  "Message": "\"System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.\\nParameter name: index\\n  at Microsoft.CodeAnalysis.Text.SourceText+LineInfo.get_Item (System.Int32 index) [0x0000f] in <74e762f007514b0991a005183ec8dcb2>:0 \\n  at Microsoft.CodeAnalysis.Text.TextLineCollection.GetPosition (Microsoft.CodeAnalysis.Text.LinePosition position) [0x00008] in <74e762f007514b0991a005183ec8dcb2>:0 \\n  at OmniSharp.Extensions.SourceTextExtensions.GetTextPosition (Microsoft.CodeAnalysis.Text.SourceText sourceText, OmniSharp.Models.Request request) [0x00017] in <be259f9fdaf7461fb1b75b43208e4134>:0 \\n  at OmniSharp.Roslyn.CSharp.Services.Completion.CompletionService.Handle (OmniSharp.Models.v1.Completion.CompletionRequest request) [0x0016c] in <845c3087cc7d4530b36a8584d73f1c1d>:0 \\n  at OmniSharp.Endpoint.EndpointHandler`2[TRequest,TResponse].GetFirstNotEmptyResponseFromHandlers (OmniSharp.Endpoint.Exports.ExportHandler`2[TRequest,TResponse][] handlers, TRequest request) [0x00099] in <0b3dc7d6f0c8428682a86ac3f9663775>:0 \\n  at OmniSharp.Endpoint.EndpointHandler`2[TRequest,TResponse].HandleRequestForLanguage (System.String language, TRequest request, OmniSharp.Protocol.RequestPacket packet) [0x00163] in <0b3dc7d6f0c8428682a86ac3f9663775>:0 \\n  at OmniSharp.Endpoint.EndpointHandler`2[TRequest,TResponse].Process (OmniSharp.Protocol.RequestPacket packet, OmniSharp.Endpoint.LanguageModel model, Newtonsoft.Json.Linq.JToken requestObject) [0x0024b] in <0b3dc7d6f0c8428682a86ac3f9663775>:0 \\n  at OmniSharp.Stdio.Host.HandleRequest (System.String json, Microsoft.Extensions.Logging.ILogger logger) [0x000e7] in <0ff274924a1842faabf855aebc686508>:0 \"",
  "Body": null,
  "Seq": 880,
  "Type": "response"
}
filipw commented 3 years ago

interesting is that I was partly correct 🙂 looks like it works on Windows, but not on MacOS

RomainHautefeuille commented 3 years ago

hum, it might be a distinct issue but here is an easy reproduction path on Windows:

  1. make a new folder, cd into it
  2. dotnet new console
  3. go edit Program class, add property : public Model MyProperty { get; set; }
  4. use Show Fixes (ctrl+;) to create Model Generate type 'Model' -> Generate class 'Model' in a new file

now Model.cs is created but Program doesn't see the class, and you can't see the 'x references' hints in the new file.

filipw commented 3 years ago

Thanks, we had a look at that yesterday with @333fred and indeed it is a bug that crept in due to improvements in handling file changes and due to certain indeterminism in how VS Code sends file events to the server.

It is addressed in https://github.com/OmniSharp/omnisharp-roslyn/pull/2019

RomainHautefeuille commented 3 years ago

Nice, thanks!

TCROC commented 3 years ago

When is this going to get released the the VS Code Marketplace?

333fred commented 3 years ago

@TCROC if you set omnisharp.path to latest in your settings, you should see it now.

TCROC commented 3 years ago

@333fred Awesome thanks! I just created a new test script from Unity and it appears to be working :)

alpha-tango-kilo commented 3 years ago
1. make a new folder, cd into it

2. `dotnet new console`

3. go edit Program class, add property : `public Model MyProperty { get; set; }`

4. use `Show Fixes (ctrl+;)` to create Model `Generate type 'Model' -> Generate class 'Model' in a new file`

now Model.cs is created but Program doesn't see the class, and you can't see the 'x references' hints in the new file.

This minimal example works for me having set omnisharp.path to latest, however within a Unity project adding a new C# script isn't recognised and I get the "Could not find document for file" warning, no autocompletions or reference counter

I'm using VSCodium 1.52.0, C# extension 1.23.7. I'm not massively familiar with C# development so please let me know what other debug info you need if you do

TCROC commented 3 years ago
1. make a new folder, cd into it

2. `dotnet new console`

3. go edit Program class, add property : `public Model MyProperty { get; set; }`

4. use `Show Fixes (ctrl+;)` to create Model `Generate type 'Model' -> Generate class 'Model' in a new file`

now Model.cs is created but Program doesn't see the class, and you can't see the 'x references' hints in the new file.

This minimal example works for me having set omnisharp.path to latest, however within a Unity project adding a new C# script isn't recognised and I get the "Could not find document for file" warning, no autocompletions or reference counter

I'm using VSCodium 1.52.0, C# extension 1.23.7. I'm not massively familiar with C# development so please let me know what other debug info you need if you do

I had the same problem. It is broken in 1.23.7. Do this to use the beta where it is fixed: https://github.com/OmniSharp/omnisharp-vscode/issues/4181#issuecomment-742668376

alpha-tango-kilo commented 3 years ago

That's what I am doing - sorry if I wasn't clear. I set omnisharp.path to latest and the extension still isn't picking up new scripts in a Unity project I'm working on

The minimal example works though

JoeRobich commented 3 years ago

@alpha-tango-kilo Sorry you are running into issues. Are you adding the script file outside of Unity?

I ask because Unity generates old style project files where each source file to be included is individually listed. This is different from SDK style projects created with dotnet new which automatically include source files without needing them listed.

I am not sure that OmniSharp would add new files to the workspace without Unity rebuilding the project file to specifically include the source file.

alpha-tango-kilo commented 3 years ago

I ask because Unity generates old style project files where each source file to be included is individually listed

I'm not surprised somehow

So I tested this and now I can't even replicate my own issue now 3 days later...

I tried (with Unity open in all cases):

  1. Creating a new script in Unity, then opening VSC
  2. Creating a new script in Unity with VSC already open
  3. Creating a new script in VSC

Everything worked!🤯 Shall report back if I have any further issues down the line.

Also given Unity itself seems also to be a factor here, I'll note that the version I'm using is 2019.4.16f1

TCROC commented 3 years ago

I did notice having this issue once when using the "latest" release. It was right after I created a new .asmdef file. I will see if I can recreate it later today.

TCROC commented 3 years ago

Sorry it took me a minute to get around to testing this. I got caught up in some other projects. Anyway, I can confirm that adding an asmdef file breaks things. Steps to reproduce:

  1. Create new Unity Project
  2. Create asmdef file
  3. Add script to folder of asmdef file
  4. Open Script in VS Code.
  5. See that everything loads as expected
  6. Create new folder and add asmef file inside of it
  7. Create new script scoped to asmdef file
  8. Open that script in VS Code and see that Omnisharp is broken for that script
JoeRobich commented 3 years ago

@TCROC I would check that Unity is updating the project file after adding the second asmdef and script. You should see a new <Compile Include="..Path to new script.." /> in the csproj.

TCROC commented 3 years ago

@JoeRobich I can confirm that behavior is working as expected. When a new asmdef file is created, a new csproj is correctly created and that <Compile Include="..Path to new script.." /> is there. Omnisharp just isn't picking up on it and I have to restart Omnisharp.

JoeRobich commented 3 years ago

@TCROC Does VS Code stay open with your unity project folder from step 4 thru step 8? or is it closed while adding the new asmdef and script?

TCROC commented 3 years ago

@JoeRobich Unity and VS Code are both open the entire time

I misspoke, Unity is open the entire time and VS Code is open in steps 4 - 8

TCROC commented 3 years ago

@TCROC Does VS Code stay open with your unity project folder from step 4 thru step 8? or is it closed while adding the new asmdef and script?

To further clarify, VS Code is open in steps 4 - 8.

TCROC commented 3 years ago

Just checking in to see what the status is on this bug

FiniteReality commented 3 years ago

I'm also seeing this issue in VSCode using extension version 1.23.8 and OmniSharp 1.37.6-beta.16. I'm not seeing any exceptions, just the Could not find document for file message. I'm using a normal SDK-style csproj targeting net5.0.

AwSkies commented 3 years ago

I'm also seeing this issue in VSCode using extension version 1.23.8 and OmniSharp 1.37.6-beta.16. I'm not seeing any exceptions, just the Could not find document for file message. I'm using a normal SDK-style csproj targeting net5.0.

Me too, it's not working for me either and just giving that message.

AwSkies commented 3 years ago

I noticed that in the changelog of the C# extension it said:

For Mono-based development (e.g. Unity) that requires full .NET framework, you need to set "omnisharp.useGlobalMono": "always". The current value of "auto" will remain "never" until Mono upgrades their bundled MSBuild version.

I tried setting it to always, but it still won't work

333fred commented 3 years ago

For those of you who are still seeing this and aren't adding non csharp files (like tcroc is) please file a new issue with specific repro steps. I use the extension every day and have yet to reproduce the issue with csharp files, so more info is needed.

TCROC commented 3 years ago

https://github.com/OmniSharp/omnisharp-vscode/issues/4181#issuecomment-742795336

This issue is occurring again for me. I've tested it in both Unity projects and Mono projects. Running on Ubuntu 20.04.

Edit: False alarm. See below comment.

TCROC commented 3 years ago

#4181 (comment)

This issue is occurring again for me. I've tested it in both Unity projects and Mono projects. Running on Ubuntu 20.04.

Never mind. False alarm. I just had to increase the file watcher limit on my system.

TCROC commented 3 years ago

However, I would like to confirm that even with the file watchers limit increased, it still doesn't work when creating a new asmdef file in Unity (which is the equivalent of adding a new .csproj).