phmonte / Buildalyzer

A utility to perform design-time builds of .NET projects without having to think too hard about it.
MIT License
605 stars 95 forks source link

cannot compile local references #193

Closed McP4nth3r closed 7 months ago

McP4nth3r commented 2 years ago

When i compile, with your tool. I cannot execute class library that includes local references from other folder. Is there a way to Include them in the builded dll file?

daveaglick commented 2 years ago

I'm not sure I'm totally following the problem you're having. Can you provide a little more information?

Thanks!

McP4nth3r commented 2 years ago

I try to compile a .csproj with a local reference to a other project but if I try to build the project with the function below, it returns this:

The type or namespace name 'Package2' does not exist in the namespace 'My' (are you missing an assembly reference?) The name 'Test' does not exist in the current context Compilation of resource test-resource wasn't successfully.

the function that I use:

        public static async Task<(bool, string)> CompileResource(string resourceName, string resourceProjPath)
        {
            //TODO: need solution path as well for supporting solutions

            var manager = new AnalyzerManager();
            var analyzer = manager.GetProject(resourceProjPath);
            analyzer.SetGlobalProperty("Configuration", "Release");

            var buildEnvironment = analyzer.EnvironmentFactory.GetBuildEnvironment().WithTargetsToBuild("publish");
            var analyzerResults = analyzer.Build(buildEnvironment);

            var workspace = analyzer.GetWorkspace();

            workspace.ClearSolution();
            foreach (var analyzerResult in analyzerResults)
            {
                analyzerResult.AddToWorkspace(workspace);
            }

            var solution = workspace.CurrentSolution;
            var dependencyGraph = solution.GetProjectDependencyGraph();
            var projectIds = dependencyGraph.GetTopologicallySortedProjects();
            var success = true;
            string projectAssemblyDllPath = null;
            foreach (var projectId in projectIds)
            {
                var proj = solution.GetProject(projectId);
                Console.WriteLine($"Compiling {proj.Name}");
                var c = await proj
                    .GetCompilationAsync();

                var result = c.WithOptions(proj.CompilationOptions).AddReferences(proj.MetadataReferences)
                    .Emit(proj.OutputFilePath);
                Console.WriteLine($"Compiled {proj.AssemblyName}");
                foreach (var diagnostic in result.Diagnostics)
                {
                    Console.WriteLine(diagnostic.GetMessage());
                }

                if (!result.Success)
                {
                    success = false;
                }
                else if (proj.Name.Equals(resourceName) || projectAssemblyDllPath == null)
                {
                    projectAssemblyDllPath = proj.OutputFilePath;
                }
            }

            return (success, projectAssemblyDllPath);
        }
McP4nth3r commented 2 years ago

any idea?

phmonte commented 7 months ago

Hello @McP4nth3r, did you manage to solve it? If not, can you upload an example of the project? It could be several factors, I can't help with the class alone.

McP4nth3r commented 7 months ago

Hello @McP4nth3r, did you manage to solve it? If not, can you upload an example of the project? It could be several factors, I can't help with the class alone.

To be quite frank, my memory of what I originally used Buildalyzer for is pretty hazy. I'm a bit in the fog as to whether this particular tool played a role in any of our projects, and if so, in which. Similarly, it's no longer clear to me whether we developed our own solution as a team or whether we ultimately switched to an alternative tool. This uncertainty probably reflects the dynamic nature of our project work. Nevertheless, I would like to express my appreciation for your efforts and the time you have invested. Your help is always much appreciated, even if the details sometimes take a back seat. Thanks for your effort :)