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

Using Microsoft.NET.Test.Sdk as GlobalPackageReference breaks the build #5116

Closed
astaerk opened this issue Jun 22, 2024 · 5 comments
Closed

Comments

@astaerk
Copy link

astaerk commented Jun 22, 2024

Description

When using the package Microsoft.NET.Test.Sdk as GlobalPackageReference in a Directory.Packages.props the build fails with this error:

CS0234	The type or namespace name 'TestPlatform' does not exist in the namespace 'Microsoft.VisualStudio' (are you missing an assembly reference?)	
Project1.Tests	
%UserProfile%\.nuget\packages\microsoft.net.test.sdk\17.10.0\build\netcoreapp3.1\Microsoft.NET.Test.Sdk.Program.cs	3	

Using it as normal PackageReference in the .csproj works fine.

Steps to reproduce

  • create a new project
  • create a Directory.Packages.props and set ManagePackageVersionsCentrally = true
  • add <GlobalPackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
  • Build

I created a small repo to demonstrate the issue:
https://github.com/proj-arch/Microsoft.NET.Test.Sdk-GlobalPackageReference-BuildError

Expected behavior

the project can be successfully built

Actual behavior

the build breaks with the error

CS0234	The type or namespace name 'TestPlatform' does not exist in the namespace 'Microsoft.VisualStudio' (are you missing an assembly reference?)	
Project1.Tests	
%UserProfile%\.nuget\packages\microsoft.net.test.sdk\17.10.0\build\netcoreapp3.1\Microsoft.NET.Test.Sdk.Program.cs	3	

Diagnostic logs

Since the build fails, I cannot run the tests with diagnostics.

Environment

  • Windows 10 22H2 (Build 19045.4529)
  • Visual Studio Enterprise 2022 17.10.1
  • vstest.console 17.10.0
  • Microsoft.NET.Test.Sdk package in version 17.10.0
@nohwnd
Copy link
Member

nohwnd commented Jul 1, 2024

It looks like non of the lib files are referenced, adding this test code:

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Project1.Tests
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
        }
    }
}

Which is a standard test class also tells me that TestClass and TestMethod does not exist. I could not find similar issue reported with GlobalPackageReference, but the nugets look standard, and we produce them from different workflows, so this looks more like the package management problem.

@MichalPavlik have you seen such problem with global package management before?

@rickyno
Copy link

rickyno commented Mar 25, 2025

I can confirm I have reproduced the reported issue.

Adding Microsoft.NET.Test.Sdk as a GlobalPackageReference in a Directory.Packages.props and changing the PackageReference to a PackageVersion, in the project file, causes the Microsoft.NET.Test.Sdk.Program.cs file provided with the NuGet package to complain about a missing VisualStudio namespace. The content of that file is the following:

// <auto-generated> This file has been auto generated. </auto-generated>
using System;
[Microsoft.VisualStudio.TestPlatform.TestSDKAutoGeneratedCode]
class AutoGeneratedProgram {static void Main(string[] args){}} 

Adding Microsoft.NET.Test.Sdk as a PackageVersion in a Directory.Packages.props and having a PackageReference without the version attribute, in the project file, results in a successful build.

I know it is specific to Microsoft.NET.Test.Sdk because the exact same approach works fine for all other referenced packages in our solution. I even attempted to pin all the dependencies Microsoft.NET.Test.Sdk has by adding them all as references too, to no avail.

CPM with global package references does not work for this package.

@nohwnd
Copy link
Member

nohwnd commented Apr 1, 2025

GlobalPackageReference (surprisingly) does not work exactly like normal PackageReference, it is supposed to be dev time only.

Here is full investigation on testfx repo: microsoft/testfx#5353 with solution that allows you to make it work like normal package reference.

Here is an issue on nuget, describing that the feature is confusing and why, I would be glad if you read and confirm if my understanding of the confusion is correct. NuGet/Home#14223

@nohwnd nohwnd closed this as not planned Won't fix, can't repro, duplicate, stale Apr 1, 2025
@rickyno
Copy link

rickyno commented Apr 1, 2025

Thanks. I have a better understanding now. As you mentioned, the name is rather confusing if we can't actually use it to reference compile assets. I'll stick with PackageVersion then, too bad though, it means none of the projects will have the references by default, which is what I was looking for, to avoid pointing out the obvious to someone adding a new project (you forgot some refs). Oh well, better than nothing I guess.

@nohwnd
Copy link
Member

nohwnd commented Apr 1, 2025 via email

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

No branches or pull requests

3 participants