WiseTechGlobal / WTG.Analyzers

Analyzers from WiseTech Global to enforce our styles, behaviours, and prevent common mistakes.
Other
15 stars 3 forks source link

Add analyzer to forbid calling MSBuildLocator.RegisterDefault() #186

Closed yaakov-h closed 1 year ago

yaakov-h commented 1 year ago

In a similar vein to #128, and regarding recent issues we've had with updated internal tools and the subsequent discussion at https://github.com/microsoft/MSBuildLocator/pull/184, we should forbid calling Microsoft.Build.Locator.MSBuildLocator.RegisterDefault().

I don't want to use the regular BannedApiAnalyzer for this as it requires somebody to go and add the banned API to BannedAPIs.txt in the repo. Lately, internal tools are being built in standalone repos all over the show, so it's not a simple case of adding this to the root of any of our existing product repositories.

I don't think a code fix is strictly necessary here, but perhaps we could provide one to either use the internal helper from DevTools (I think we publish that via internal NuGet), or to transform the invocation into a full LINQ expression along the lines of (pseudocode-ish):

MSBuildLocator.RegisterInstance(
    MSBuildLocator.QueryInstances()
    .OrderByDescending(i => i.Version)
    .FirstOrDefault() ?? throw new Exception("there weren't any"));