Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 3, 2025

Implements a code linter for Business packages to detect performance anti-patterns. Initial rule flags Load() calls inside foreach loops, which cause N+1 query issues.

Architecture

Roslyn-based linter following existing ValidateStartAndEndMethodsCommand pattern:

  • Command: cmf build business lint <solution-path> [files...]
  • Rule system: Factory pattern with DI for extensibility
  • Detection: AST analysis finds Load() invocations within ForEachStatementSyntax

Implementation

BusinessLinter/
├── Rules/NoLoadInForeachRule.cs    # Detects Load() in foreach
├── SolutionLinter.cs                # Orchestrates Roslyn analysis
├── RuleFactory.cs                   # Creates enabled rule instances
└── Abstractions/                    # ILintRule, ILintLogger, IRuleFactory

NoLoadInForeach catches:

foreach (DataRow row in rows) {
    material.Load();            // ⚠️ Flagged
    material.Facility.Load();   // ⚠️ Flagged (chained)
}

Outputs:

Warning: [NoLoadInForeach] file.cs:23 - Load() method should not be called inside foreach loops. 
Found in class 'MaterialOrchestration', method 'GetMaterialsWithLoadInLoop'.

Extensibility

New rules require:

  1. Inherit BaseLintRule
  2. Implement Analyze(MethodDeclarationSyntax, ...)
  3. Register in ServiceCollectionExtensions

Rule enable/disable via IsEnabled property. Configuration schema provided for future file-based config.

Testing

6 unit tests cover: direct calls, chained calls, multiple violations, nested loops, and valid patterns outside loops.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • 0t3vsblobprodcus362.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • 1javsblobprodcus364.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • 2zrvsblobprodcus388.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • 37bvsblobprodcus311.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • 4m6vsblobprodcus384.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • 4zjvsblobprodcus390.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • 51yvsblobprodcus36.vsblob.vsassets.io
    • Triggering command: dotnet restore --no-cache (dns block)
  • 5dkvsblobprodcus355.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • h6tvsblobprodcus346.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • jd4vsblobprodcus366.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • kh4vsblobprodcus325.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • kxqvsblobprodcus376.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • l49vsblobprodcus358.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • mfjvsblobprodcus373.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
  • p2ovsblobprodcus312.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • s8mvsblobprodcus38.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • se1vsblobprodcus349.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • uy6vsblobprodcus34.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)
  • x3yvsblobprodcus370.vsblob.vsassets.io
    • Triggering command: dotnet build (dns block)
    • Triggering command: dotnet restore --no-cache (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>Implement Code Linter for Business Packages</issue_title>
<issue_description>We should add a code linter for all package types. The first stage of implementation should focus on Business packages, with an initial rule aimed at preventing inefficient or problematic code patterns.

Initial Rule Proposal:

Rule: Do not allow Load() method calls inside a foreach loop.

The foreach loop is a common place where performance bottlenecks can occur if certain methods (like Load()) are called repetitively for each iteration.

Example of problematic code:

foreach (DataRow dataRow in results.Tables[0].Rows)
{
    IMaterial material = _entityFactory.Create<IMaterial>();
    material.Name = (string)dataRow["Name"];
    material.Load();            // Bad practice: Load() inside foreach
    material.Facility.Load();  // Bad practice: Load() inside foreach
    materials.Add(material);
}

This is inefficient because the Load() method is called for every single material inside the loop, which could result in unnecessary resource usage or performance issues. Ideally, these methods should be called outside the loop using Collection Loads.

Goal:

To implement a linter rule that flags any Business package file where the Load() method is called inside a foreach loop in Business packages.

Future Considerations:

This rule should be configurable, so additional rules can be added in the future.

The linter implementation should allow for rule customization, possibly through configuration files or a central rules engine.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 5 commits November 3, 2025 10:40
Co-authored-by: joaoopereira <3718704+joaoopereira@users.noreply.github.com>
Co-authored-by: joaoopereira <3718704+joaoopereira@users.noreply.github.com>
Co-authored-by: joaoopereira <3718704+joaoopereira@users.noreply.github.com>
Co-authored-by: joaoopereira <3718704+joaoopereira@users.noreply.github.com>
…andling

Co-authored-by: joaoopereira <3718704+joaoopereira@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement Code Linter for Business Packages Add Business package linter with NoLoadInForeach rule Nov 3, 2025
Copilot AI requested a review from joaoopereira November 3, 2025 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants