ChatGPT解决这个技术问题 Extra ChatGPT

Cannot find .cs files for debugging .NET source code

I tried setting up debugging the .NET source by following this MDSN walkthrough. The Symbol cache is setup properly, as is the check 'Enable .NET Framework source stepping'.

But subsequently, whenever I want to step into .NET code, I am prompted to specify the location of the relevant cs file. The error message is You need to find <filename>.cs to view the source for the current call stack frame and The debugger could not locate the source file <filename>.cs.

I am offered to browse for the file (but I don't have it) or view a disassembly (but I don't want that).

How to step into the .NET source code?


d
dier

Well, in my case I was not trying to debug the .Net framework, but I was getting the same error: Cannot find .cs files for debugging .NET source code. So I had to turn on the "Enable just my code" option under: Tools -> Options -> Debugging -> General -> Enable just my Code

Per MS docs:

You can configure Visual Studio to automatically step over system, framework, and other non-user calls and collapse those calls in the call stack window.

https://docs.microsoft.com/en-us/visualstudio/debugger/just-my-code


I had enabled ".NET Framework source stepping" which had disable "Just my code" automatically and it drove me nuts not to be able to step in till i found this. So watch out when marking options to see what gets disabled :)
I thought I had previously enabled this. I went and checked, and sure enough, it wasn't checked. Thanks!
Great answer and fixed my problem but this will skip the file and not step into it. Might need another solution if the goal is step into the file.
J
JBSnorro

Checking Tools -> Options -> Debugging -> General -> Enable source server support mysteriously made everything work. I hope the same is true for you


Lord, why does it not work? Still getting prompted to browse to the source code file when attempting to step into framework code.
M
MHN

This took me an hour as well. I fixed it finally by resetting the Settings -> Tools -> Import and Export Settings -> Reset all settings


It worked! After reset, I just ignore 'options->Debugging' setting and imported what I exported. Nice!!
Worked in VS 2015 Pro, of course I don't have any custom settings I need as this would blow them away.
Resetting all my settings also resolved this for me ...what a headache. I have so many custom color settings for debugging had to save them to a separate file - which I can no longer use, grrr.
I reset the settings and restarted Visual Studio 2017. Restarting after resetting did the trick.
I did this reset for Visual Basic .NET with VS Ultimate 2013 because that is what I am coding with and this reset stopped the odd datetime.cs cannot find error message.
H
HomeMade

I've tried all the answers from above and nothing worked.

This solved it for me:

Debug -> Delete all Breakpoints

and it solved the problem! So many different things causing this issue.


After trying every single combination of JIT debugger settings, cleaning my solution, restarting VS2019 hundreds of times, repairing it, resetting all my settings, etc, etc, etc, it was Delete all Breakpoints that made my "Source Not Found" error go away, when attempting to debug an XUnit test. AAAAAAAAAAARGH. And thank you @HomeMade
As an additional step - I had to clean-ed & rebuilt my solution - to make it work for me.
How is it going to debug the code if all break points are deleted :/
S
Shahar Shokrani

Clean the solution before build solved the issue for me.

Just navigate and click on:

Build -> Clean Solution. Build -> Build Solution (Ctrl + Shift + B).


E
Eat at Joes

The answers here all talk about ignoring/avoiding the source code instead of actually stepping into it.

@JBSnorro is on the right track but the issue is Microsoft doesn't appear to publish all the .NET symbols/source you might encounter. I don't know if it is intentional on their part but to step into MS sources they need to publish every version of every assembly which is a big logistical task.

Tools -> Options -> Debugging -> General -> Enable source server support will work in many cases but I found for example mscorlib.dll for 4.6.1 was lacking symbols and/or decompiled source. So I couldn't step into common source code like Dictionary.cs or Task.cs as examples. Since MS symbol server's source & symbols likely change all the time. My issue may be resolved by the time you read this?

When I debug the same solution in Jetbrain's Rider, I can see and step through every class in every .NET assembly with no issue. However in VS I can only step into some class but not into others?

If you are really committed to stepping into all .NET source code you can use Jetbrain's DotPeek and decompile the .NET assemblies to actual .cs files to your disk. Then when you see this,

https://i.stack.imgur.com/hxo7W.png

You can now browse your disk to the source code you decompiled using DotPeek. Just make sure you decompiled the same assembly version you reference in your project. If not, the symbols may not match up with the correct source line numbers.

Instead, If you just want to hide this "Source Not Found" from constantly appearing and you don't care to step into the code there are no sources for, read @Alex Sherman's answer. You will need to figure out what assembly the offending file is contained in, then add that assembly name to the exclusion list.

Food for thought, I'm not a fan of Rider over VS. Rider is still a touch raw and lacks the crazy amount of built in tooling VS has. However!! I like to have it installed side-by-side in cases like this where I know I can get deeper into the weeds.


A
AdamL

Go to Tools -> Options -> Debugging -> and make sure "Enable Just My Code" is checked true.

This works for me. Make sure to upvote IT WORKS


A
Alex Sherman

If the error is from looking for "nullable.cs" or some other core source file:

You can disable symbols for specific modules by using Debug -> Options -> Debugging -> Symbols and then on the bottom Specify Excluded Modules.

This is useful for cases where you do want to disable "Just My Code" to step into other assemblies that you have PDBs for. Visual Studio I think comes with symbols for mscorlib.dll but does not include the source so sometimes stepping into things will look for a "nullable.cs" or some other core source file.


O
OlegI

Had the same issue, neither proposed above solutions helped me to solve the problem. Occurred in VS 2017. When I ran the project in Visual Studio 2019, everything worked. So just try to run it in other environments. Hope this answer will help someone


It helped me. I installed 2019 and everything works again. Maybe I messed up my settings.
R
RonnyR

I got this error when updating a NuGet package in a project, while missing to update it in other projects of the solution.

Going to the NuGet Manager of the solution and using the consolidate function, which ensures all projects in the solution use the same version, resolved the problem for me.


K
Kirsten

In my case I wound up renaming the class. Maybe it was getting confused with some other module. Once I had renamed it I could step in.


A
Arjun MG

I was having the same issue. Enable Just My Code and Enable source server support were already checked.

But my issue was not that. The csproj file had an extra tag without its opening tag.

I removed it and the issue resolved.

Hope it helps someone who is facing this issue.


C
CodeCaster

I had this error after using the checkbox to make single instance application in the program properties section:

https://i.stack.imgur.com/8gjec.png

It was strange that it didn't throw an error before this. The program would crash every time suddenly on startup and not break on a line that showed I didn't have a code error.

After unchecking the "Make single instance application", the program fired right up.