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

The SDK 'Microsoft.NET.Sdk' specified could not be found. #58286

Closed
yiszza opened this issue Dec 12, 2021 · 10 comments
Closed

The SDK 'Microsoft.NET.Sdk' specified could not be found. #58286

yiszza opened this issue Dec 12, 2021 · 10 comments
Assignees
Labels
Area-Infrastructure Contributor Pain The issue impedes progress for project collaborators.
Milestone

Comments

@yiszza
Copy link

yiszza commented Dec 12, 2021

Version Used: latest version

Steps to Reproduce:

RoslynTest.zip

code :

MSBuildWorkspace workspace = MSBuildWorkspace.Create();
var project = await workspace.OpenProjectAsync(@"../../../RoslynTest.csproj");

if (project.Documents.Count() == 0)
{
    var diagnostics = workspace.Diagnostics;
    foreach (var diagnostic in diagnostics)
    {
        Console.WriteLine(diagnostic.Message);
    }
}

run the demo,

Expected Behavior: project.Documents.Count() != 0;

Actual Behavior: project.Documents.Count() == 0; The SDK 'Microsoft.NET.Sdk' specified could not be found.

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged Issues and PRs which have not yet been triaged by a lead label Dec 12, 2021
@jinujoseph jinujoseph added Area-Infrastructure Contributor Pain The issue impedes progress for project collaborators. and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 14, 2021
@jinujoseph jinujoseph added this to the 17.1 milestone Dec 14, 2021
@cguedel
Copy link

cguedel commented Apr 21, 2022

Any update and/or workaround for this?

@yiszza
Copy link
Author

yiszza commented Apr 27, 2022

Any update and/or workaround for this?
Maybe not

@jinujoseph jinujoseph modified the milestones: 17.1, 17.3 Apr 27, 2022
@WeihanLi
Copy link
Contributor

The same problem met

@LordBenjamin
Copy link

I'm also able to reproduce.

csproj file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Build.Locator" Version="1.5.3" />
    <PackageReference Include="Microsoft.CodeAnalysis" Version="4.2.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.2.0" />
  </ItemGroup>
</Project>

Program.cs:

using System.Reflection;
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;

internal class Program {
    public static async Task Main(string[] args) {
        var vs = MSBuildLocator.QueryVisualStudioInstances().First();
        Console.WriteLine(vs.Name);
        Console.WriteLine(vs.Version);
        Console.WriteLine(vs.DiscoveryType);
        Console.WriteLine(vs.MSBuildPath);
        Console.WriteLine(vs.VisualStudioRootPath);
        Console.WriteLine();

        MSBuildLocator.RegisterDefaults();

        using (var workspace = MSBuildWorkspace.Create()) {
            workspace.WorkspaceFailed += Workspace_WorkspaceFailed;

            var project = await workspace.OpenProjectAsync(@"/path/to/file.csproj");
        }
    }

    private static void Workspace_WorkspaceFailed(object? sender, WorkspaceDiagnosticEventArgs e) {
        Console.WriteLine(e.Diagnostic.Message);
    }
}

Prints:

.NET Core SDK
6.0.400
DotNetSdk
C:\Program Files\dotnet\sdk\6.0.400
C:\Program Files\dotnet\sdk\6.0.400

Msbuild failed when processing the file '/path/to/file.csproj' with message: The SDK 'Microsoft.NET.Sdk' specified could not be found.  /path/to/file.csproj

SDKs installed:

$ dotnet --list-sdks
6.0.303 [C:\Program Files\dotnet\sdk]
6.0.400-preview.22301.10 [C:\Program Files\dotnet\sdk]
6.0.400 [C:\Program Files\dotnet\sdk]

VS versions installed: 2019, 2022, 2022-preview

@LordBenjamin
Copy link

LordBenjamin commented Aug 18, 2022

So I managed to get my code working:

  • Changed from targeting net6.0 to net472
  • Changed from MSBuildLocator.RegisterDefaults() to MSBuildLocator.RegisterInstance(MSBuildLocator.QueryVisualStudioInstances().Last())

This essentially means that instead of MSBuildLocator registering the .NET 6.0 SDK, it's registering Visual Studio 2022 Preview.

Note that this is the only combination that works on my PC - net472/VS2022 didn't work, and none of the three net6.0 SDKs worked.

I found that the "Standalone Code Analysis Tool" template in Visual Studio made it easy to reproduce this issue with various frameworks (because it lets you pick which MSBuild instance to use).
image
image

I hope this helps Microsoft debug the issue, and might help others as a workaround (assuming .NET Framework is an option for you).

@JoeRobich
Copy link
Member

@LordBenjamin Your original code would work if you downgrade MSBuildLocator to version 1.4.1. Seems that something has regressed in that package.

@JoeRobich
Copy link
Member

@yiszza To use MSBuildWorkspace we recommend using MSBuildLocator to load an installation of MSBuild Tools either from a .NET SDK or a VS install. In your project you only referenced the Microsoft.Build package which does not bring along the needed build tasks and target.

@ceztko
Copy link

ceztko commented Aug 23, 2022

Thanks @JoeRobich, downgrading to 1.4.1 works for me and saves my day. It's the bug correctly tracked here?

@JoeRobich
Copy link
Member

The bug is microsoft/MSBuildLocator#176. One workaround is to query for the instance your want then when registering the MSBuildPath, ensure it ends with a directory separator.

var instance = MSBuildLocator.QueryVisualStudioInstances().First();
var msBuildPath = Path.EndsInDirectorySeparator(instance.MSBuildPath)
    ? instance.MSBuildPath
    : instance.MSBuildPath + Path.DirectorySeparatorChar;
MSBuildLocator.RegisterMSBuildPath(msBuildPath);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Infrastructure Contributor Pain The issue impedes progress for project collaborators.
Projects
None yet
Development

No branches or pull requests

9 participants