Skip to content

Bound --script checks, report scripts without check results, add --define - #361

Merged
TheAngryByrd merged 4 commits into
mainfrom
scripts-perf
Nov 16, 2026
Merged

TheAngryByrd merged 4 commits into
mainfrom
scripts-perf

Conversation

@TheAngryByrd

@TheAngryByrd TheAngryByrd commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

Problem

fsharp-analyzers --script is used to type check and analyze many scripts in one run. In a repository with about 280 scripts that each #load a large shared file (about 330 assembly references), three things went wrong:

  1. Memory. The CLI started every script check at once, and the checker kept the type check state (the incremental builder) of each script until the run ended. Memory grew by about 0.4 GB per script. 40 scripts used 15 GB, and 60 scripts went over 16 GB. All scripts would need about 40 GB.
  2. Missed type errors. Some scripts with a type error showed as type checked. --fail-on-type-errors then passed a run that it must fail.
  3. Script options. --script had no way to set conditional compilation symbols. A --script path or glob that matched no file was dropped silently, so an unknown flag after --script (for example --define FOO) gave a clean run that checked less than expected.

Root cause of the missed type errors

The scripts that missed errors all use FSharp.Data type providers. When the CLI checks several of these scripts at the same time, the type provider setup can fail. The F# compiler service (FCS) then returns no check results and no diagnostics for that script, and the CLI took that as a clean result.

flowchart TD
    A[Several scripts set up FSharp.Data type providers at the same time] --> B[Type provider design-time code fails<br/>TypeProviderError and FS3033 during setup]
    B --> C[FCS ImportTypeProviderExtensions raises ReportedError]
    C --> D[CombineImportedAssembliesTask re-raises it<br/>its own logger holds the errors]
    D --> E[TryCreateIncrementalBuilderForProjectOptions<br/>swallows ReportedError, returns no builder]
    E --> F[ParseAndCheckProject: HasCriticalErrors = true,<br/>Diagnostics = empty]
    F --> G[CLI before this PR: no errors, so type checked]
Loading

Evidence, from a copy of those scripts with FCS 43.12.402:

  • Checking the 9 type-provider scripts one at a time gave the expected error in 8 of 8 runs. Checking them 9 at a time gave HasCriticalErrors = true with zero diagnostics for 4 to 7 of the 9 scripts in each run.
  • One FSharpChecker per script still fails, so the shared state is process-wide (type provider design-time code), not the CLI or the checker caches.
  • The failures come from type provider design-time code. At concurrency 9 only, the provided type setup raised TypeProviderError (a FSharp.Core constructor "not found" in a target type) and FS3033, and the FSharp.Data WorldBank schema cache file was locked by another reader. I did not identify the exact defect in that code.
  • FCS loses the diagnostics in IncrementalBuild.fs: CombineImportedAssembliesTask catches the ReportedError and calls diagnosticsLogger.Warning exn, which re-raises it, and TryCreateIncrementalBuilderForProjectOptions ignores ReportedError in errorRecoveryNoRange.

The CLI part of the fault is that it never read HasCriticalErrors. This PR fixes that part. The type provider race and the lost FCS diagnostics are upstream (FSharp.Data / ProvidedTypes SDK, and dotnet/fsharp).

Changes

  • Bounded script checks (Program.fs). The CLI checks at most --max-concurrent-scripts scripts at the same time (default 4). Scripts and projects run under one Async.Parallel, so an exception in either stops the run at once, as before. After the analysis of a script, it removes that script's builder from the checker cache and starts a background gen 2 collection. No project references a script, so FCS does not use that builder again. --project inputs keep their old behavior.
  • No check results is a type check failure (Analysis.fs). When FCS returns HasCriticalErrors and no error, runProject reports a type check failure with the message "The F# compiler service returned no check results and no error for this project, so its code was not type checked." It runs no analyzer on such a project, because the file results are empty and the project results throw on use. This applies to --project, --script and --fsc-args inputs.
  • --define (Arguments.fs, ProjectLoading.fs). Defines conditional compilation symbols for --script files, like dotnet fsi --define. Each value must be one symbol by the F# preprocessor rules, or the CLI exits with code 20 (so --define "A;B" is an error, not a silent no-op). It does not apply to --project, which gets its symbols from DefineConstants. The CLI warns when --define is given without a script.
  • Unmatched --script values are an error (ProjectLoading.fs, Program.fs). Each path or glob must match at least one file, or the CLI exits with code 20 and names each unmatched value. A script that several values match is checked once.
  • Docs (CLI.md) and CHANGELOG.md.

Measurement

Peak = working set of the CLI process tree, sampled each second. Windows 11, 24 logical processors, 64 GB, other work running. Scripts: the first N of the scripts that load the shared file, in a fixed order.

Scripts 0.41.1 time 0.41.1 peak This PR time This PR peak
10 22.1 s 4.43 GB 22.8 s 3.47 GB
20 35.0 to 36.0 s (4 runs) 8.52 to 8.90 GB 34.4 to 36.6 s (4 runs) 4.38 to 4.74 GB
40 55.3 s, 38.2 s 15.15 GB, 14.07 GB 52.3 s (2 runs), 36.3 s, 34.4 s 4.50 to 4.80 GB
60 stopped at 16.4 GB after 42 s more than 16 GB 71.9 s 5.27 GB
239 to 284 not run (about 40 GB expected) 244.9 s, 162.5 s 6.00 GB, 5.63 GB

Concurrency sweep at 60 scripts: 2 = 81.4 s / 3.53 GB, 4 = 72.4 s / 4.82 GB, 8 = 70.4 s / 9.16 GB, 12 = 74.2 s / 13.06 GB. Time stays flat above 4, so 4 is the default.

Analyzer output is the same: all analyzers on 10 scripts give the same 647 message lines with both builds, and the SDK samples (--project, and --project with --script) give the same output and exit codes.

Missed type errors, 9 type-provider scripts, 3 runs each ("missed" = shown as type checked although each script has a type error):

Build Scripts at the same time Missed Reported "returned no check results"
0.41.1 all 5, 7, 4 of 9 not detected
This PR 9 0, 0, 0 3, 5, 7
This PR 4 (default) 0, 0, 0 3, 2, 2
This PR 1 0 0

With the default of 4, the race still happens for scripts that use type providers. The CLI now reports those scripts as not type checked instead of clean. --max-concurrent-scripts 1 avoids the race.

Tests

Each new test fails on 582617f (v0.41.1). The critical-results test fails on the old Analysis.fs. The other tests use the new API, and a mutant of each changed rule makes its test fail.

  • a script that FCS returns no check results for counts as a type check failure and no analyzer runs on a script that FCS returns no check results for: compile a type provider whose GetNamespaces throws. That gives FCS's empty result without a race.
  • code under #if is type checked when its symbol is defined and ... is not type checked when its symbol is not defined.
  • scripts are analyzed at most the given number at the same time, and each one is analyzed: six fake scripts under a limit of 2. Each analysis waits on a signal until 2 are active. Without the bound, 6 are active.
  • script values that match no file are reported, script values that each match a file resolve to their full paths, and a script that several values match resolves once.
  • CliProcessTests run the built CLI and check the exit codes for an unmatched --script value, --max-concurrent-scripts 0, a --define that is not one symbol, and a defined and an undefined symbol.
  • a define after the scripts is not taken as a script, the accepted and rejected --define values, and the --max-concurrent-scripts default, value, and rejection of 0 and -1.

How to test

dotnet test tests/FSharp.Analyzers.Cli.Tests
# --define
dotnet run --project src/FSharp.Analyzers.Cli -- --script ./samples/BadOptionUsage.fsx --define FOO --analyzers-path ./artifacts/bin/OptionAnalyzer/release
# an unmatched script exits with 20
dotnet run --project src/FSharp.Analyzers.Cli -- --script ./samples/DoesNotExist.fsx --analyzers-path ./artifacts/bin/OptionAnalyzer/release

What this PR does not change

  • Script loading is serial. GetProjectOptionsFromScript runs one script at a time (a comment says it is not thread-safe). At 284 scripts, this is about 63 s of 245 s. Starting each check as its options arrive would hide most of it.
  • Analyzers run on every file of a script's #load closure. A shared loaded file is analyzed once per script that loads it, so its messages repeat.
  • The type provider race and the lost FCS diagnostics. They are upstream. I can open an issue on dotnet/fsharp for the lost diagnostics (with the throwing-provider repro), and one on FSharp.Data for the concurrent setup failures.
  • No fsi --lib equivalent. A bare #r "X.dll" resolves only against the #I folders in the scripts. Passing --lib:<dir> in the same script flags that --define uses makes it resolve (checked with FCS), so it is cheap to add later.
  • Checker settings and --project inputs are unchanged.

I can split this PR into (1) bounded script checks, (2) the no-results failure, and (3) --define and unmatched --script values.

🤖 Generated with Claude Code

TheAngryByrd and others added 3 commits November 15, 2026 16:06
With many --script inputs, the CLI started every script check at once and
the checker kept each script's incremental builder in its project cache
(size 200) until the run ended. Memory grew by about 0.4 GB per script.

Check at most 4 scripts at a time, clear each script's builder from the
checker cache after its analysis, and start a background gen 2 collection
so the released builders do not pile up in gen 2. Project inputs keep
their old behavior, because other projects can reference them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When a type provider fails while FCS sets it up, FCS returns no check
results and no diagnostic for the project (HasCriticalErrors). Several
scripts that set up FSharp.Data providers at the same time hit this.
runProject did not read HasCriticalErrors, so such a script counted as
type checked. It now reports a type check failure and runs no analyzer
on it, because the file results are empty and the project results throw.

Scripts:
- --max-concurrent-scripts sets the bound of script checks (default 4,
  1 or more). Scripts and projects run under one Async.Parallel, so an
  exception in either stops the run at once.
- --define passes conditional compilation symbols to the scripts. A
  value that is not one symbol exits with code 20.
- A --script path or glob that matches no file exits with code 20, and
  a script that several values match is checked once.

Tests run the built CLI for the exit codes, and a type provider that
throws in GetNamespaces gives FCS's empty result without a race.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The check allowed only letters, digits and underscores. The F# preprocessor
lexer also accepts an apostrophe, connector punctuation, combining marks,
format characters and letter numbers, so `--define Baz'` was rejected
although `dotnet fsi --define:Baz'` defines it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TheAngryByrd
TheAngryByrd marked this pull request as ready for review November 16, 2026 01:52
@TheAngryByrd
TheAngryByrd requested review from nojaf and a balanced review from Copilot November 16, 2026 01:52

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The tests do not verify that a positive parallelism limit bounds concurrent script analyses while allowing every script to complete, leaving the memory-related regression unprotected.

1 open issue
What changed in this PR

This PR improves the analyzer CLI’s reliability and memory usage when processing many scripts, while adding conditional compilation support and stricter input validation.

Changes:

  • Limit concurrent script checks and release each script’s cached check state after analysis.
  • Treat missing compiler check results as a type-check failure and skip analyzers for those inputs.
  • Add --define, reject unmatched script arguments, and expand tests and documentation.
File Description
tests/​FSharp.Analyzers.Cli.Tests/​ScriptLoadingTests.fs Test conditional symbols, unmatched paths, and script deduplication.
tests/​FSharp.Analyzers.Cli.Tests/​FSharp.Analyzers.Cli.Tests.fsproj Include the new CLI process tests.
tests/​FSharp.Analyzers.Cli.Tests/​CliProcessTests.fs Verify argument validation and conditional compilation through CLI exit codes.
tests/​FSharp.Analyzers.Cli.Tests/​ArgumentsTests.fs Test symbol validation and concurrency-limit parsing.
tests/​FSharp.Analyzers.Cli.Tests/​AnalysisTests.fs Test missing check results and analyzer suppression.
src/​FSharp.Analyzers.Cli/​ProjectLoading.fs Resolve unique script paths, report unmatched inputs, and pass defines to FCS.
src/​FSharp.Analyzers.Cli/​Program.fs Validate inputs, bound script concurrency, and clear script caches.
src/​FSharp.Analyzers.Cli/​Arguments.fs Declare and validate the new CLI options.
src/​FSharp.Analyzers.Cli/​Analysis.fs Report critical check failures and skip unavailable results.
docs/​content/​getting-started/​CLI.md Document script options, limits, and failure handling.
CHANGELOG.md Summarize the new options and reliability improvements.

Review effort: Balanced

Comment thread src/FSharp.Analyzers.Cli/Program.fs Resolved
Removing maxDegreeOfParallelism left every test passing. The script loop
moves to Analysis.runScripts, and a test runs six scripts under a limit
of 2. Each analysis waits on a signal until 2 are active, so the test
sees 6 active analyses when the bound is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

There are no active review issues blocking the CLI script-analysis improvements.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@nojaf nojaf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good! Ship it!

@TheAngryByrd
TheAngryByrd merged commit dc51b29 into main Nov 16, 2026
3 checks passed
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.

3 participants