private TestServiceProvider(
            IOmniSharpEnvironment environment,
            ILoggerFactory loggerFactory,
            IAssemblyLoader assemblyLoader,
            IAnalyzerAssemblyLoader analyzerAssemblyLoader,
            IMemoryCache memoryCache,
            ISharedTextWriter sharedTextWriter,
            IMSBuildLocator msbuildLocator,
            IEventEmitter eventEmitter,
            IDotNetCliService dotNetCliService,
            IConfigurationRoot configuration,
            IOptionsMonitor <OmniSharpOptions> optionsMonitor)
        {
            _logger = loggerFactory.CreateLogger <TestServiceProvider>();

            AddService(environment);
            AddService(loggerFactory);
            AddService(assemblyLoader);
            AddService(memoryCache);
            AddService(sharedTextWriter);
            AddService(msbuildLocator);
            AddService(eventEmitter);
            AddService(dotNetCliService);
            AddService(configuration);
            AddService(optionsMonitor);
            AddService(analyzerAssemblyLoader);
        }
        public ProjectSystem(
            IOmniSharpEnvironment environment,
            OmniSharpWorkspace workspace,
            IMSBuildLocator msbuildLocator,
            DotNetCliService dotNetCliService,
            SdksPathResolver sdksPathResolver,
            MetadataFileReferenceCache metadataFileReferenceCache,
            IEventEmitter eventEmitter,
            IFileSystemWatcher fileSystemWatcher,
            FileSystemHelper fileSystemHelper,
            ILoggerFactory loggerFactory)
        {
            _environment                = environment;
            _workspace                  = workspace;
            _propertyOverrides          = msbuildLocator.RegisteredInstance.PropertyOverrides;
            _dotNetCli                  = dotNetCliService;
            _sdksPathResolver           = sdksPathResolver;
            _metadataFileReferenceCache = metadataFileReferenceCache;
            _eventEmitter               = eventEmitter;
            _fileSystemWatcher          = fileSystemWatcher;
            _fileSystemHelper           = fileSystemHelper;
            _loggerFactory              = loggerFactory;

            _projectsToProcess = new Queue <ProjectFileInfo>();
            _logger            = loggerFactory.CreateLogger <ProjectSystem>();
        }
        public static void RegisterDefaultInstance(this IMSBuildLocator msbuildLocator, ILogger logger)
        {
            var bestInstanceFound = GetBestInstance(msbuildLocator, logger, out var invalidVSFound, out var vsWithoutSdkResolver);

            if (bestInstanceFound != null)
            {
                // Did we end up choosing the standalone MSBuild because there was an invalid Visual Studio?
                // If so, provide a helpful message to the user.
                if (invalidVSFound && bestInstanceFound.DiscoveryType == DiscoveryType.StandAlone)
                {
                    logger.LogWarning(
                        @"It looks like you have Visual Studio lower than VS 2019 16.3 installed.
 Try updating Visual Studio to the most recent release to enable better MSBuild support."
                        );
                }

                if (vsWithoutSdkResolver && bestInstanceFound.DiscoveryType == DiscoveryType.StandAlone)
                {
                    logger.LogWarning(
                        @"It looks like you have Visual Studio 2019 installed without .NET Core SDK support which is required by OmniSharp.
 Try updating Visual Studio 2019 installation with .NET Core SDK to enable better MSBuild support."
                        );
                }

                msbuildLocator.RegisterInstance(bestInstanceFound);
            }
            else
            {
                logger.LogError("Could not locate MSBuild instance to register with OmniSharp");
            }
        }
Example #4
0
        public ProjectSystem(
            IOmniSharpEnvironment environment,
            OmniSharpWorkspace workspace,
            IMSBuildLocator msbuildLocator,
            IDotNetCliService dotNetCliService,
            SdksPathResolver sdksPathResolver,
            MetadataFileReferenceCache metadataFileReferenceCache,
            IEventEmitter eventEmitter,
            IFileSystemWatcher fileSystemWatcher,
            FileSystemHelper fileSystemHelper,
            ILoggerFactory loggerFactory,
            CachingCodeFixProviderForProjects codeFixesForProjects,
            RulesetsForProjects rulesetsForProjects,
            IAnalyzerAssemblyLoader assemblyLoader,
            [ImportMany] IEnumerable <IMSBuildEventSink> eventSinks)
        {
            _environment                = environment;
            _workspace                  = workspace;
            _propertyOverrides          = msbuildLocator.RegisteredInstance.PropertyOverrides;
            _dotNetCli                  = dotNetCliService;
            _sdksPathResolver           = sdksPathResolver;
            _metadataFileReferenceCache = metadataFileReferenceCache;
            _eventEmitter               = eventEmitter;
            _fileSystemWatcher          = fileSystemWatcher;
            _fileSystemHelper           = fileSystemHelper;
            _loggerFactory              = loggerFactory;
            _rulesetsForProjects        = rulesetsForProjects;
            _eventSinks                 = eventSinks.ToImmutableArray();

            _projectsToProcess = new Queue <ProjectFileInfo>();
            _logger            = loggerFactory.CreateLogger <ProjectSystem>();
            _assemblyLoader    = assemblyLoader;
        }
 public ConfigurationFormWriter(bool isLocal, Configuration configuration, IMSBuildLocator locator)
 {
     _isLocal = isLocal;
     Configuration = configuration;
     _locator = locator;
     _atReadTimeIgnoreFileExisted = configuration.IgnoreFile.Exists && configuration.IgnoreFile.Item.File.Trim().Length > 0;
 }
Example #6
0
 public ConfigurationFormWriter(bool isLocal, Configuration configuration, IMSBuildLocator locator)
 {
     _isLocal      = isLocal;
     Configuration = configuration;
     _locator      = locator;
     _atReadTimeIgnoreFileExisted = configuration.IgnoreFile.Exists && configuration.IgnoreFile.Item.File.Trim().Length > 0;
 }
Example #7
0
        public static MSBuildInstance GetBestInstance(this IMSBuildLocator msbuildLocator, Version minimumMSBuildVersion, ILogger logger, out bool invalidVSFound, out bool vsWithoutSdkResolver)
        {
            invalidVSFound       = false;
            vsWithoutSdkResolver = false;
            MSBuildInstance bestMatchInstance = null;
            var             bestMatchScore    = 0;

            foreach (var instance in msbuildLocator.GetInstances())
            {
                var score = GetInstanceFeatureScore(instance, minimumMSBuildVersion);

                logger.LogDebug($"MSBuild instance {instance.Name} {instance.Version} scored at {score}");

                invalidVSFound       = invalidVSFound || instance.IsInvalidVisualStudio();
                vsWithoutSdkResolver = vsWithoutSdkResolver || (!instance.IsInvalidVisualStudio() && !instance.HasDotNetSdksResolvers());

                if (bestMatchInstance == null ||
                    score > bestMatchScore ||
                    score == bestMatchScore && instance.Version > bestMatchInstance.Version)
                {
                    bestMatchInstance = instance;
                    bestMatchScore    = score;
                }
            }

            return(bestMatchInstance);
        }
Example #8
0
        public static MSBuildInstance GetBestInstance(this IMSBuildLocator msbuildLocator, ILogger logger, out bool invalidVSFound)
        {
            invalidVSFound = false;
            MSBuildInstance bestMatchInstance = null;
            var             bestMatchScore    = 0;

            foreach (var instance in msbuildLocator.GetInstances())
            {
                var score = GetInstanceFeatureScore(instance);

                logger.LogDebug($"MSBuild instance {instance.Name} {instance.Version} scored at {score}");

                invalidVSFound = invalidVSFound || instance.IsInvalidVisualStudio();

                if (bestMatchInstance == null ||
                    score > bestMatchScore ||
                    score == bestMatchScore && instance.Version > bestMatchInstance.Version)
                {
                    bestMatchInstance = instance;
                    bestMatchScore    = score;
                }
            }

            return(bestMatchInstance);
        }
        public void SetUp()
        {
            var container = ContainerHelper.ConfigureContainer().Build();

            _command = container.Resolve <CleanCommand>();
            _locator = container.Resolve <IMSBuildLocator>();

            _locator.RegisterDefaults();
        }
        public AbstractMSBuildTestFixture(ITestOutputHelper output)
            : base(output)
        {
            _assemblyLoader = new AssemblyLoader(this.LoggerFactory);
            _msbuildLocator = MSBuildLocator.CreateStandAlone(this.LoggerFactory, _assemblyLoader, allowMonoPaths: false);

            // Some tests require MSBuild to be discovered early
            // to ensure that the Microsoft.Build.* assemblies can be located
            _msbuildLocator.RegisterDefaultInstance(this.LoggerFactory.CreateLogger("MSBuildTests"));
        }
        public AbstractMSBuildTestFixture(ITestOutputHelper output)
            : base(output)
        {
            _assemblyLoader         = new AssemblyLoader(this.LoggerFactory);
            _analyzerAssemblyLoader = ShadowCopyAnalyzerAssemblyLoader.Instance;
            _msbuildLocator         = MSBuildLocator.CreateStandAlone(this.LoggerFactory, _assemblyLoader);

            // Some tests require MSBuild to be discovered early
            // to ensure that the Microsoft.Build.* assemblies can be located
            _msbuildLocator.RegisterDefaultInstance(this.LoggerFactory.CreateLogger("MSBuildTests"), dotNetInfo: null);
        }
Example #12
0
        public AbstractMSBuildTestFixture(ITestOutputHelper output)
            : base(output)
        {
            _assemblyLoader         = new AssemblyLoader(this.LoggerFactory);
            _analyzerAssemblyLoader = ShadowCopyAnalyzerAssemblyLoader.Instance;

            // Since we can only load MSBuild once into our process we need to include
            // prerelease version so that our .NET 7 tests will pass.
            var configuration = new Dictionary <string, string>
            {
                ["sdk:IncludePrereleases"] = bool.TrueString
            }.ToConfiguration();

            _msbuildLocator = MSBuildLocator.CreateDefault(this.LoggerFactory, _assemblyLoader, configuration);

            // Some tests require MSBuild to be discovered early
            // to ensure that the Microsoft.Build.* assemblies can be located
            _msbuildLocator.RegisterDefaultInstance(this.LoggerFactory.CreateLogger("MSBuildTests"), dotNetInfo: null);
        }
Example #13
0
        public static void RegisterDefaultInstance(this IMSBuildLocator msbuildLocator, ILogger logger, DotNetInfo dotNetInfo = null)
        {
            var minimumMSBuildVersion = GetSdkMinimumMSBuildVersion(dotNetInfo, logger);

            logger.LogDebug($".NET SDK requires MSBuild instances version {minimumMSBuildVersion} or higher");

            var bestInstanceFound = GetBestInstance(msbuildLocator, minimumMSBuildVersion, logger, out var invalidVSFound, out var vsWithoutSdkResolver);

            if (bestInstanceFound != null)
            {
                if (bestInstanceFound.Version < minimumMSBuildVersion)
                {
                    if (bestInstanceFound.DiscoveryType == DiscoveryType.Mono)
                    {
                        logger.LogWarning(
                            $@"It looks like you have Mono installed which contains a MSBuild lower than {minimumMSBuildVersion} which is the minimum supported by the configured .NET Core Sdk.
 Try updating Mono to the latest stable or preview version to enable better .NET Core Sdk support."
                            );
                    }
                    else if (bestInstanceFound.DiscoveryType == DiscoveryType.VisualStudioSetup)
                    {
                        logger.LogWarning(
                            $@"It looks like you have Visual Studio 2019 installed which contains a MSBuild lower than {minimumMSBuildVersion} which is the minimum supported by the configured .NET Core Sdk.
 Try updating Visual Studio to version {minimumMSBuildVersion} or higher to enable better .NET Core Sdk support."
                            );
                    }
                    else if (bestInstanceFound.DiscoveryType == DiscoveryType.UserOverride)
                    {
                        logger.LogWarning(
                            $@"It looks like you have overridden the version of MSBuild with a version lower than {minimumMSBuildVersion} which is the minimum supported by the configured .NET Core Sdk.
 Try updating your MSBuild to version {minimumMSBuildVersion} or higher to enable better .NET Core Sdk support."
                            );
                    }
                }

                msbuildLocator.RegisterInstance(bestInstanceFound);
            }
            else
            {
                throw new MSBuildNotFoundException("Could not locate MSBuild instance to register with OmniSharp.");
            }
        }
Example #14
0
        public static IServiceProvider Create(
            ITestOutputHelper testOutput,
            IOmniSharpEnvironment environment,
            ILoggerFactory loggerFactory,
            IAssemblyLoader assemblyLoader,
            IAnalyzerAssemblyLoader analyzerAssemblyLoader,
            IMSBuildLocator msbuildLocator,
            IConfiguration configurationData  = null,
            DotNetCliVersion dotNetCliVersion = DotNetCliVersion.Current,
            IEventEmitter eventEmitter        = null)
        {
            eventEmitter = eventEmitter ?? NullEventEmitter.Instance;

            var dotNetCliService = CreateDotNetCliService(dotNetCliVersion, loggerFactory, eventEmitter);
            var configuration    = CreateConfiguration(configurationData, dotNetCliService);
            var sharedTextWriter = CreateSharedTextWriter(testOutput);

            return(new TestServiceProvider(
                       environment, loggerFactory, assemblyLoader, analyzerAssemblyLoader, sharedTextWriter,
                       msbuildLocator, eventEmitter, dotNetCliService, configuration));
        }
Example #15
0
        public static MSBuildInstance GetBestInstance(this IMSBuildLocator msbuildLocator, out bool invalidVSFound)
        {
            invalidVSFound = false;
            MSBuildInstance bestMatchInstance = null;
            var             bestMatchScore    = 0;

            foreach (var instance in msbuildLocator.GetInstances())
            {
                var score = GetInstanceFeatureScore(instance);

                invalidVSFound = invalidVSFound || instance.IsInvalidVisualStudio();

                if (score > bestMatchScore ||
                    (score == bestMatchScore && instance.Version.Major > (bestMatchInstance?.Version.Major ?? 0)))
                {
                    bestMatchInstance = instance;
                    bestMatchScore    = score;
                }
            }

            return(bestMatchInstance);
        }
        public static IServiceProvider Create(
            ITestOutputHelper testOutput,
            IOmniSharpEnvironment environment,
            ILoggerFactory loggerFactory,
            IAssemblyLoader assemblyLoader,
            IMSBuildLocator msbuildLocator,
            IEnumerable <KeyValuePair <string, string> > configurationData = null,
            DotNetCliVersion dotNetCliVersion = DotNetCliVersion.Current,
            IEventEmitter eventEmitter        = null)
        {
            eventEmitter = eventEmitter ?? NullEventEmitter.Instance;

            var dotNetCliService = CreateDotNetCliService(dotNetCliVersion, loggerFactory, eventEmitter);
            var configuration    = CreateConfiguration(configurationData, dotNetCliService);
            var memoryCache      = CreateMemoryCache();
            var optionsMonitor   = CreateOptionsMonitor(configuration);
            var sharedTextWriter = CreateSharedTextWriter(testOutput);

            return(new TestServiceProvider(
                       environment, loggerFactory, assemblyLoader, memoryCache, sharedTextWriter,
                       msbuildLocator, eventEmitter, dotNetCliService, configuration, optionsMonitor));
        }
Example #17
0
        private TestServiceProvider(
            IOmniSharpEnvironment environment,
            ILoggerFactory loggerFactory,
            IAssemblyLoader assemblyLoader,
            IAnalyzerAssemblyLoader analyzerAssemblyLoader,
            ISharedTextWriter sharedTextWriter,
            IMSBuildLocator msbuildLocator,
            IEventEmitter eventEmitter,
            IDotNetCliService dotNetCliService,
            IConfigurationRoot configuration)
        {
            _logger = loggerFactory.CreateLogger <TestServiceProvider>();
            var services = _services = new ServiceCollection();

            services
            .AddLogging()
            .AddOptions()
            .AddMemoryCache();

            services
            .AddSingleton(environment)
            .AddSingleton(loggerFactory)
            .AddSingleton(assemblyLoader)
            .AddSingleton(sharedTextWriter)
            .AddSingleton(msbuildLocator)
            .AddSingleton(eventEmitter)
            .AddSingleton(dotNetCliService)
            .AddSingleton(configuration)
            .AddSingleton(configuration as IConfiguration)
            .Configure <OmniSharpOptions>(configuration)
            .PostConfigure <OmniSharpOptions>(OmniSharpOptions.PostConfigure)
            .AddSingleton(analyzerAssemblyLoader);

            services.TryAddSingleton(_ => new ManualFileSystemWatcher());
            services.TryAddSingleton <IFileSystemNotifier>(_ => _.GetRequiredService <ManualFileSystemWatcher>());
            services.TryAddSingleton <IFileSystemWatcher>(_ => _.GetRequiredService <ManualFileSystemWatcher>());

            _serviceProvider = services.BuildServiceProvider();
        }
Example #18
0
        public static void RegisterDefaultInstance(this IMSBuildLocator msbuildLocator, ILogger logger)
        {
            var bestInstanceFound = GetBestInstance(msbuildLocator, out var invalidVSFound);

            if (bestInstanceFound != null)
            {
                // Did we end up choosing the standalone MSBuild because there was an invalid Visual Studio?
                // If so, provide a helpful message to the user.
                if (invalidVSFound && bestInstanceFound.DiscoveryType == DiscoveryType.StandAlone)
                {
                    logger.LogWarning(
                        @"It looks like you have Visual Studio 2017 RTM installed.
 Try updating Visual Studio 2017 to the most recent release to enable better MSBuild support."
                        );
                }

                msbuildLocator.RegisterInstance(bestInstanceFound);
            }
            else
            {
                logger.LogError("Could not locate MSBuild instance to register with OmniSharp");
            }
        }
Example #19
0
        private static void RegisterMSBuildInstance(IMSBuildLocator msbuildLocator, ILogger logger)
        {
            MSBuildInstance instanceToRegister = null;
            var             invalidVSFound     = false;

            foreach (var instance in msbuildLocator.GetInstances())
            {
                if (instance.IsInvalidVisualStudio())
                {
                    invalidVSFound = true;
                }
                else
                {
                    instanceToRegister = instance;
                    break;
                }
            }


            if (instanceToRegister != null)
            {
                // Did we end up choosing the standalone MSBuild because there was an invalid Visual Studio?
                // If so, provide a helpful message to the user.
                if (invalidVSFound && instanceToRegister.DiscoveryType == DiscoveryType.StandAlone)
                {
                    logger.LogWarning(@"It looks like you have Visual Studio 2017 RTM installed.
Try updating Visual Studio 2017 to the most recent release to enable better MSBuild support.");
                }

                msbuildLocator.RegisterInstance(instanceToRegister);
            }
            else
            {
                logger.LogError("Could not locate MSBuild instance to register with OmniSharp");
            }
        }
Example #20
0
        public static void RegisterDefaultInstance(this IMSBuildLocator msbuildLocator, ILogger logger, DotNetInfo dotNetInfo = null)
        {
            var minimumMSBuildVersion = GetSdkMinimumMSBuildVersion(dotNetInfo, logger);

            logger.LogDebug($".NET SDK requires MSBuild instances version {minimumMSBuildVersion} or higher");

            var bestInstanceFound = GetBestInstance(msbuildLocator, minimumMSBuildVersion, logger, out var invalidVSFound, out var vsWithoutSdkResolver);

            if (bestInstanceFound != null)
            {
                // Did we end up choosing the standalone MSBuild because there was an invalid Visual Studio?
                // If so, provide a helpful message to the user.
                if (invalidVSFound && bestInstanceFound.DiscoveryType == DiscoveryType.StandAlone)
                {
                    logger.LogWarning(
                        $@"It looks like you have Visual Studio lower than VS 2019 {s_minimumMSBuildVersion} installed.
 Try updating Visual Studio to the most recent release to enable better MSBuild support."
                        );
                }

                if (vsWithoutSdkResolver && bestInstanceFound.DiscoveryType == DiscoveryType.StandAlone)
                {
                    logger.LogWarning(
                        @"It looks like you have Visual Studio 2019 installed without .NET Core SDK support which is required by OmniSharp.
 Try updating Visual Studio 2019 installation with .NET Core SDK to enable better MSBuild support."
                        );
                }

                if (bestInstanceFound.Version < minimumMSBuildVersion)
                {
                    if (bestInstanceFound.DiscoveryType == DiscoveryType.StandAlone)
                    {
                        logger.LogWarning(
                            $@"It looks like the included version of MSBuild is lower than {minimumMSBuildVersion} which is the minimum supported by the configured .NET Core Sdk.
 Try installing MSBuild version {minimumMSBuildVersion} or higher to enable better .NET Core Sdk support."
                            );
                    }
                    else if (bestInstanceFound.DiscoveryType == DiscoveryType.Mono)
                    {
                        logger.LogWarning(
                            $@"It looks like you have Mono installed which contains a MSBuild lower than {minimumMSBuildVersion} which is the minimum supported by the configured .NET Core Sdk.
 Try updating Mono to the latest stable or preview version to enable better .NET Core Sdk support."
                            );
                    }
                    else if (bestInstanceFound.DiscoveryType == DiscoveryType.VisualStudioSetup)
                    {
                        logger.LogWarning(
                            $@"It looks like you have Visual Studio 2019 installed which contains a MSBuild lower than {minimumMSBuildVersion} which is the minimum supported by the configured .NET Core Sdk.
 Try updating Visual Studio to version {minimumMSBuildVersion} or higher to enable better .NET Core Sdk support."
                            );
                    }
                    else if (bestInstanceFound.DiscoveryType == DiscoveryType.UserOverride)
                    {
                        logger.LogWarning(
                            $@"It looks like you have overridden the version of MSBuild with a version lower than {minimumMSBuildVersion} which is the minimum supported by the configured .NET Core Sdk.
 Try updating your MSBuild to version {minimumMSBuildVersion} or higher to enable better .NET Core Sdk support."
                            );
                    }
                }

                msbuildLocator.RegisterInstance(bestInstanceFound);
            }
            else
            {
                throw new MSBuildNotFoundException("Could not locate MSBuild instance to register with OmniSharp.");
            }
        }
Example #21
0
        public SetupComCommand(IMSBuildLocator locator) : base("setup-com", "Setup support for projects with COM references")
        {
            _locator = locator;

            Handler = CommandHandler.Create <bool>(ExecuteHandler);
        }
Example #22
0
        public CleanCommand(IMSBuildLocator locator) : base("clean", "Return to a clean state without support for additional project types")
        {
            _locator = locator;

            Handler = CommandHandler.Create <bool>(ExecuteHandler);
        }