Skip to content

Commit b5d8b8a

Browse files
authored
Use solution folder as starting search path for global.json (#9789)
* Use solution folder as starting search path for global.json * remove unused UnconfiguredProject import * remove unneeded comments
1 parent 7c32371 commit b5d8b8a

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

src/Microsoft.VisualStudio.ProjectSystem.Managed.VS/ProjectSystem/VS/Retargeting/ProjectRetargetHandler.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@ namespace Microsoft.VisualStudio.ProjectSystem.VS.Retargeting;
1313
[Order(Order.Default)]
1414
internal sealed partial class ProjectRetargetHandler : IProjectRetargetHandler, IDisposable
1515
{
16-
private readonly UnconfiguredProject _unconfiguredProject;
1716
private readonly Lazy<IDotNetReleasesProvider> _releasesProvider;
1817
private readonly IFileSystem _fileSystem;
1918
private readonly IProjectThreadingService _projectThreadingService;
2019
private readonly IVsService<SVsTrackProjectRetargeting, IVsTrackProjectRetargeting2> _projectRetargetingService;
20+
private readonly IVsService<SVsSolution, IVsSolution> _solutionService;
2121

2222
private Guid _currentSdkDescriptionId = Guid.Empty;
2323
private Guid _sdkRetargetId = Guid.Empty;
2424

2525
[ImportingConstructor]
2626
public ProjectRetargetHandler(
27-
UnconfiguredProject unconfiguredProject,
2827
Lazy<IDotNetReleasesProvider> releasesProvider,
2928
IFileSystem fileSystem,
3029
IProjectThreadingService projectThreadingService,
31-
IVsService<SVsTrackProjectRetargeting, IVsTrackProjectRetargeting2> projectRetargetingService)
30+
IVsService<SVsTrackProjectRetargeting, IVsTrackProjectRetargeting2> projectRetargetingService,
31+
IVsService<SVsSolution, IVsSolution> solutionService)
3232
{
33-
_unconfiguredProject = unconfiguredProject;
3433
_releasesProvider = releasesProvider;
3534
_fileSystem = fileSystem;
3635
_projectThreadingService = projectThreadingService;
3736
_projectRetargetingService = projectRetargetingService;
37+
_solutionService = solutionService;
3838
}
3939

4040
public Task<IProjectTargetChange?> CheckForRetargetAsync(RetargetCheckOptions options)
@@ -133,11 +133,11 @@ public Task RetargetAsync(TextWriter outputLogger, RetargetOptions options, IPro
133133

134134
private async Task<string?> GetSdkVersionForProjectAsync()
135135
{
136-
string projectDirectory = _unconfiguredProject.GetProjectDirectory();
136+
string? solutionDirectory = await GetSolutionDirectoryAsync();
137137

138-
if (!string.IsNullOrEmpty(projectDirectory))
138+
if (!string.IsNullOrEmpty(solutionDirectory))
139139
{
140-
string? globalJsonPath = FindGlobalJsonPath(projectDirectory);
140+
string? globalJsonPath = FindGlobalJsonPath(solutionDirectory!);
141141
if (globalJsonPath is not null)
142142
{
143143
try
@@ -159,6 +159,22 @@ public Task RetargetAsync(TextWriter outputLogger, RetargetOptions options, IPro
159159
return null;
160160
}
161161

162+
private async Task<string?> GetSolutionDirectoryAsync()
163+
{
164+
IVsSolution? solution = await _solutionService.GetValueOrNullAsync();
165+
166+
if (solution is not null)
167+
{
168+
int hr = solution.GetSolutionInfo(out string solutionDirectory, out string _, out string _);
169+
if (hr == HResult.OK && !string.IsNullOrEmpty(solutionDirectory))
170+
{
171+
return solutionDirectory;
172+
}
173+
}
174+
175+
return null;
176+
}
177+
162178
public void Dispose()
163179
{
164180
if (_currentSdkDescriptionId != Guid.Empty || _sdkRetargetId != Guid.Empty)

0 commit comments

Comments
 (0)