Skip to content

Commit

Permalink
Merge pull request #726 from chsienki/ComponentDebugger
Browse files Browse the repository at this point in the history
  • Loading branch information
jmarolf authored Feb 17, 2021
2 parents 800be6d + 4dcffa0 commit eb99474
Show file tree
Hide file tree
Showing 14 changed files with 473 additions and 1 deletion.
1 change: 1 addition & 0 deletions NuGet.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
<add key="dotnet-eng" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json" />
<add key="dotnet5" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json" />
<add key="vs-impl" value="https://pkgs.dev.azure.com/azure-public/vside/_packaging/vs-impl/nuget/v3/index.json" />
</packageSources>
<disabledPackageSources />
</configuration>
7 changes: 7 additions & 0 deletions Roslyn-SDK.sln
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "Microsoft.CodeAnalysis.Visu
EndProject
Project("{778DAE3C-4631-46EA-AA77-85C1314464D9}") = "Microsoft.CodeAnalysis.VisualBasic.SourceGenerators.Testing.XUnit.UnitTests", "tests\Microsoft.CodeAnalysis.Testing\Microsoft.CodeAnalysis.VisualBasic.SourceGenerators.Testing.XUnit.UnitTests\Microsoft.CodeAnalysis.VisualBasic.SourceGenerators.Testing.XUnit.UnitTests.vbproj", "{92BD1781-5DB4-4F72-BCCB-0D64C0790A2B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Roslyn.ComponentDebugger", "src\VisualStudio.Roslyn.SDK\ComponentDebugger\Roslyn.ComponentDebugger.csproj", "{7E91C1C7-A836-4378-8ABC-9298C13228D1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -539,6 +541,10 @@ Global
{92BD1781-5DB4-4F72-BCCB-0D64C0790A2B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{92BD1781-5DB4-4F72-BCCB-0D64C0790A2B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{92BD1781-5DB4-4F72-BCCB-0D64C0790A2B}.Release|Any CPU.Build.0 = Release|Any CPU
{7E91C1C7-A836-4378-8ABC-9298C13228D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7E91C1C7-A836-4378-8ABC-9298C13228D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7E91C1C7-A836-4378-8ABC-9298C13228D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7E91C1C7-A836-4378-8ABC-9298C13228D1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -635,6 +641,7 @@ Global
{7D9C0EF5-7383-4E35-811B-3288B3C806F3} = {9905147E-CC1F-42A0-BD27-05586C583DF7}
{7C3FE60E-055B-4E0C-BB85-C7E94A640074} = {9905147E-CC1F-42A0-BD27-05586C583DF7}
{92BD1781-5DB4-4F72-BCCB-0D64C0790A2B} = {9905147E-CC1F-42A0-BD27-05586C583DF7}
{7E91C1C7-A836-4378-8ABC-9298C13228D1} = {F9B73995-76C6-4056-ADA9-18342F951361}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {56695AA9-EA80-47A7-8562-E51285906C54}
Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<UsingToolNetFrameworkReferenceAssemblies>true</UsingToolNetFrameworkReferenceAssemblies>
<UsingToolMicrosoftNetCompilers>true</UsingToolMicrosoftNetCompilers>
<UsingToolSymbolUploader>true</UsingToolSymbolUploader>
<MicrosoftNetCompilersToolsetVersion>3.8.0-4.20464.1</MicrosoftNetCompilersToolsetVersion>
<MicrosoftNetCompilersToolsetVersion>3.8.0-5.final</MicrosoftNetCompilersToolsetVersion>
<!-- Force prior version due to https://github.com/microsoft/vstest/pull/2192 and https://github.com/microsoft/vstest/pull/2067 -->
<MicrosoftNETTestSdkVersion>16.1.1</MicrosoftNETTestSdkVersion>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Collections.Immutable;
using System.ComponentModel.Composition;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.ProjectSystem;

namespace Roslyn.ComponentDebugger
{
[Export(ExportContractNames.Scopes.ConfiguredProject, typeof(IProjectCapabilitiesProvider))]
[AppliesTo(ProjectCapabilities.CSharp + " | " + ProjectCapabilities.VB)]
public class CapabilityProvider : ConfiguredProjectCapabilitiesProviderBase
{
private readonly IProjectSnapshotService snapshotService;

[ImportingConstructor]
[System.Obsolete("This exported object must be obtained through the MEF export provider.", error: true)]
public CapabilityProvider(ConfiguredProject configuredProject, IProjectSnapshotService snapshotService)
: base(nameof(CapabilityProvider), configuredProject)
{
this.snapshotService = snapshotService;
}

protected override async Task<ImmutableHashSet<string>> GetCapabilitiesAsync(CancellationToken cancellationToken)
{
// an alternative design could be to have 'IsRoslynComponent' just define the <Capability... directly in the managed.core targets
// but that would require a specific roslyn version to work, this allows it to be backwards compatible with older SDKs
var caps = Empty.CapabilitiesSet;

var snapshot = await snapshotService.GetLatestVersionAsync(ConfiguredProject, cancellationToken: cancellationToken).ConfigureAwait(false);
var isRoslynComponentProperty = snapshot.Value.ProjectInstance.GetPropertyValue(Constants.RoslynComponentPropertyName);
var isComponent = string.Compare(isRoslynComponentProperty.Trim(), "true", System.StringComparison.OrdinalIgnoreCase) == 0;
if (isComponent)
{
caps = caps.Add(Constants.RoslynComponentCapability);
}
return caps;
}
}
}
17 changes: 17 additions & 0 deletions src/VisualStudio.Roslyn.SDK/ComponentDebugger/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace Roslyn.ComponentDebugger
{
internal static class Constants
{
public const string RoslynComponentPropertyName = "IsRoslynComponent";

public const string RoslynComponentCapability = "RoslynComponent";

public const string CommandName = "DebugRoslynComponent";

public const string TargetProjectPropertyName = "targetProject";
}
}
109 changes: 109 additions & 0 deletions src/VisualStudio.Roslyn.SDK/ComponentDebugger/DebugProfileProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.VisualStudio.ProjectSystem;
using Microsoft.VisualStudio.ProjectSystem.Debug;
using Microsoft.VisualStudio.ProjectSystem.VS.Debug;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.Threading;
using Task = System.Threading.Tasks.Task;

namespace Roslyn.ComponentDebugger
{
[Export(typeof(IDebugProfileLaunchTargetsProvider))]
[AppliesTo(Constants.RoslynComponentCapability)]
public class DebugProfileProvider : IDebugProfileLaunchTargetsProvider
{
private readonly ConfiguredProject _configuredProject;
private readonly IDebugTokenReplacer _tokenReplacer;
private readonly AsyncLazy<string> _compilerRoot;

[ImportingConstructor]
[Obsolete("This exported object must be obtained through the MEF export provider.", error: true)]
public DebugProfileProvider(ConfiguredProject configuredProject, IDebugTokenReplacer tokenReplacer, SVsServiceProvider? serviceProvider)
{
_configuredProject = configuredProject;
_tokenReplacer = tokenReplacer;
_compilerRoot = new AsyncLazy<string>(() => GetCompilerRootAsync(serviceProvider), ThreadHelper.JoinableTaskFactory);
}

public Task OnAfterLaunchAsync(DebugLaunchOptions launchOptions, ILaunchProfile profile) => Task.CompletedTask;

public Task OnBeforeLaunchAsync(DebugLaunchOptions launchOptions, ILaunchProfile profile) => Task.CompletedTask;

public bool SupportsProfile(ILaunchProfile? profile) => Constants.CommandName.Equals(profile?.CommandName, StringComparison.Ordinal);

public async Task<IReadOnlyList<IDebugLaunchSettings>> QueryDebugTargetsAsync(DebugLaunchOptions launchOptions, ILaunchProfile? profile)
{
// set up the managed (net fx) debugger to start a process
// https://github.com/dotnet/roslyn-sdk/issues/729
var settings = new DebugLaunchSettings(launchOptions)
{
LaunchDebugEngineGuid = Microsoft.VisualStudio.ProjectSystem.Debug.DebuggerEngines.ManagedOnlyEngine,
LaunchOperation = DebugLaunchOperation.CreateProcess
};

// try and get the target project
var targetProjectUnconfigured = await TryGetTargetProjectAsync(profile).ConfigureAwait(false);
if (targetProjectUnconfigured is object)
{
settings.CurrentDirectory = Path.GetDirectoryName(targetProjectUnconfigured.FullPath);
var compiler = _configuredProject.Capabilities.Contains(ProjectCapabilities.VB) ? "vbc.exe" : "csc.exe";
var compilerRoot = await _compilerRoot.GetValueAsync().ConfigureAwait(false);
settings.Executable = Path.Combine(compilerRoot, compiler);

// try and get the configured version of the target project
var targetProject = await targetProjectUnconfigured.GetSuggestedConfiguredProjectAsync().ConfigureAwait(false);
if (targetProject is object)
{
// get its compilation args
var args = await targetProject.GetCompilationArgumentsAsync().ConfigureAwait(false);

// append the command line args to the debugger launch
settings.Arguments = string.Join(" ", args);
}
}

// https://github.com/dotnet/roslyn-sdk/issues/728 : better error handling
return new IDebugLaunchSettings[] { settings };
}

private static async Task<string> GetCompilerRootAsync(SVsServiceProvider? serviceProvider)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

// https://github.com/dotnet/roslyn-sdk/issues/729
object rootDir = string.Empty;
var shell = (IVsShell?)serviceProvider?.GetService(typeof(SVsShell));
shell?.GetProperty((int)__VSSPROPID2.VSSPROPID_InstallRootDir, out rootDir);
return Path.Combine((string)rootDir, "MSBuild", "Current", "Bin", "Roslyn");
}

private async Task<UnconfiguredProject?> TryGetTargetProjectAsync(ILaunchProfile? profile)
{
UnconfiguredProject? targetProject = null;
object? value = null;
profile?.OtherSettings?.TryGetValue(Constants.TargetProjectPropertyName, out value);

if (value is string targetProjectPath)
{
// expand any variables in the path, and root it based on this project
var replacedProjectPath = await _tokenReplacer.ReplaceTokensInStringAsync(targetProjectPath, true).ConfigureAwait(false);
replacedProjectPath = _configuredProject.UnconfiguredProject.MakeRooted(replacedProjectPath);

targetProject = _configuredProject.Services.ProjectService.LoadedUnconfiguredProjects.SingleOrDefault(p => p.FullPath == replacedProjectPath);
}

return targetProject;
}
}
}
19 changes: 19 additions & 0 deletions src/VisualStudio.Roslyn.SDK/ComponentDebugger/DebuggerOptions.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<UserControl x:Class="Roslyn.ComponentDebugger.DebuggerOptions"
x:ClassModifier="internal"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="Auto"
HorizontalAlignment="Left"
VerticalAlignment="Top">
<Grid Margin="0,2,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="158" />
<ColumnDefinition Width="350" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<!-- https://github.com/dotnet/roslyn-sdk/issues/730 : Localization -->
<Label Margin="4,4,3,5">Target Project:</Label>
<ComboBox Grid.Column="1" Margin="5,7,2,6" ItemsSource="{Binding ProjectNames}" SelectedIndex="{Binding SelectedProjectIndex, Mode=TwoWay}" />
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Windows.Controls;

namespace Roslyn.ComponentDebugger
{
/// <summary>
/// Interaction logic for DebuggerOptions.xaml
/// </summary>
internal sealed partial class DebuggerOptions : UserControl
{
public DebuggerOptions()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.ProjectSystem;
using Microsoft.VisualStudio.ProjectSystem.Debug;

namespace Roslyn.ComponentDebugger
{
internal class DebuggerOptionsViewModel : INotifyPropertyChanged
{
private IWritableLaunchProfile? _launchProfile;

private readonly ImmutableArray<ConfiguredProject> _targetProjects;

private readonly IEnumerable<string> _targetProjectNames;

public event PropertyChangedEventHandler? PropertyChanged;

public DebuggerOptionsViewModel(ImmutableArray<ConfiguredProject> targetProjects)
{
_targetProjects = targetProjects;
_targetProjectNames = _targetProjects.Select(t => Path.GetFileNameWithoutExtension(t.UnconfiguredProject.FullPath));
}

public IEnumerable<string> ProjectNames { get => _targetProjectNames; }

public IWritableLaunchProfile? LaunchProfile
{
get => _launchProfile;
set
{
_launchProfile = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedProjectIndex)));
}
}

public int SelectedProjectIndex
{
get
{
if (LaunchProfile?.OtherSettings.ContainsKey(Constants.TargetProjectPropertyName) == true)
{
var target = LaunchProfile.OtherSettings[Constants.TargetProjectPropertyName].ToString();
for (var i = 0; i < _targetProjects.Length; i++)
{
if (_targetProjects[i].UnconfiguredProject.FullPath.Equals(target, StringComparison.OrdinalIgnoreCase))
{
return i;
}
}
}
return -1;
}
set
{
if (LaunchProfile is object)
{
var newTargetProject = _targetProjects[value].UnconfiguredProject;
LaunchProfile.OtherSettings[Constants.TargetProjectPropertyName] = newTargetProject.FullPath;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(SelectedProjectIndex)));
}
}
}
}
}
Loading

0 comments on commit eb99474

Please sign in to comment.