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

Generated files in .csproj projects are not processed #1531

Closed
tsomogyi opened this issue May 31, 2017 · 24 comments · Fixed by OmniSharp/omnisharp-roslyn#907
Closed

Generated files in .csproj projects are not processed #1531

tsomogyi opened this issue May 31, 2017 · 24 comments · Fixed by OmniSharp/omnisharp-roslyn#907

Comments

@tsomogyi
Copy link

tsomogyi commented May 31, 2017

Environment data

dotnet --info output:

.NET Command Line Tools (1.0.4)

Product Information:
 Version:            1.0.4
 Commit SHA-1 hash:  af1e6684fd

Runtime Environment:
 OS Name:     Windows
 OS Version:  6.3.9600
 OS Platform: Windows
 RID:         win81-x64
 Base Path:   C:\Program Files\dotnet\sdk\1.0.4

VS Code version: 1.12.2
C# Extension version: 1.10.0

Steps to reproduce

  1. Create a new project
  2. Create a new source file 'test.cs' in /obj/Debug/netcoreapp1.1, content: namespace TestNs { public class TestClass { } }
  3. In Program.cs, Main function add: var t = new TestNs.TestClass();
  4. In .csproj add <Compile Include="$(IntermediateOutputPath)test.cs" />

Expected behavior

C# IntelliSense should recognize the namespace TestNs.

Actual behavior

In the editor I can see TestNs is underlined with red saying "The type or namespace name 'TestNs' could not be found...".

Remarks

The project builds fine using 'dotnet build'.
The significance of this scenario is that I generate source files using xsd tool, and the generated files go to the intermediate folder.
So the C# tool should either parse the intermediate folder or the extra files added for compilation in the project file - whichever is easier to implement.

@DustinCampbell
Copy link
Member

Does the error go away if you restart VS Code? We simply call MSBuild to return files from the .csproj file, so I would expect the file to be there.

@DustinCampbell
Copy link
Member

FWIW, this is working for me. I had to make some tweaks to your repro steps (such as adding the <Compile /> tag to an <ItemGroup> and ensuring that dotnet restore executed), but other than that, it worked fine.

@DustinCampbell
Copy link
Member

DustinCampbell commented May 31, 2017

In fact, I didn't have to add the <Compile /> element at all. The file-globbing support for .csproj in MSBuild found the file under obj\Debug\netcoreapp1.1 just fine without it.

Actually, scratch that. That was just OmniSharp lighting the file up because I was editing it. The <Compile/> element is needed to make dotnet build happy.

@DustinCampbell
Copy link
Member

OK. I'm definitely seeing this working just fine. Is there anything missing from your repro steps?

@tsomogyi
Copy link
Author

tsomogyi commented Jun 1, 2017

test
I restarted VS Code and the problem is still there.
And if I move the file test.cs to the root folder beside Program.cs then it works fine.

@DustinCampbell
Copy link
Member

I'll try again today. I had tried using dotnet new console and it worked fine on Windows. I'll try again with dotnet new webapi, which is what it looks like you are doing. Are there any other specifics to your repro steps that might make a difference? Also, it might be useful to zip up and share the project. Perhaps there's something special about it that I'm missing.

@tsomogyi
Copy link
Author

tsomogyi commented Jun 1, 2017

TestConsole.zip
testconsole
I can also reproduce with console type. PFA the complete project. However the extra bits might be needed in csproj for "generating" the file (just a copy in this example for the sake of simplicity), as if I directly refer test.cs, it works indeed. But this way I'm still getting the red underline.

@DustinCampbell
Copy link
Member

Would you mind providing your OmniSharp Log after opening the project? To retrieve it, select View->Output from the menu and then choose "OmniSharp Log" from the combo box at the top-right of the Output pane.

@tsomogyi
Copy link
Author

tsomogyi commented Jun 2, 2017

PFA
OmniSharp.zip

@DustinCampbell
Copy link
Member

Thanks for your prompt response. I think I'm going to need a bit more detail. Could you try setting the "omnisharp.loggingLevel": "debug" setting in VS Code? Then, open your project and send me the log again. Thanks for your patience!

@tsomogyi
Copy link
Author

tsomogyi commented Jun 3, 2017

OmniSharp_debug.zip
PFA the debug level log. Thanks for looking at it.

@DustinCampbell
Copy link
Member

OK. I've been unsuccessful in my attempts to reproduce this problem so far. Here are the steps I went through to try and reproduce the problem on something similar to your set up (which I gleaned from the Logs you provided -- thanks!):

  1. Create a Windows 8.1 VM
  2. Install VS Code 1.12.2
  3. Install .NET Core SDK 1.0.4
  4. Install C# for VS Code 1.10.0 from the Extensions pane in VS Code.
  5. Install .NET Framework 4.6.2 (OmniSharp won't launch if this isn't installed).
  6. Typed the following at a command prompt to create a project:
    > mkdir TestConsole
    > cd TestConsole
    > dotnet new console
    
  7. Open Notepad and create a file called 'test.cs' in obj\Debug\netcoreapp1.1 with the following content:
    namespace TestNs { public class TestClass { } }
  8. At the console, type code . to open the project in VS Code.
  9. In VS Code, open 'TestConsole.csproj' and add the following:
    <ItemGroup>
      <Compile Include="$(IntermediateOutputPath)test.cs" />
    </ItemGroup>
  10. In VS Code, open 'Program.cs' and wait for the prompts and squiggles to appear:
    • There are unresolved depedencies from 'TestConsole.csproj'. Please execute the restore command to continue. (I clicked 'Restore')
    • Required assets to build and debug are missing from 'TestConsole.csproj'. Add them? (I clicked 'Yes')
  11. Add the following code inside Main(...):
    var t = new TestNs.TestClass();

Unfortunately, with these steps, I don't get a squiggle on the code above. In fact, I even tried deleting the <Compile/> item from my project to verify that I do get the squiggle if that's removed, and then pasting it back to see the squiggle go away. Note that I did have to save the project file to see the change propagate, which is by design.

With <Compile/> item:
image

With <Compile/> item removed:
image

With item added back:
image

Here's the project I used: TestConsole.zip

Do you see anything that I might have missed in my repro steps?

@tsomogyi
Copy link
Author

tsomogyi commented Jun 6, 2017

The test.cs file should not directly reside in obj\Debug\netcoreapp1.1, but it should be generated/copied there during the build process. Did you have a look in my test project 'TestConsole.zip' I posted earlier? There I have a file called test.xsd which is transformed to a .cs file in the .csproj. Following your repro steps above I don't get the squiggle either, but using my project I can always see it no matter if I restart VS Code.

@DustinCampbell
Copy link
Member

Sorry @tsomogyi. I was following the repro steps that you filed with this issue. I'll take a closer look at the project.

@DustinCampbell
Copy link
Member

OK. Sorry that I didn't look more closely at your project earlier. I expected that it would be similar to your repro steps, but it's clearly very different. (In the future, it'd be helpful if the repro steps you provide are a bit closer to what you're actually doing. 😄)

The issue here is that OmniSharp uses MSBuild in-proc to build the ResolveAssemblyReferences target (code) with DesignTimeBuild set to true, which allows it to retrieve all of the references within a project. However, this will not run the CompileGeneratedFiles target. To do that, we'll need to update OmniSharp to use run the Compile target (again, with DesignTimeBuild set to true).

I quickly verified locally that this approach will work, but there are some details that will need to be worked out.

@DustinCampbell DustinCampbell changed the title Source files in obj are not parsed Generated files in .csproj projects are not processed Jun 6, 2017
@tsomogyi
Copy link
Author

tsomogyi commented Jun 6, 2017

Thanks for your efforts and sorry for the misleading description - I oversimplified the repro steps and ended up with something different (i.e. not realizing that the simpler scenario works after restarting VS Code).

@DustinCampbell DustinCampbell added this to the 1.11 milestone Jun 6, 2017
@DustinCampbell DustinCampbell self-assigned this Jun 6, 2017
@DustinCampbell
Copy link
Member

No worries! I'm just glad I understand the problem now. FWIW, the simpler scenario works without a restart, which is why I was confused. 😄

@asfernandes
Copy link

asfernandes commented Oct 23, 2017

I'm having the same problem with .net core 2, antlr4 and Linux.

dotnet build works ok, but vscode shows errors.

Edit: sorry, adding <Compile Include="$(IntermediateOutputPath)\*.cs" /> it worked.

@asfernandes
Copy link

The line <Compile Include="$(IntermediateOutputPath)\*.cs" /> causes warnings (CS2002) when the files are already generated when building via dotnet cli.

Is this bug of the extension fixed in beta versions?

@DustinCampbell
Copy link
Member

@asfernandes: Could you file a separate issue with full repro steps and details? My guess is that the same '*.cs' files are appearing twice in your project.

Note that the default globs for a .NET Core project should already include *.cs files under $(IntermediateOutputPath), so I would expect that <Compile Include to be redundant.

@asfernandes
Copy link

@DustinCampbell it's redundant with dotnet cli really, but the problem is that without Compile Include inside vscode it does not load the generated files in obj/Debug, then showing errors.

As I understand the problem is the same stated in this ticket. I tested the current beta version and the problem still exist.

@DustinCampbell
Copy link
Member

@asfernandes : I just verified the test project that was provided in this issue and the problem reported here does not repro. Could you please file a new issue with a full repro rather than commenting on a closed issue? At the moment, I really need more information to investigate the issue you're experiencing. Thanks!

@asfernandes
Copy link

Done: #1822

@DustinCampbell
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants