You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Some F# projects cannot take a binary dependency on FsToolkit.ErrorHandling. They copy the source files by hand, and the copy goes out of date. This PR adds a second package, FsToolkit.ErrorHandling.Sources. It delivers the source of src/FsToolkit.ErrorHandling as files that compile inside the consumer's project. The consumer picks the namespace and the visibility:
<PackageReferenceInclude="FsToolkit.ErrorHandling.Sources"Version="x.y.z"PrivateAssets="all" />
<FsToolkitErrorHandlingNamespace>Contoso.Internal.ErrorHandling</FsToolkitErrorHandlingNamespace> <!-- default: FsToolkit.ErrorHandling -->
<FsToolkitErrorHandlingVisibility>internal</FsToolkitErrorHandlingVisibility> <!-- default: internal, or public -->
Summary
flowchart LR
A["src/FsToolkit.ErrorHandling<br/>.fsproj + 55 .fs files"] -->|GenerateSources<br/>build/SourcesPackage.fs| B["obj/generated/<br/>001_Nullness.fs.pp ... 055_Seq.fs.pp"]
B -->|DotnetPack| C["FsToolkit.ErrorHandling.Sources.nupkg<br/>contentFiles/fs/any + build/*.targets"]
C -->|SourcesTest| D["Sources.Tests: full test suite, public"]
C -->|SourcesTest| E["Sources.Consumer: strict, internal,<br/>custom namespace, build + rebuild"]
Loading
The generator (build/SourcesPackage.fs) reads the Compile items in order and changes each file:
-namespace FsToolkit.ErrorHandling+namespace $fstoolkiterrorhandlingnamespace$+#nowarn "52" "57" "1182" "3180" "3261" "3390" "3513" "3517" "3559"+#if !FABLE_COMPILER // only for files the main project excludes under Fable
[<RequireQualifiedAccess>]
-module TaskResult =+module $fstoolkiterrorhandlingvisibility$ TaskResult =
NuGet replaces the two tokens with the consumer's properties at build time. The package's build/FsToolkit.ErrorHandling.Sources.targets file does four things:
It validates the properties.
It sets CompileOrder="CompileFirst", because NuGet adds preprocessed files after the project's own files.
It repairs a multi-target Rebuild, where the SDK skips the content files.
It warns (FSTKSRC001) when a packable project omits PrivateAssets="all".
The generator fails closed. It gives correct output for every input it accepts, and it rejects F# that it does not understand with the file and line. Examples are a column-0 type extension, an explicit public, a module abbreviation, indented code at namespace level, a #if line inside a string, a comment in a namespace line, or a $word$ NuGet token. A scope check follows each #if/#else path, so a branch cannot hide a public declaration. In a file that the main project excludes from Fable, only comments, attributes, and closed #if blocks can come before the first declaration, because the Fable guard starts after that declaration. A failed run leaves no generated files, so a manual pack cannot ship stale files.
Key changes
File
Change
build/SourcesPackage.fs
Generator: project parser, line lexer, transform, staging writer, export check.
build/build.fs
GenerateSources and SourcesTest targets. DotnetPack packs the new project. SourcesTest is the default target. SourcesTest fails when the dependency groups of the two packages differ, because the groups set the consumer's FSharp.Core minimum version.
Moves the test Async module into [<AutoOpen>] module TestAsync. Without it, the source package gives FS0248.
.github/workflows/build.yml, publish.yml
CI and release run SourcesTest by name.
Evidence
Before:TestHelpers.fs from master against the package: error FS0248: Two modules named 'FsToolkit.ErrorHandling.Async' occur in two parts of this assembly. After:./build.cmd SourcesTest on 9b0170a, Status: Ok (a1a2c19 changes only the namespace check and its tests, and the 82 generator tests pass on it):
FsToolkit.ErrorHandling.Sources.Tests (net10.0) Passed: 2129, Skipped: 8 (same as FsToolkit.ErrorHandling.Tests)
FsToolkit.ErrorHandling.Sources.Tests (net11.0) Passed: 2127, Skipped: 10 (same as FsToolkit.ErrorHandling.Tests)
FsToolkit.ErrorHandling.Sources.Generator.Tests (net11.0) Passed: 82
Sources.Consumer: build OK, then build --no-incremental OK (6 TFMs: netstandard2.0, netstandard2.1, net8.0, net9.0, net10.0, net11.0)
Dependency groups: the same as FsToolkit.ErrorHandling (FSharp.Core 6.0.4, 6.0.4, 9.0.300, 10.0.100, 11.0.101)
Before: a multi-target consumer, dotnet build --no-incremental without the repair target: Library.fs(5,6): error FS0039: The namespace or module 'Contoso' is not defined (all 3 TFMs). After: with the repair target, build, --no-incremental, and -t:Rebuild pass on SDK 10.0.403. SourcesTest runs the same build and --no-incremental build on SDK 11.0.100.
Determinism: two GenerateSources runs, and a copy of all 55 sources converted to CRLF, give identical SHA-256 hashes for all 55 files. The files in obj/generated have the same hashes.
Generator tests detect defects: 16 mutants of SourcesPackage.fs (comment nesting, (*), string masking, namespace scope, #nowarn placement, explicit public, and others) each fail at least one test. A reviewer's extra mutant that keeps stale output also fails 6 tests. 11 mutants of the scope check (scopeErrors, the #if path merge, the module-body rule, and the directive rules) each fail at least one test.
Merge Danger
Door: two-way for the code. The first release with a dated RELEASE_NOTES.md entry pushes a new package ID to nuget.org, and a published package version cannot be deleted, only unlisted.
Blast Radius: build and release.
The FsToolkit.ErrorHandling package does not change. Outside the new package, the PR moves the test Async module in TestHelpers.fs, makes SourcesTest the default FAKE target, and runs SourcesTest in build.yml and in the release job (publish.yml). Fable still compiles the test project.
Each build.yml matrix leg runs longer. Locally the SourcesTest target took 28 to 44 seconds, plus the pack of the new project.
The rebuild repair uses private SDK names (_CleaningWithoutRebuilding, _ContentFilesToPreprocess). The --no-incremental step in SourcesTest fails on each CI run if a new SDK changes them. The release job then fails too. global.json rolls forward to the latest SDK 11 feature band, so a new SDK can reach the release job without a commit.
Types of changes
Bugfix (non-breaking change which fixes an issue)
New feature (non-breaking change which adds functionality)
Breaking change (fix or feature that would cause existing functionality to not work as expected)
Checklist
Build and tests pass locally
I have added tests that prove my fix is effective or that my feature works (if appropriate)
I have added necessary documentation (if appropriate)
Further comments
Not in this PR, with reasons:
IcedTasks, JobResult, and AsyncSeq have no source package. The scope is the main package only.
Fable and Paket consumers are not tested. The package README says so.
A consumer that enables --warnon:3180 must add FS3180 to WarningsNotAsErrors. The compiler reports FS3180 at unknown(1,1), so #nowarn cannot suppress it.
Possible simplifications for a follow-up issue: replace the XML project parser with dotnet msbuild -getItem:Compile (about 190 lines less), and share one test file list between the two test projects instead of the drift check.
Test gap for a follow-up issue: the generator's line model does not generate the shapes that the fail-closed rules reject (interpolated strings, quoted identifiers, comments on namespace/module/open lines, module abbreviations, directives inside strings). Example tests cover each rule.
The package needs .NET SDK 10.0 or later (baf9587). No CI job uses SDK 9 after the FSharp.Core 11 shim files came from master. The strict consumer builds on SDK 10.0.115 and 10.0.404 against the packed nupkg. With --warnon:3517, SDK 10.0.115 reports FS3517 at the call sites, also with the binary package, and the README says so.
A public Sources consumer on net11.0 also exports the FSharpCore11AsyncShims and FSharpCore11TaskShims modules, which have [<Obsolete>] and EditorBrowsable(Never) there. The modules stay, because removing them needs a generator rule for each target framework. The default internal visibility does not export them.
An attribute before the first declaration of a file excluded from Fable stays in the Fable compile. If it names a type that Fable does not compile, Fable fails. Today only Task.FSharpCore11.fs has attributes there. Under Fable, NET11_0_OR_GREATER is not defined, so only [<AutoOpen>] (an FSharp.Core attribute) stays, on an empty module. The README says that Fable is not tested.
The scope check treats each #if as independent. A file that splits one module over two #if blocks with the same condition is rejected, although it compiles. The generator fails closed here. No real source file has this shape.
Review: two rounds of the adversarial review (Claude Fable and Codex gpt-6-astra), then a fix round verified by tests. A full-branch /pr-hound review at 0a66040 found one defect (a module abbreviation got the visibility token, FS0536), fixed in c10e32b. A second full-branch /pr-hound review at 122f8e5 found no defects. Round 1 found a high defect (multi-target Rebuild) that this PR fixes. Copilot then found two generator inputs that broke the fail-closed rule (a module abbreviation with its target on the next line, and indented namespace-level code). Two more review rounds (Claude Fable and Codex) on that fix found #if branch cases, which 339156e and 9855763 fix. A third full-branch /pr-hound review at 9855763 found two documentation defects (the default target in the contributor docs, and the FSharp.Core 9.0.300 floor), fixed in c563b0b. A later Copilot review led to eb62534, which corrects the README visibility example and the PrivateAssets effect. No separate reviewer checked c563b0b beyond the /pr-hound follow-up, and none checked eb62534. A compiler build or a NuGet pack and restore proves each changed sentence in those two commits. After the merge of master (#373, FSharp.Core 11 shims), two review rounds (Claude Fable and Codex gpt-6-astra) checked eece19e and cbd59c7, which adapt the generator, the target frameworks, and the dependency check. Copilot then found that a quoted or spaced spelling of the namespace (for example namespace ``FsToolkit``.ErrorHandling) passed the generator unchanged. cfc115c and a1a2c19 reject each line that still names the identifier FsToolkit after the replacement, which also covers a name split over two lines. Claude Fable and Codex reviewed that fix, and a1a2c19 closes the boundary cases that Claude Fable found. A full-branch /pr-hound review at 35b0934 found that the release-notes entry sat in the released 6.0.0-beta005 section after master moved. The merge 9b0170a moves it to 6.0.0-beta007 - Unreleased.
CI: all 23 checks pass on a1a2c19, including dotnet-build (which runs SourcesTest) in Debug and Release on Ubuntu, macOS, and Windows.
Flaky, not caused by this PR: on 9855763, dotnet-build (Release, macOS-latest) failed once with tests/FsToolkit.ErrorHandling.Tests/AsyncResultOptionCE.fs(433,25): error FS1118: Failed to inline the value 'Using' marked 'inline' (run 42570250559, attempt 1). This PR does not change that file or the library source after 122f8e5. The same leg passed on 122f8e5, 739971c, and c563b0b. Verdict: a nondeterministic F# compiler inlining failure.
Intermittent, not caused by this PR: build-devcontainer failed on 9855763 and c563b0b, and it passed on eb62534. A devcontainer feature download fails (Starship here, Node.js on master in run 39861598639). This PR does not change .devcontainer.
After merge:
Before the first release, confirm that the nuget.org trusted publishing policy can push the new ID FsToolkit.ErrorHandling.Sources.
The new package ships the FsToolkit.ErrorHandling source as NuGet .pp
content files that compile inside the consumer project. Consumers set
FsToolkitErrorHandlingNamespace and FsToolkitErrorHandlingVisibility
(internal by default).
The FAKE target GenerateSources builds the package content from the
main project's compile order. SourcesTest packs the package, runs the
full test suite against it with public visibility, and builds a strict
internal consumer with a custom namespace.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Repair multi-target Rebuild, which skipped the package files on SDK 9
and SDK 10. SourcesTest now rebuilds the consumer fixture.
- Generator: typed errors, closed project shape, one-pass lexer for
comments and strings, #nowarn after each namespace and top-level
module, namespace replacement on declaration lines only, Fable guard,
explicit public rejected, staging directory with delete first.
- Targets: CompileFirst, case-sensitive visibility, keyword check,
FSTKSRC001 warning without PrivateAssets="all".
- Generator property tests, full-path drift check, explicit CI target,
CI CPU limit for fixtures, shorter docs page.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Reject comments in namespace, module, and open lines, and source
namespace references that remain in code after the replacement.
- Reject interpolation holes and quoted identifiers that contain
lexer markers, with errors that name the construct and line.
- Mask string text before the attribute check. Report code after an
attribute as CodeAfterAttribute.
- Targets: reject the namespace segment '_', emit FSTKSRC001 only for
packable projects, and preprocess only this package's content in
the rebuild repair.
- SourcesTest runs the targets diagnostics. The real-file insert-only
test covers the Fable guard.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A column-0 'module R = Some.Namespace' line got the visibility token,
and F# rejects an access modifier on a module abbreviation (FS0536).
The generator now fails with ColumnZeroModuleAbbreviation. The targets
diagnostics also check a project that does not set IsPackable.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Same-line type extensions can produce uncompilable F#, while incomplete reserved-word validation allows namespaces that break consumer builds using TreatWarningsAsErrors.
Adds FsToolkit.ErrorHandling.Sources for projects that need the core library’s helpers compiled into their own assembly rather than a binary dependency, addressing #381.
Changes:
Generates ordered source files with configurable namespace and visibility.
Adds package validation, compile-order handling, and multi-target rebuild support.
Integrates generator tests, package-consumer checks, documentation, and CI/release validation.
The reason will be displayed to describe this comment to others. Learn more.
Key change: the transform. For each source file it strips the BOM, normalizes to LF, and lexes each line (comments, strings, interpolation holes). It inserts the visibility token on column-0 module/type/and declarations, replaces the namespace only on namespace/module/open lines, puts #nowarn after each header, and wraps Fable-excluded files in #if !FABLE_COMPILER. Any shape it does not understand returns a typed SourceError with the file and line. The rule is fail closed: correct output for every accepted input.
The reason will be displayed to describe this comment to others. Learn more.
Key change: generate. It deletes obj/generated before it reads the project, transforms all files in memory, writes to obj/generated.staging, and then moves the folder. A failure at any step leaves no generated files, so a manual pack cannot ship old or partial files.
The reason will be displayed to describe this comment to others. Learn more.
Key change: compile order. NuGet adds preprocessed .pp content files after the project's own files, and F# then fails with FS0039 in the consumer's code. CompileFirst puts the package files first. SDK 8, 9, and 10 Microsoft.FSharp.Targets honor it.
The reason will be displayed to describe this comment to others. Learn more.
Key change: multi-target Rebuild repair. In a multi-target Rebuild (IDE Rebuild or dotnet build --no-incremental), the inner Clean sets _CleaningWithoutRebuilding, so RunProduceContentAssets skips and the package files are missing (FS0039 on SDK 9 and 10). This target produces them again. It uses private SDK names, so sourcesTest rebuilds a multi-target consumer on every CI run to detect an SDK change.
The reason will be displayed to describe this comment to others. Learn more.
Key change: the package gate.sourcesTest checks test-file drift, runs the targets errors and FSTKSRC001 warning cases, restores both fixtures from bin/ with an empty package cache, builds the strict internal consumer twice (normal and --no-incremental), runs the full test suite against the package with public visibility, and checks exported types. CI and publish.yml call this target by name.
The reason will be displayed to describe this comment to others. Learn more.
The only test-code change outside the new test projects. The test Async module had the full name FsToolkit.ErrorHandling.Async, the same as a library module. With the source package in the same assembly that gives FS0248. Nesting it in an [<AutoOpen>] module keeps every Async.never call unchanged. .NET and Fable still compile.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The namespace validation omits seven F# reserved words, allowing values such as Contoso.object to reach generated code and fail under TreatWarningsAsErrors instead of producing the intended property-specific diagnostic.
FsToolkit.ErrorHandling.Sources (PR #382, issue #381). 10 min 47 s. It covers the problem, the F#-specific failures of a naive source package, the generator and package targets, the tests, how to verify with ./build.cmd SourcesTest, the after-merge checks, follow-up work, and trade-offs.
video.mp4
Automated: video made and posted by Claude Code for @TheAngryByrd.
Review comment: "This should be in the gitbook docs as well".
The gitbook page now holds the package README text, without its
Documentation link section. SourcesTest fails when the two files differ.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Critical visibility-transform defects leave indented namespace-level types public when internal visibility is selected and generate invalid F# for multiline module abbreviations.
No open issues 🎉
2 previously missed issues
In code that hasn't changed since last review
Multiline module abbreviations receive an invalid visibility modifier
build/SourcesPackage.fs:563
A valid multiline abbreviation—module Alias = followed by an indented Microsoft.FSharp.Collections.List—passes this same-line check. The transform inserts a visibility modifier, which is forbidden on module abbreviations and causes FS0536 in the generated source. Inspect continuation lines to reject multiline abbreviations too, and add a regression beside the single-line abbreviation test.
A valid namespace-level declaration such as an indented type Probe() = class end is accepted here but receives no visibility token. The generated type therefore remains public even when the consumer selects internal, breaking the generator's fail-closed guarantee. Track declaration scope to transform or reject indented namespace-level declarations, and add a regression test.
Copilot review: "Multiline module abbreviations receive an invalid
visibility modifier" and "Indented namespace-level types bypass
visibility transformation".
The generator classified each line alone. A scope pass over the
classified lines now rejects a module abbreviation whose target or '='
is on the next line (FS0536), and indented code at namespace level,
which does not get the visibility token.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review findings on d9b05e8: an #if branch hid indented namespace-level
code, a top-level module line in each branch was rejected, and quoted,
non-ASCII, or split abbreviation targets got the visibility token.
The scope pass now follows each #if/#else path and merges the paths at
#endif. The first line of a nested module body must start with a
declaration keyword. Indented, unbalanced, and #elif directives are
rejected.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Review finding on d4af7b2: a triple-quoted string that opens in an #if
branch hid the #else branch from the generator, so a type in that branch
stayed public. The generator scans all branches with one lexer state, so
it now rejects #if, #else, #elif, and #endif lines that start inside a
string or block comment.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Reply to the Copilot review of 2026-11-09 ("2 previously missed issues"). Both findings are valid. The compiler confirmed each one.
Multiline module abbreviations receive an invalid visibility modifier. Fixed in d9b05e8 and 339156e. The generator classified each line alone. A scope check over the classified lines now requires that the first line of a nested module body starts with a declaration keyword (let, type, module, open, do, exception, extern, or [<). Any other first line is rejected as a possible abbreviation. This covers a target on the next line, module R with = List on the next line, a quoted or non-ASCII target, and a target split after a trailing dot.
Indented namespace-level types bypass visibility transformation. Fixed in d9b05e8, 339156e, and 9855763. Indented code at namespace level is now IndentedNamespaceCode. The scope check follows each #if/#else path, so a module in only one branch cannot cover the code after #endif. A #if, #else, #elif, or #endif line inside a string or block comment is rejected, because it can hide a branch from the generator.
New tests: 12 example tests in TransformTests.fs, and a property that checks rejection and the first rejected line against the line model. 11 mutants of the scope check each fail at least one test. SourcesTest passes on 9855763.
…Core floor
Review findings: README.md and .github/copilot-instructions.md still
named DotnetPack as the default target. The package README said ".NET
SDK 9.0 or later", but the package depends on FSharp.Core 9.0.300 for
net9.0, so an earlier SDK 9.0 restore reports NU1605.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
Human assessment is needed to weigh unpublished findings about inaccurate type-accessibility examples and misleading downstream source-compilation guidance.
…ffect
A public function can return Validation<'ok, 'error> under internal,
because the compiler replaces a type abbreviation. FS0410 applies to a
real type such as TaskResultBuilder, and FS1113 to a public inline
function that calls a package function.
Without PrivateAssets="all", projects that reference the consumer's
package do not compile the sources: NuGet gives them only placeholder
content and build assets. They get the FSharp.Core minimum version of
the Sources package. FSTKSRC001 now says this.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Reply to the Copilot review of 2026-11-09 06:17 UTC. It names "inaccurate type-accessibility examples" and "misleading downstream source-compilation guidance" but posts no comments. I tested the matching statements in the package README with the compiler and with a real pack. Both statements were wrong. eb62534 corrects them:
Visibility example. The README said that under internal a public function cannot return Validation<'ok, 'error> (FS0410). That is false. Validation is a type abbreviation, and the compiler replaces it, so the function builds. The README now uses real cases. A public function that returns a TaskResultBuilder gives FS0410. A public inline function that calls a package function gives FS1113. I compiled both cases.
PrivateAssets effect. The README and the FSTKSRC001 text said that projects that reference the consumer's package compile the source files again. That is false. A downstream app of a library packed without PrivateAssets="all" receives only contentFiles/any/any/_._ (buildAction: None) and build/_._ for FsToolkit.ErrorHandling.Sources. It resolves the package's FSharp.Core 9.0.300 dependency. The README and the warning now say this.
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The handwritten F# visibility rewriting and reliance on private MSBuild rebuild state warrant human compatibility review, alongside confirmation of first-release package contents and trusted-publishing permissions for the new NuGet package ID.
- Match the Fable Compile condition without whitespace between tokens,
as MSBuild does.
- Accept column-0 attributes and closed #if blocks before the first
declaration of a file excluded from Fable.
- Add warning 52 to the generated #nowarn line.
- Target net10.0 and net11.0, so the package gets the FSharp.Core
minimum versions of the binary package. Run the Sources tests on the
runtimes that CI installs.
- Add the new test files to FsToolkit.ErrorHandling.Sources.Tests.
- Document the FSharp.Core minimum versions for net10.0 and net11.0.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- SourcesTest fails when the dependency groups of the Sources package
differ from those of FsToolkit.ErrorHandling. The groups set the
FSharp.Core minimum version of each consumer.
- The consumer fixture also builds for net8.0, a runtime TFM without
NET9_0_OR_GREATER.
- Add a nested #if example to the Fable guard tests.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A header inside the outer block of nested #if blocks is rejected.
- A frameworkReferences group does not replace the dependency group of
the same target framework.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🟡 Changes recommended
Quoted namespace identifiers bypass replacement and validation, leaving declarations under FsToolkit.ErrorHandling even when a consumer configures a custom namespace.
Copilot: "The valid header namespace ``FsToolkit``.ErrorHandling is
accepted and emitted unchanged because replacement and validation only
recognize the literal spelling."
The generator replaces only the exact text FsToolkit.ErrorHandling after
a namespace, module, or open keyword. It now rejects each other line that
names the identifier FsToolkit. This also covers a quoted identifier,
spaces around the dot, and a name split over two lines.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Move the Sources entry to a new 6.0.0-beta007 - Unreleased section, because master released beta005 and beta006.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A ' directly before FsToolkit is a type variable only when it does not
close a char literal, such as 'a'FsToolkit.ErrorHandling. Test both
boundaries of the identifier match.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The reason will be displayed to describe this comment to others. Learn more.
🔵 Needs a closer look
The final packed consumer needs validation on the advertised SDK 9/F# 9 minimum after the FSharp.Core 11 shim merge, including its multi-target rebuild repair’s reliance on private SDK targets and items.
No CI job or local build uses SDK 9 after the FSharp.Core 11 shim
files. The strict consumer builds on SDK 10.0.115 and 10.0.404.
SDK 10.0.115 reports FS3517 with --warnon:3517 at the call sites, also
with the binary package. SDK 10.0.404 and 11.0.100 do not.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
On the Copilot review of baf9587 ("same-line F# type groups can bypass visibility checks and export types despite the default internal setting"): this shape does not compile, so no change is necessary.
F# rejects each type group with and on one line. dotnet fsi on SDK 11.0.100 gives error FS0010: Unexpected keyword 'and' in member definition for each of these:
typeinternalA= int andB= string
typeinternalA()=classendandB()=classendtypeinternalA={ X:int }andB={ Y:int }typeinternalA=classendandB=classendtypeinternalA=interfaceendandB=interfaceendtypeinternalA=| X andB=| Y
The valid form puts and at column 0 on its own line. The generator gives that line its own visibility token:
-type A = A of int-and B = B of string+type $fstoolkiterrorhandlingvisibility$ A = A of int+and $fstoolkiterrorhandlingvisibility$ B = B of string
Types and let rec ... and ... groups inside a module get the visibility of the module. The generator rejects an indented and at namespace level (IndentedNamespaceCode).
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
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.
Proposed Changes
Closes #381.
Some F# projects cannot take a binary dependency on
FsToolkit.ErrorHandling. They copy the source files by hand, and the copy goes out of date. This PR adds a second package,FsToolkit.ErrorHandling.Sources. It delivers the source ofsrc/FsToolkit.ErrorHandlingas files that compile inside the consumer's project. The consumer picks the namespace and the visibility:Summary
flowchart LR A["src/FsToolkit.ErrorHandling<br/>.fsproj + 55 .fs files"] -->|GenerateSources<br/>build/SourcesPackage.fs| B["obj/generated/<br/>001_Nullness.fs.pp ... 055_Seq.fs.pp"] B -->|DotnetPack| C["FsToolkit.ErrorHandling.Sources.nupkg<br/>contentFiles/fs/any + build/*.targets"] C -->|SourcesTest| D["Sources.Tests: full test suite, public"] C -->|SourcesTest| E["Sources.Consumer: strict, internal,<br/>custom namespace, build + rebuild"]The generator (
build/SourcesPackage.fs) reads theCompileitems in order and changes each file:NuGet replaces the two tokens with the consumer's properties at build time. The package's
build/FsToolkit.ErrorHandling.Sources.targetsfile does four things:CompileOrder="CompileFirst", because NuGet adds preprocessed files after the project's own files.FSTKSRC001) when a packable project omitsPrivateAssets="all".The generator fails closed. It gives correct output for every input it accepts, and it rejects F# that it does not understand with the file and line. Examples are a column-0 type extension, an explicit
public, a module abbreviation, indented code at namespace level, a#ifline inside a string, a comment in anamespaceline, or a$word$NuGet token. A scope check follows each#if/#elsepath, so a branch cannot hide a public declaration. In a file that the main project excludes from Fable, only comments, attributes, and closed#ifblocks can come before the first declaration, because the Fable guard starts after that declaration. A failed run leaves no generated files, so a manual pack cannot ship stale files.Key changes
build/SourcesPackage.fsbuild/build.fsGenerateSourcesandSourcesTesttargets.DotnetPackpacks the new project.SourcesTestis the default target.SourcesTestfails when the dependency groups of the two packages differ, because the groups set the consumer'sFSharp.Coreminimum version.src/FsToolkit.ErrorHandling.Sources/.targets, package README.gitbook/sources/sourcesPackage.mdSourcesTestfails when the two files differ.tests/FsToolkit.ErrorHandling.Sources.Generator.Teststests/FsToolkit.ErrorHandling.Sources.TestsFsToolkit.ErrorHandling.Testsand runs them against the package.tests/FsToolkit.ErrorHandling.Sources.Consumerinternalconsumer with a custom namespace, plusdiagnostics/Diagnostics.projfor the targets errors and warning.tests/FsToolkit.ErrorHandling.Tests/TestHelpers.fsAsyncmodule into[<AutoOpen>] module TestAsync. Without it, the source package gives FS0248..github/workflows/build.yml,publish.ymlSourcesTestby name.Evidence
Before:
TestHelpers.fsfrommasteragainst the package:error FS0248: Two modules named 'FsToolkit.ErrorHandling.Async' occur in two parts of this assembly.After:
./build.cmd SourcesTeston9b0170a,Status: Ok(a1a2c19changes only the namespace check and its tests, and the 82 generator tests pass on it):Before: a multi-target consumer,
dotnet build --no-incrementalwithout the repair target:Library.fs(5,6): error FS0039: The namespace or module 'Contoso' is not defined(all 3 TFMs).After: with the repair target,
build,--no-incremental, and-t:Rebuildpass on SDK 10.0.403.SourcesTestruns the same build and--no-incrementalbuild on SDK 11.0.100.Determinism: two
GenerateSourcesruns, and a copy of all 55 sources converted to CRLF, give identical SHA-256 hashes for all 55 files. The files inobj/generatedhave the same hashes.Generator tests detect defects: 16 mutants of
SourcesPackage.fs(comment nesting,(*), string masking, namespace scope,#nowarnplacement, explicitpublic, and others) each fail at least one test. A reviewer's extra mutant that keeps stale output also fails 6 tests. 11 mutants of the scope check (scopeErrors, the#ifpath merge, the module-body rule, and the directive rules) each fail at least one test.Merge Danger
Door: two-way for the code. The first release with a dated
RELEASE_NOTES.mdentry pushes a new package ID to nuget.org, and a published package version cannot be deleted, only unlisted.Blast Radius: build and release.
FsToolkit.ErrorHandlingpackage does not change. Outside the new package, the PR moves the testAsyncmodule inTestHelpers.fs, makesSourcesTestthe default FAKE target, and runsSourcesTestinbuild.ymland in the release job (publish.yml). Fable still compiles the test project.build.ymlmatrix leg runs longer. Locally theSourcesTesttarget took 28 to 44 seconds, plus the pack of the new project._CleaningWithoutRebuilding,_ContentFilesToPreprocess). The--no-incrementalstep inSourcesTestfails on each CI run if a new SDK changes them. The release job then fails too.global.jsonrolls forward to the latest SDK 11 feature band, so a new SDK can reach the release job without a commit.Types of changes
Checklist
Further comments
Not in this PR, with reasons:
IcedTasks,JobResult, andAsyncSeqhave no source package. The scope is the main package only.--warnon:3180must addFS3180toWarningsNotAsErrors. The compiler reports FS3180 atunknown(1,1), so#nowarncannot suppress it.dotnet msbuild -getItem:Compile(about 190 lines less), and share one test file list between the two test projects instead of the drift check.namespace/module/openlines, module abbreviations, directives inside strings). Example tests cover each rule.baf9587). No CI job uses SDK 9 after theFSharp.Core11 shim files came frommaster. The strict consumer builds on SDK 10.0.115 and 10.0.404 against the packed nupkg. With--warnon:3517, SDK 10.0.115 reports FS3517 at the call sites, also with the binary package, and the README says so.publicSources consumer onnet11.0also exports theFSharpCore11AsyncShimsandFSharpCore11TaskShimsmodules, which have[<Obsolete>]andEditorBrowsable(Never)there. The modules stay, because removing them needs a generator rule for each target framework. The defaultinternalvisibility does not export them.Task.FSharpCore11.fshas attributes there. Under Fable,NET11_0_OR_GREATERis not defined, so only[<AutoOpen>](anFSharp.Coreattribute) stays, on an empty module. The README says that Fable is not tested.#ifas independent. A file that splits one module over two#ifblocks with the same condition is rejected, although it compiles. The generator fails closed here. No real source file has this shape.Review: two rounds of the adversarial review (Claude Fable and Codex
gpt-6-astra), then a fix round verified by tests. A full-branch/pr-houndreview at0a66040found one defect (a module abbreviation got the visibility token, FS0536), fixed inc10e32b. A second full-branch/pr-houndreview at122f8e5found no defects. Round 1 found a high defect (multi-target Rebuild) that this PR fixes. Copilot then found two generator inputs that broke the fail-closed rule (a module abbreviation with its target on the next line, and indented namespace-level code). Two more review rounds (Claude Fable and Codex) on that fix found#ifbranch cases, which339156eand9855763fix. A third full-branch/pr-houndreview at9855763found two documentation defects (the default target in the contributor docs, and theFSharp.Core9.0.300 floor), fixed inc563b0b. A later Copilot review led toeb62534, which corrects the README visibility example and thePrivateAssetseffect. No separate reviewer checkedc563b0bbeyond the/pr-houndfollow-up, and none checkedeb62534. A compiler build or a NuGet pack and restore proves each changed sentence in those two commits. After the merge ofmaster(#373,FSharp.Core11 shims), two review rounds (Claude Fable and Codexgpt-6-astra) checkedeece19eandcbd59c7, which adapt the generator, the target frameworks, and the dependency check. Copilot then found that a quoted or spaced spelling of the namespace (for examplenamespace ``FsToolkit``.ErrorHandling) passed the generator unchanged.cfc115canda1a2c19reject each line that still names the identifierFsToolkitafter the replacement, which also covers a name split over two lines. Claude Fable and Codex reviewed that fix, anda1a2c19closes the boundary cases that Claude Fable found. A full-branch/pr-houndreview at35b0934found that the release-notes entry sat in the released6.0.0-beta005section aftermastermoved. The merge9b0170amoves it to6.0.0-beta007 - Unreleased.CI: all 23 checks pass on
a1a2c19, includingdotnet-build(which runsSourcesTest) in Debug and Release on Ubuntu, macOS, and Windows.9855763,dotnet-build (Release, macOS-latest)failed once withtests/FsToolkit.ErrorHandling.Tests/AsyncResultOptionCE.fs(433,25): error FS1118: Failed to inline the value 'Using' marked 'inline'(run 42570250559, attempt 1). This PR does not change that file or the library source after122f8e5. The same leg passed on122f8e5,739971c, andc563b0b. Verdict: a nondeterministic F# compiler inlining failure.build-devcontainerfailed on9855763andc563b0b, and it passed oneb62534. A devcontainer feature download fails (Starship here, Node.js onmasterin run 39861598639). This PR does not change.devcontainer.After merge:
FsToolkit.ErrorHandling.Sources.🤖 Generated with Claude Code