Skip to content

Feature: File-Level Analysis Filtering for CI Optimization #365

@chief-nerd

Description

@chief-nerd

Problem Statement

When running custom_lint in CI/CD pipelines on pull requests, the tool currently analyzes entire packages even when only a few files have changed. This results in unnecessary analysis time and resource usage.

Example scenario:

  • A PR changes 28 files across 2 packages
  • Each package contains 125+ files
  • custom_lint analyzes all 250+ files instead of just the 28 changed files
  • Analysis takes ~2 minutes when it could potentially take 20-30 seconds

Proposed Solution

Add CLI support for analyzing specific files, similar to how dart analyze works:

# Analyze specific files only
dart run custom_lint lib/file1.dart lib/file2.dart test/file3.dart

# Or via stdin for large file lists
echo "lib/file1.dart\nlib/file2.dart" | dart run custom_lint --stdin-files

Use Case

This feature is particularly valuable for:

  1. CI/CD PR checks - Only lint changed files in pull requests
  2. Pre-commit hooks - Only lint staged files
  3. IDE integrations - Faster feedback on file save
  4. Incremental analysis - Large monorepos with frequent small changes

Current Workaround Limitations

The existing LintRule.filesToAnalyze property filters which files to analyze within a package, but:

  • ❌ Cannot limit analysis to specific file paths
  • ❌ Only supports glob patterns for file matching
  • ❌ Still requires analyzing the entire package structure
  • ❌ No way to pass explicit file list via CLI

Using dart analyze as a fallback:

  • ❌ Doesn't run custom lint rules
  • ❌ Loses the value of custom_lint entirely

Proposed API

CLI Option

dart run custom_lint --files lib/a.dart,lib/b.dart,test/c.dart

stdin Support (for large file lists)

dart run custom_lint --stdin-files < changed_files.txt

Environment Variable (current melos pattern)

CUSTOM_LINT_FILES="lib/a.dart,lib/b.dart" dart run custom_lint

Implementation Notes

This would likely require:

  1. Extending the CLI argument parser to accept file paths
  2. Modifying CustomLintWorkspace.fromPaths() to support explicit file filtering
  3. Ensuring analysis context still loads necessary package dependencies
  4. Maintaining compatibility with existing glob-based filtering

Benefits

  • Faster CI/CD - 60-90% reduction in analysis time for small PRs
  • 💰 Cost savings - Reduced compute time in cloud CI runners
  • Better DX - Faster feedback loops for developers
  • 🎯 Targeted analysis - Only analyze what changed

Related Issues

  • This would complement the existing --watch mode for hot-reload
  • Similar to how dart analyze and ESLint support file-specific analysis
  • Aligns with incremental analysis patterns in other linters

Environment

  • Tested on custom_lint version: 0.8.0
  • Flutter monorepo with Melos
  • Current workaround: Package-based filtering (sub-optimal) -- better than nothing

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions