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

Test Explorer cannot find a generated test file (exception) #15015

Open
mjvh80 opened this issue Feb 20, 2025 · 2 comments
Open

Test Explorer cannot find a generated test file (exception) #15015

mjvh80 opened this issue Feb 20, 2025 · 2 comments

Comments

@mjvh80
Copy link

mjvh80 commented Feb 20, 2025

Summary: Cannot open Roslyn generated tests via Test Explorer

The Roslyn generator project looks like

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
    <LangVersion>latest</LangVersion>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.12.0" PrivateAssets="all" />
  </ItemGroup>

</Project>

and something like

using Microsoft.CodeAnalysis;

namespace Generator
{
    [Generator]
    public class Generator : IIncrementalGenerator
    {
        public void Initialize(IncrementalGeneratorInitializationContext context) =>
            context.RegisterSourceOutput(context.CompilationProvider, (sourceCtx, _) =>
        {
            sourceCtx.AddSource("GeneratedTest.cs", """
            using System;
            using Microsoft.VisualStudio.TestTools.UnitTesting;

            namespace GeneratedTests
            {
                [TestClass]
                public sealed class GeneratedTest
                {
                    [TestMethod]
                    public void TestMethod()
                    {
                        Assert.IsTrue(true);
                    }
                }
            }
            """);
        });
    }
}

(of course in reality this actually generates something)

The consuming project looks like

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
    <PackageReference Include="MSTest" Version="3.8.1" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\Generator\Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
  </ItemGroup>

</Project>

If I now click Run All Tests on the Runner project, the tests are found, and run correctly. However, clicking on the TestMethod in Test Explorer

Image

leads to this exception seen in the Test Output window

[20/02/2025 11:32:04.319 AM]  Source location lookup for Source: D:\temp\Runner\bin\Debug\net8.0\Runner.dll by ManagedType: GeneratedTests.GeneratedTest and ManagedMethod: TestMethod
[20/02/2025 11:32:04.557 AM] [Error] System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
   at EnvDTE.ItemOperations.OpenFile(String FileName, String ViewKind)
   at Microsoft.VisualStudio.TestWindow.VsHost.IOpenTargetExtensions.<OpenFileAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.VisualStudio.TestWindow.VsHost.IOpenTargetExtensions.<OpenFileAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.VsHost.IOpenTargetExtensions.<OpenAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.Internal.TestWindowViewModel.<<OnNavigationRequested>b__140_1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.Logging.ILoggerExtensions.<CallWithCatchAsync>d__12.MoveNext()
[20/02/2025 11:32:05.374 AM] [Error] System.ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))
   at EnvDTE.ItemOperations.OpenFile(String FileName, String ViewKind)
   at Microsoft.VisualStudio.TestWindow.VsHost.IOpenTargetExtensions.<OpenFileAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.VisualStudio.TestWindow.VsHost.IOpenTargetExtensions.<OpenFileAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.VsHost.IOpenTargetExtensions.<OpenAsync>d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.Internal.TestWindowViewModel.<<OnNavigationRequested>b__140_1>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.VisualStudio.TestWindow.Logging.ILoggerExtensions.<CallWithCatchAsync>d__12.MoveNext()

and nothing happens.

Expected behavior: the generated source file is opened (I emit them if that matters, but possibly more is needed?).

Actual behavior: nothing happens

@nohwnd
Copy link
Member

nohwnd commented Feb 21, 2025

This is Test Explorer issue @AbhitejJohn please redirect this as appropriate.

@nohwnd
Copy link
Member

nohwnd commented Feb 21, 2025

@mjvh80 this is not the best repo to report this issue, if you post on https://developercommunity.visualstudio.com/home you will have a better luck getting this tracked by Test Explorer team. I tagged Abhitej above, so it is up to you if you decide to file it on visual studio, or keep it here. :)

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

2 participants