Esempio n. 1
0
        public void MissingDependenciesAreIgnored()
        {
            var testProvider = new TestDependencyProvider()
                .Package("a", "1.0", that => that.Needs("x", "1.0"));

            var walker = new DependencyWalker(new[] { testProvider });
            walker.Walk("a", new SemanticVersion("1.0"), VersionUtility.ParseFrameworkName("net45"));

            AssertDependencies(testProvider.Dependencies, that => that
                .Needs("a", "1.0"));
        }
Esempio n. 2
0
        public void NearestDependencyVersionWins()
        {
            var testProvider = new TestDependencyProvider()
                .Package("a", "1.0", that => that.Needs("b", "1.0").Needs("c", "1.0").Needs("x", "1.0"))
                .Package("b", "1.0", that => that.Needs("x", "2.0"))
                .Package("c", "1.0", that => that.Needs("x", "2.0"))
                .Package("x", "1.0")
                .Package("x", "2.0");

            var walker = new DependencyWalker(new[] { testProvider });
            walker.Walk("a", new SemanticVersion("1.0"), VersionUtility.ParseFrameworkName("net45"));

            AssertDependencies(testProvider.Dependencies, that => that
                .Needs("a", "1.0")
                .Needs("b", "1.0")
                .Needs("c", "1.0")
                .Needs("x", "1.0"));
        }
Esempio n. 3
0
        public ModuleLoaderContext(IServiceProvider serviceProvider,
                                       string projectDirectory)
        {
            ProjectDirectory = projectDirectory;
            RootDirectory = Microsoft.Dnx.Runtime.ProjectResolver.ResolveRootDirectory(ProjectDirectory);

            // A new project resolver is required. you cannot reuse the one from the
            // parent service provider as that will be for the parent context.
            ProjectResolver = new ProjectResolver(ProjectDirectory, RootDirectory);

            var referenceAssemblyDependencyResolver = new ReferenceAssemblyDependencyResolver(new FrameworkReferenceResolver());

            // Need to pass through package directory incase you download a package from the gallary, this needs to know about it
            var nuGetDependencyProvider = new NuGetDependencyResolver(new PackageRepository(
                NuGetDependencyResolver.ResolveRepositoryPath(RootDirectory)));
            var gacDependencyResolver = new GacDependencyResolver();
            var projectDepencyProvider = new ProjectReferenceDependencyProvider(ProjectResolver);
            var unresolvedDependencyProvider = new UnresolvedDependencyProvider();

            var projectLockJsonPath = Path.Combine(ProjectDirectory, LockFileFormat.LockFileName);
            if (File.Exists(projectLockJsonPath)) {
                var lockFileFormat = new LockFileFormat();
                var lockFile = lockFileFormat.Read(projectLockJsonPath);
                nuGetDependencyProvider.ApplyLockFile(lockFile);
            }

            DependencyWalker = new DependencyWalker(new IDependencyProvider[] {
                projectDepencyProvider,
                nuGetDependencyProvider,
                referenceAssemblyDependencyResolver,
                gacDependencyResolver,
                unresolvedDependencyProvider
            });

            LibraryExportProvider = new CompositeLibraryExportProvider(new ILibraryExportProvider[] {
                new ModuleProjectLibraryExportProvider(
                    ProjectResolver, serviceProvider),
                referenceAssemblyDependencyResolver,
                gacDependencyResolver,
                nuGetDependencyProvider
            });
        }
Esempio n. 4
0
        public static void Initialize(ApplicationHostContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            context.ProjectDirectory = context.Project?.ProjectDirectory ?? context.ProjectDirectory;
            context.RootDirectory    = context.RootDirectory ?? ProjectResolver.ResolveRootDirectory(context.ProjectDirectory);
            var projectResolver            = new ProjectResolver(context.ProjectDirectory, context.RootDirectory);
            var frameworkReferenceResolver = context.FrameworkResolver ?? new FrameworkReferenceResolver();

            context.PackagesDirectory = context.PackagesDirectory ?? PackageDependencyProvider.ResolveRepositoryPath(context.RootDirectory);

            var referenceAssemblyDependencyResolver = new ReferenceAssemblyDependencyResolver(frameworkReferenceResolver);
            var gacDependencyResolver        = new GacDependencyResolver();
            var projectDependencyProvider    = new ProjectReferenceDependencyProvider(projectResolver);
            var unresolvedDependencyProvider = new UnresolvedDependencyProvider();

            IList <IDependencyProvider> dependencyProviders = null;
            LockFileLookup lockFileLookup = null;

            if (context.Project == null)
            {
                Project project;
                if (Project.TryGetProject(context.ProjectDirectory, out project))
                {
                    context.Project = project;
                }
                else
                {
                    throw new InvalidOperationException($"Unable to resolve project from {context.ProjectDirectory}");
                }
            }

            var    projectLockJsonPath       = Path.Combine(context.ProjectDirectory, LockFileReader.LockFileName);
            var    lockFileExists            = File.Exists(projectLockJsonPath);
            var    validLockFile             = false;
            var    skipLockFileValidation    = context.SkipLockfileValidation;
            string lockFileValidationMessage = null;

            if (lockFileExists)
            {
                var lockFileReader = new LockFileReader();
                var lockFile       = lockFileReader.Read(projectLockJsonPath);
                validLockFile = lockFile.IsValidForProject(context.Project, out lockFileValidationMessage);

                // When the only invalid part of a lock file is version number,
                // we shouldn't skip lock file validation because we want to leave all dependencies unresolved, so that
                // VS can be aware of this version mismatch error and automatically do restore
                skipLockFileValidation = context.SkipLockfileValidation && (lockFile.Version == Constants.LockFileVersion);

                if (validLockFile || skipLockFileValidation)
                {
                    lockFileLookup = new LockFileLookup(lockFile);
                    var packageDependencyProvider = new PackageDependencyProvider(context.PackagesDirectory, lockFileLookup);

                    dependencyProviders = new IDependencyProvider[] {
                        projectDependencyProvider,
                        packageDependencyProvider,
                        referenceAssemblyDependencyResolver,
                        gacDependencyResolver,
                        unresolvedDependencyProvider
                    };
                }
            }

            if ((!validLockFile && !skipLockFileValidation) || !lockFileExists)
            {
                // We don't add the PackageDependencyProvider to DependencyWalker
                // It will leave all NuGet packages unresolved and give error message asking users to run "dnu restore"
                dependencyProviders = new IDependencyProvider[] {
                    projectDependencyProvider,
                    referenceAssemblyDependencyResolver,
                    gacDependencyResolver,
                    unresolvedDependencyProvider
                };
            }

            var libraries = DependencyWalker.Walk(dependencyProviders, context.Project.Name, context.Project.Version, context.TargetFramework);

            context.LibraryManager = new LibraryManager(context.Project.ProjectFilePath, context.TargetFramework, libraries);

            if (!validLockFile)
            {
                context.LibraryManager.AddGlobalDiagnostics(new DiagnosticMessage(
                                                                $"{lockFileValidationMessage}. Please run \"dnu restore\" to generate a new lock file.",
                                                                Path.Combine(context.Project.ProjectDirectory, LockFileReader.LockFileName),
                                                                DiagnosticMessageSeverity.Error));
            }

            if (!lockFileExists)
            {
                context.LibraryManager.AddGlobalDiagnostics(new DiagnosticMessage(
                                                                $"The expected lock file doesn't exist. Please run \"dnu restore\" to generate a new lock file.",
                                                                Path.Combine(context.Project.ProjectDirectory, LockFileReader.LockFileName),
                                                                DiagnosticMessageSeverity.Error));
            }

            // Clear all the temporary memory aggressively here if we don't care about reuse
            // e.g. runtime scenarios
            lockFileLookup?.Clear();
            projectResolver.Clear();
        }
Esempio n. 5
0
        public void RecursiveDependenciesAreNotFollowed()
        {
            var testProvider = new TestDependencyProvider()
                .Package("a", "1.0", that => that.Needs("b", "1.0"))
                .Package("b", "1.0", that => that.Needs("c", "1.0"))
                .Package("c", "1.0", that => that.Needs("d", "1.0").Needs("b", "1.0"))
                .Package("d", "1.0", that => that.Needs("b", "1.0"));

            var walker = new DependencyWalker(new[] { testProvider });

            Assert.Throws<InvalidOperationException>(() =>
            {
                walker.Walk("a", new SemanticVersion("1.0"), VersionUtility.ParseFrameworkName("net45"));
            });
        }
Esempio n. 6
0
        public void RejectedDependenciesToNotCarryConstraints()
        {
            // a1->b1-*d1->e2->x2
            // a1->c1->d2
            // a1->c1->e1->x1
            // * b1->d1 lower than c1->d2 so d1->e2->x2 are n/a

            var testProvider = new TestDependencyProvider()
                .Package("a", "1.0", that => that.Needs("b", "1.0").Needs("c", "1.0"))
                .Package("b", "1.0", that => that.Needs("d", "1.0"))
                .Package("c", "1.0", that => that.Needs("d", "2.0").Needs("e", "1.0"))
                .Package("d", "1.0", that => that.Needs("e", "2.0"))
                .Package("d", "2.0")
                .Package("e", "1.0", that => that.Needs("x", "1.0"))
                .Package("e", "2.0", that => that.Needs("x", "2.0"))
                .Package("x", "1.0")
                .Package("x", "2.0")
                .Package("g", version: null);

            var walker = new DependencyWalker(new[] { testProvider });
            walker.Walk("a", new SemanticVersion("1.0"), VersionUtility.ParseFrameworkName("net45"));

            // the d1->e2->x2 line has no effect because d2 has no dependencies,

            AssertDependencies(testProvider.Dependencies, that => that
                .Needs("a", "1.0")
                .Needs("b", "1.0")
                .Needs("c", "1.0")
                .Needs("d", "2.0")
                .Needs("e", "1.0")
                .Needs("x", "1.0"));
        }
Esempio n. 7
0
        public ApplicationHostContext(IServiceProvider hostServices,
                                      string projectDirectory,
                                      string packagesDirectory,
                                      string configuration,
                                      FrameworkName targetFramework,
                                      IAssemblyLoadContextFactory loadContextFactory = null,
                                      bool skipLockFileValidation = false)
        {
            ProjectDirectory           = projectDirectory;
            Configuration              = configuration;
            RootDirectory              = Runtime.ProjectResolver.ResolveRootDirectory(ProjectDirectory);
            ProjectResolver            = new ProjectResolver(ProjectDirectory, RootDirectory);
            FrameworkReferenceResolver = new FrameworkReferenceResolver();
            ProjectGraphProvider       = new ProjectGraphProvider(hostServices);
            _serviceProvider           = new ServiceProvider(hostServices);

            PackagesDirectory = packagesDirectory ?? NuGetDependencyResolver.ResolveRepositoryPath(RootDirectory);

            var referenceAssemblyDependencyResolver = new ReferenceAssemblyDependencyResolver(FrameworkReferenceResolver);

            NuGetDependencyProvider = new NuGetDependencyResolver(new PackageRepository(PackagesDirectory));
            var gacDependencyResolver = new GacDependencyResolver();

            ProjectDependencyProvider = new ProjectReferenceDependencyProvider(ProjectResolver);
            var unresolvedDependencyProvider = new UnresolvedDependencyProvider();

            var projectName = PathUtility.GetDirectoryName(ProjectDirectory);

            Project project;

            if (ProjectResolver.TryResolveProject(projectName, out project))
            {
                Project = project;
            }
            else
            {
                throw new InvalidOperationException(
                          string.Format("Unable to resolve project '{0}' from {1}", projectName, ProjectDirectory));
            }

            var projectLockJsonPath = Path.Combine(ProjectDirectory, LockFileReader.LockFileName);
            var lockFileExists      = File.Exists(projectLockJsonPath);
            var validLockFile       = false;

            if (lockFileExists)
            {
                var lockFileReader = new LockFileReader();
                _lockFile     = lockFileReader.Read(projectLockJsonPath);
                validLockFile = _lockFile.IsValidForProject(project);

                // When the only invalid part of a lock file is version number,
                // we shouldn't skip lock file validation because we want to leave all dependencies unresolved, so that
                // VS can be aware of this version mismatch error and automatically do restore
                skipLockFileValidation = skipLockFileValidation && (_lockFile.Version == Constants.LockFileVersion);

                if (validLockFile || skipLockFileValidation)
                {
                    NuGetDependencyProvider.ApplyLockFile(_lockFile);

                    DependencyWalker = new DependencyWalker(new IDependencyProvider[] {
                        ProjectDependencyProvider,
                        NuGetDependencyProvider,
                        referenceAssemblyDependencyResolver,
                        gacDependencyResolver,
                        unresolvedDependencyProvider
                    });
                }
            }

            if ((!validLockFile && !skipLockFileValidation) || !lockFileExists)
            {
                // We don't add NuGetDependencyProvider to DependencyWalker
                // It will leave all NuGet packages unresolved and give error message asking users to run "dnu restore"
                DependencyWalker = new DependencyWalker(new IDependencyProvider[] {
                    ProjectDependencyProvider,
                    referenceAssemblyDependencyResolver,
                    gacDependencyResolver,
                    unresolvedDependencyProvider
                });
            }

            LibraryManager = new LibraryManager(() => DependencyWalker.Libraries);

            AssemblyLoadContextFactory = loadContextFactory ?? new RuntimeLoadContextFactory(ServiceProvider);

            // Create a new Application Environment for running the app. It needs a reference to the Host's application environment
            // (if any), which we can get from the service provider we were given.
            // If this is null (i.e. there is no Host Application Environment), that's OK, the Application Environment we are creating
            // will just have it's own independent set of global data.
            IApplicationEnvironment hostEnvironment = null;

            if (hostServices != null)
            {
                hostEnvironment = (IApplicationEnvironment)hostServices.GetService(typeof(IApplicationEnvironment));
            }
            ApplicationEnvironment = new ApplicationEnvironment(Project, targetFramework, configuration, hostEnvironment);

            // Default services
            _serviceProvider.Add(typeof(IApplicationEnvironment), ApplicationEnvironment);
            _serviceProvider.Add(typeof(ILibraryManager), LibraryManager);
            _serviceProvider.Add(typeof(ICompilerOptionsProvider), new CompilerOptionsProvider(ProjectResolver));

            // Not exposed to the application layer
            _serviceProvider.Add(typeof(IProjectResolver), ProjectResolver, includeInManifest: false);
            _serviceProvider.Add(typeof(NuGetDependencyResolver), NuGetDependencyProvider, includeInManifest: false);
            _serviceProvider.Add(typeof(ProjectReferenceDependencyProvider), ProjectDependencyProvider, includeInManifest: false);
            _serviceProvider.Add(typeof(IAssemblyLoadContextFactory), AssemblyLoadContextFactory, includeInManifest: false);
        }
Esempio n. 8
0
 public IEnumerable <DiagnosticMessage> GetAllDiagnostics()
 {
     return(GetLockFileDiagnostics()
            .Concat(DependencyWalker.GetDependencyDiagnostics(Project.ProjectFilePath)));
 }