|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | +using System.Reflection; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Globalization; |
| 7 | +using System.Collections.Generic; |
| 8 | +using GitHub.Logging; |
| 9 | +using Microsoft.VisualStudio.Shell; |
| 10 | +using Microsoft.VisualStudio.Shell.Interop; |
| 11 | +using Microsoft.VisualStudio.Shell.Settings; |
| 12 | +using Microsoft.VisualStudio.Settings; |
| 13 | +using Serilog; |
| 14 | + |
| 15 | +namespace GitHub.VisualStudio.Helpers |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// This a workaround for extensions that define a ProvideBindingPath attribute and |
| 19 | + /// install for AllUsers. |
| 20 | + /// </summary> |
| 21 | + /// <remarks> |
| 22 | + /// Extensions that are installed for AllUsers, will also be installed for all |
| 23 | + /// instances of Visual Studio - including the experimental (Exp) instance which |
| 24 | + /// is used in development. This isn't a problem so long as all features that |
| 25 | + /// exist in the AllUsers extension, also exist in the extension that is being |
| 26 | + /// developed. |
| 27 | + /// |
| 28 | + /// When an extension uses the ProvideBindingPath attribute, the binding path for |
| 29 | + /// the AllUsers extension gets installed at the same time as the one in development. |
| 30 | + /// This doesn't matter when an assembly is strong named and is loaded using its |
| 31 | + /// full name (including version number). When an assembly is loaded using its |
| 32 | + /// simple name, assemblies from the AllUsers extension can end up loaded at the |
| 33 | + /// same time as the extension being developed. This can happen when an assembly |
| 34 | + /// is loaded from XAML or an .imagemanifest. |
| 35 | + /// |
| 36 | + /// This is a workaround for that issue. The <see cref="FindRedundantBindingPaths(List{string}, string)" /> |
| 37 | + /// method will check to see if a reference assembly could be loaded from an alternative |
| 38 | + /// binding path. It will return any alternative paths that is finds. |
| 39 | + /// See https://github.com/github/VisualStudio/issues/1995 |
| 40 | + /// </remarks> |
| 41 | + public static class BindingPathHelper |
| 42 | + { |
| 43 | + static readonly ILogger log = LogManager.ForContext(typeof(BindingPathHelper)); |
| 44 | + |
| 45 | + internal static void CheckBindingPaths(Assembly assembly, IServiceProvider serviceProvider) |
| 46 | + { |
| 47 | + log.Information("Looking for assembly on wrong binding path"); |
| 48 | + |
| 49 | + ThreadHelper.CheckAccess(); |
| 50 | + var bindingPaths = FindBindingPaths(serviceProvider); |
| 51 | + var bindingPath = FindRedundantBindingPaths(bindingPaths, assembly.Location) |
| 52 | + .FirstOrDefault(); |
| 53 | + if (bindingPath == null) |
| 54 | + { |
| 55 | + log.Information("No incorrect binding path found"); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // Log what has been detected |
| 60 | + log.Warning("Found assembly on wrong binding path {BindingPath}", bindingPath); |
| 61 | + |
| 62 | + var message = string.Format(CultureInfo.CurrentCulture, @"Found assembly on wrong binding path: |
| 63 | +{0} |
| 64 | +
|
| 65 | +Would you like to learn more about this issue?", bindingPath); |
| 66 | + var action = VsShellUtilities.ShowMessageBox(serviceProvider, message, "GitHub for Visual Studio", OLEMSGICON.OLEMSGICON_WARNING, |
| 67 | + OLEMSGBUTTON.OLEMSGBUTTON_YESNO, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST); |
| 68 | + if (action == 6) // Yes = 6, No = 7 |
| 69 | + { |
| 70 | + Process.Start("https://github.com/github/VisualStudio/issues/2006"); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Find any alternative binding path that might have been installed by an AllUsers extension. |
| 76 | + /// </summary> |
| 77 | + /// <param name="bindingPaths">A list of binding paths to search</param> |
| 78 | + /// <param name="assemblyLocation">A reference assembly that has been loaded from the correct path.</param> |
| 79 | + /// <returns>A list of redundant binding paths.</returns> |
| 80 | + public static IList<string> FindRedundantBindingPaths(IEnumerable<string> bindingPaths, string assemblyLocation) |
| 81 | + { |
| 82 | + var fileName = Path.GetFileName(assemblyLocation); |
| 83 | + return bindingPaths |
| 84 | + .Select(p => (path: p, file: Path.Combine(p, fileName))) |
| 85 | + .Where(pf => File.Exists(pf.file)) |
| 86 | + .Where(pf => !pf.file.Equals(assemblyLocation, StringComparison.OrdinalIgnoreCase)) |
| 87 | + .Select(pf => pf.path) |
| 88 | + .ToList(); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// Find Visual Studio's list of binding paths. |
| 93 | + /// </summary> |
| 94 | + /// <returns>A list of binding paths.</returns> |
| 95 | + public static IEnumerable<string> FindBindingPaths(IServiceProvider serviceProvider) |
| 96 | + { |
| 97 | + const string bindingPaths = "BindingPaths"; |
| 98 | + var manager = new ShellSettingsManager(serviceProvider); |
| 99 | + var store = manager.GetReadOnlySettingsStore(SettingsScope.Configuration); |
| 100 | + foreach (var guid in store.GetSubCollectionNames(bindingPaths)) |
| 101 | + { |
| 102 | + var guidPath = Path.Combine(bindingPaths, guid); |
| 103 | + foreach (var path in store.GetPropertyNames(guidPath)) |
| 104 | + { |
| 105 | + yield return path; |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments