Buildalyzer icon indicating copy to clipboard operation
Buildalyzer copied to clipboard

cannot compile local references

Open McP4nth3r opened this issue 3 years ago • 3 comments
trafficstars

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?

McP4nth3r avatar Jan 12 '22 23:01 McP4nth3r

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

  • How are you calling Buildalyzer (code samples, etc.)?
  • What is your expectation in terms of results and output?
  • What's actually happening that's different than your expectation?

Thanks!

daveaglick avatar Jan 13 '22 00:01 daveaglick

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 avatar Jan 13 '22 20:01 McP4nth3r

any idea?

McP4nth3r avatar Jan 23 '22 01:01 McP4nth3r

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.

phmonte avatar Feb 18 '24 23:02 phmonte

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 :)

McP4nth3r avatar Feb 19 '24 05:02 McP4nth3r