msbuild icon indicating copy to clipboard operation
msbuild copied to clipboard

Projects compiled over BuildManager dont have roslyn analyzers executed

Open payn-git opened this issue 1 year ago • 4 comments

Issue Description

When compiling project over BuildManager.DefaultBuildManager.Build, configured roslyn analyzers are not executed and build will not report any issues. When exactly same project is compiled over command line msbuild analyzers are executed correctly and warnings\errors are reported.

Attaching bin logs from project compilation from command line and from code over BuildManager for comparison. Also code which is compiling projects is attached.

Expected Behavior

Roslyn analyzers are executed during compilation.

Actual Behavior

Roslyn analyzers are not executed and build will not show any warnings\errors.

Ask us questions

Why there is difference in compilation on command line vs BuildManager. SolutionBuilder.zip

payn-git avatar Aug 10 '22 22:08 payn-git

@jaredpar is this expected?

benvillalobos avatar Aug 18 '22 16:08 benvillalobos

I'm not familiar with BuildManager.DefaultBuildManager.Build or what the implications of it are. Do we have it documented anywhere?

@mavasani

jaredpar avatar Aug 19 '22 21:08 jaredpar

I'm not familiar with BuildManager.DefaultBuildManager.Build or what the implications of it are.

It's an MSBuild concept happening above the Roslyn compiler layer. All builds run through a BuildManager.Build call (with varying indirection) and DefaultBuildManager is the default. Roslyn is not at fault here.

@payn-git There are a few problems with your approach:

            var buildRequest = new BuildRequestData(projectPath, myGlobalProperties, null, new[] { "Restore", "ReBuild" }, null);

The result of evaluating the project from before restore is being cached and reused when running Rebuild. That state may miss information about analyzers (or references or other things). You must run the Restore and Rebuild operations separately, so that the second evaluation can pull in updated information from the restore. I see that you were doing this in the "command line" approach as well, which is broken too. There you should specify -restore -t:Rebuild instead of -t:Restore;Rebuild.

More critically to your immediate problem is this from the log:

NoImport: $(NuGetTargets) at (16;3) false condition; (Exists('$(NuGetTargets)') and '$(SkipImportNuGetBuildTargets)' != 'true' was evaluated as Exists('C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\NuGet\17.0\Microsoft.NuGet.targets') and '' != 'true').

There's an inconsistency between C:\Program Files (x86)\Microsoft Visual Studio\2019 and the later 17.0, which appears to be coming in through an environment variable. You may wish to clear that if it's critical to be able to run your builds in a VS 2019 context from a VS 2022 developer command prompt.

rainersigwald avatar Aug 22 '22 16:08 rainersigwald

@payn-git did @rainersigwald 's comment help?

benvillalobos avatar Sep 15 '22 17:09 benvillalobos

This issue is marked as stale because feedback has been requested for 30 days with no response. Please respond within 14 days or this issue will be closed due to inactivity.

ghost avatar Nov 27 '22 00:11 ghost