Build failed. Again.

“Predefined type ‘System.Object’ is not defined or imported” and other System namespace stuff missing in your solution?

This post was most recently updated on April 5th, 2024.

3 min read.

This article will explain how to fix an annoying situation, where Visual Studio fails to find any System types or other “basic” stuff you’re pretty sure your project should have, automagically, out-of-the-box, without you doing anything. Stuff that comes with .NET. Stuff that should just work.

But life isn’t always that simple.

Problem & Background

So I recently had a Windows Update brick my machine, and after reinstalling I was missing, well, practically everything. Setting up a dev environment for a customer project is usually a matter of a couple of minutes, but sometimes stuff goes sideways.

This was one such time. After configuring some debugging environment stuff, installing the right SDK, downloading about 600 megabytes worth of npm packages and hitting F5, I ran into… Well, I ran into whatever in tarnation this is supposed to be:

Yes. On build, suddenly absolutely everything was broken.

Almost every single line of code in my solution was suddenly underlined with annoying red squiggly lines.

What gives?

Reason

Who knows what actually went wrong and why? Probably a couple of things could cause something like this, really, but in my case, it turned out to be simple.

Long story short, something was holding on to a file in the bin folder and silently failing to build, so that Visual Studio ended up not having the symbols files it needed. Additionally, I think said files in the bin -folder were built for a .NET version that wasn’t present on my machine anymore.

Solution

I’ve found a couple of options on how to fix this. Starting with the least intrusive and moving on to the trickier ones. The last one worked for me this time, but I’ve run into the issue before and sometimes other stuff helps – so I’ll have them listed here.

1. Clean and rebuild

Boring, I know. But sometimes it works. And it’s really quick to do nowadays, so start with this.

2. Restart Visual Studio (possibly as an administrator)

So many issues with Visual Studio can be solved by restarting the application. This might be even more boring than the clean & rebuild option, but hey, if it helps…

And maybe throw in some admin rights for good measure. Sometimes that solves the file access issues. Annoying, but it is what it is.

Note, that this might not be what you want. But consider giving it a go, if you trust the code and dependencies you’re going to run.

3. Remove bin & obj folders manually

Sometimes it does finally have to come to this. There’s a file deep within your bin folder (probably) that’s locked by something, and your app can’t actually read that file, and that’s silently failing your build and creating/fetching/updating whatever symbols files Intellisense in Visual Studio needs to properly understand your code or build it at all.

You could just try to remove the folders (after shutting Visual Studio down), but that might not work. If it doesn’t, You’ll probably have to first remove a readonly -flag from the folders themselves, and then drill down and remove subfolders one-by-one.

Or maybe you won’t, but that’s how it went for me. Would’ve possibly been faster with PowerShell (recursively deleting files and folders starts from the “leaves” of the directory tree – i.e. at the deepest level), but it took about a minute through the UI, so… That’s what I did.

Here’s the PowerShell script for your reference, though:

Remove-Item -Path "C:\code\myrepo\bin" -Recurse -Force
Remove-Item -Path "C:\code\myrepo\obj" -Recurse -Force

If you have multiple projects, adding a wildcard like this might help:

Remove-Item -Path "C:\code\myrepo\*\bin\" -Recurse -Force
Remove-Item -Path "C:\code\myrepo\*\obj\" -Recurse -Force

If you want to perform a “dry run”, replace “-force” with “-whatif”

4. Double-check your global.json file (should you have one)

Still running into problems? Probably worth checking which .NET versions you have installed, and which version of the SDK the project requires. Otherwise you might have something like this at the end of the list of your nonsensical exceptions:

First, run this in Terminal (or Developer PowerShell directly in Visual Studio) to see which SDK versions you have installed on your machine:

> dotnet --list-sdks
7.0.407 [C:\Program Files\dotnet\sdk]
8.0.200 [C:\Program Files\dotnet\sdk]

Then compare this with what you have in your global.json -file, and update it to a matching version (or install the version required in the file) if need be. Mine was:

{
  "sdk": {
    "version": "7.0.304"
  }
}

Updating it to something you have (from running dotnet –list-sdks before) should help:

{
  "sdk": {
    "version": "7.0.407"
  }
}

That’s it for today! Hope it helps :)

mm
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments