Repository navigation
Bound --script checks, report scripts without check results, add --define - #361
Merged
Merged
Conversation
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>
Contributor
There was a problem hiding this comment.
🟡 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
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>
nojaf
approved these changes
Nov 16, 2026
nojaf
left a comment
Contributor
There was a problem hiding this comment.
Thanks, looks good! Ship it!
This was referenced Nov 16, 2026
This was referenced Nov 16, 2026
This was referenced Nov 23, 2026
This was referenced Nov 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Problem
fsharp-analyzers --scriptis used to type check and analyze many scripts in one run. In a repository with about 280 scripts that each#loada large shared file (about 330 assembly references), three things went wrong:--fail-on-type-errorsthen passed a run that it must fail.--scripthad no way to set conditional compilation symbols. A--scriptpath 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]Evidence, from a copy of those scripts with FCS 43.12.402:
HasCriticalErrors = truewith zero diagnostics for 4 to 7 of the 9 scripts in each run.FSharpCheckerper script still fails, so the shared state is process-wide (type provider design-time code), not the CLI or the checker caches.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.IncrementalBuild.fs:CombineImportedAssembliesTaskcatches theReportedErrorand callsdiagnosticsLogger.Warning exn, which re-raises it, andTryCreateIncrementalBuilderForProjectOptionsignoresReportedErrorinerrorRecoveryNoRange.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
Program.fs). The CLI checks at most--max-concurrent-scriptsscripts at the same time (default 4). Scripts and projects run under oneAsync.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.--projectinputs keep their old behavior.Analysis.fs). When FCS returnsHasCriticalErrorsand no error,runProjectreports 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,--scriptand--fsc-argsinputs.--define(Arguments.fs,ProjectLoading.fs). Defines conditional compilation symbols for--scriptfiles, likedotnet fsi --define. Each value must be one symbol by the F# preprocessor rules, or the CLI exits with code20(so--define "A;B"is an error, not a silent no-op). It does not apply to--project, which gets its symbols fromDefineConstants. The CLI warns when--defineis given without a script.--scriptvalues are an error (ProjectLoading.fs,Program.fs). Each path or glob must match at least one file, or the CLI exits with code20and names each unmatched value. A script that several values match is checked once.CLI.md) andCHANGELOG.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.
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--projectwith--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):
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 1avoids the race.Tests
Each new test fails on
582617f(v0.41.1). The critical-results test fails on the oldAnalysis.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 failureandno analyzer runs on a script that FCS returns no check results for: compile a type provider whoseGetNamespacesthrows. That gives FCS's empty result without a race.code under #if is type checked when its symbol is definedand... 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, anda script that several values match resolves once.CliProcessTestsrun the built CLI and check the exit codes for an unmatched--scriptvalue,--max-concurrent-scripts 0, a--definethat 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--definevalues, and the--max-concurrent-scriptsdefault, value, and rejection of 0 and -1.How to test
What this PR does not change
GetProjectOptionsFromScriptruns 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.#loadclosure. A shared loaded file is analyzed once per script that loads it, so its messages repeat.fsi --libequivalent. A bare#r "X.dll"resolves only against the#Ifolders in the scripts. Passing--lib:<dir>in the same script flags that--defineuses makes it resolve (checked with FCS), so it is cheap to add later.--projectinputs are unchanged.I can split this PR into (1) bounded script checks, (2) the no-results failure, and (3)
--defineand unmatched--scriptvalues.🤖 Generated with Claude Code