Skip to content

Commit 106309a

Browse files
authored
Force initialize EnvDTE.Project because EnvDTE.Project initialization does not specify its thread affinity (#7292)
1 parent 1f130f6 commit 106309a

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

src/NuGet.Clients/NuGet.PackageManagement.VisualStudio/Projects/CpsPackageReferenceProjectProvider.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ public NuGetProject TryCreateNuGetProject(
8484

8585
var projectServices = new CpsProjectSystemServices(vsProject, _scriptExecutor);
8686

87+
// Ensure the EnvDTE.Project is initialized.
88+
// Under the hood VSProjectAdapter actually has a Lazy<EnvDte.project>, which needs the UI thread to satisfy the cast.
89+
// If PMC is actually the first one that wants the EnvDTE.Project, it would fail because it's being called from the pipeline thread.
90+
// This is a workaround to make sure the EnvDTE.Project is initialized before any PMC code tries to use it.
91+
_ = vsProject.Project;
8792
var lazyUnconfiguredProject = new AsyncLazy<UnconfiguredProject>(async () =>
8893
{
8994
await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

test/NuGet.Tests.Apex/NuGet.Tests.Apex/NuGetEndToEndTests/NuGetConsoleTestCase.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,22 @@ public async Task InstallPackageFromPMCWithWhatIf_DoesNotInstallPackageAsync()
865865
CommonUtility.AssertPackageNotInPackagesConfig(VisualStudio, testContext.Project, packageName, Logger);
866866
}
867867

868+
[TestMethod]
869+
[Timeout(DefaultTimeout)]
870+
public void GetProject_CanAccessProjectName()
871+
{
872+
using var testContext = new ApexTestContext(VisualStudio, ProjectTemplate.NetCoreConsoleApp, Logger);
873+
874+
var nugetConsole = GetConsole(testContext.Project);
875+
nugetConsole.Clear();
876+
877+
nugetConsole.Execute("$proj = Get-Project; $proj.Name");
878+
879+
string pmcText = nugetConsole.GetText();
880+
pmcText.Should().Contain(testContext.Project.Name, because: pmcText);
881+
pmcText.Should().NotContain("FullyQualifiedErrorId", because: pmcText);
882+
}
883+
868884
public static IEnumerable<object[]> GetNetCoreTemplates()
869885
{
870886
yield return new object[] { ProjectTemplate.NetCoreConsoleApp };

0 commit comments

Comments
 (0)