Skip to content

Commit a8db5c6

Browse files
CopilotT-Gro
andcommitted
Implement --typecheck-only support for FSI scripts
Co-authored-by: T-Gro <[email protected]>
1 parent 89b69aa commit a8db5c6

File tree

5 files changed

+51
-0
lines changed

5 files changed

+51
-0
lines changed

src/Compiler/Driver/CompilerOptions.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,13 @@ let advancedFlagsFsi tcConfigB =
13081308
None,
13091309
Some(FSComp.SR.optsClearResultsCache ())
13101310
)
1311+
CompilerOption(
1312+
"typecheck-only",
1313+
tagNone,
1314+
OptionUnit(fun () -> tcConfigB.typeCheckOnly <- true),
1315+
None,
1316+
Some(FSComp.SR.optsTypecheckOnly ())
1317+
)
13111318
]
13121319

13131320
let advancedFlagsFsc tcConfigB =

src/Compiler/FSComp.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,7 @@ optsVersion,"Display compiler version banner and exit"
903903
optsResponseFile,"Read response file for more options"
904904
optsCodepage,"Specify the codepage used to read source files"
905905
optsClearResultsCache,"Clear the package manager results cache"
906+
optsTypecheckOnly,"Perform type checking only, do not execute code"
906907
optsUtf8output,"Output messages in UTF-8 encoding"
907908
optsFullpaths,"Output messages with fully qualified paths"
908909
optsLib,"Specify a directory for the include path which is used to resolve source files and assemblies (Short form: -I)"

src/Compiler/Interactive/fsi.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,10 @@ type internal FsiDynamicCompiler
22172217
inputs
22182218
))
22192219

2220+
// Add this check after CheckClosedInputSet
2221+
if tcConfig.typeCheckOnly then
2222+
raise StopProcessing
2223+
22202224
let codegenResults, optEnv, fragName =
22212225
ProcessTypedImpl(
22222226
diagnosticsLogger,

tests/FSharp.Compiler.ComponentTests/FSharp.Compiler.ComponentTests.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@
271271
<Compile Include="Interop\ParamArray.fs" />
272272
<Compile Include="Interop\Literals.fs" />
273273
<Compile Include="Scripting\Interactive.fs" />
274+
<Compile Include="Scripting\TypeCheckOnlyTests.fs" />
274275
<Compile Include="TypeChecks\SeqTypeCheckTests.fs" />
275276
<Compile Include="TypeChecks\CheckDeclarationsTests.fs" />
276277
<Compile Include="TypeChecks\Graph\Utils.fs" />
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module FSharp.Compiler.ComponentTests.Scripting.TypeCheckOnlyTests
2+
3+
open Xunit
4+
open FSharp.Test
5+
open FSharp.Test.Compiler
6+
7+
[<Fact>]
8+
let ``typecheck-only flag works for valid script``() =
9+
Fsx """
10+
let x = 42
11+
printfn "This should not execute"
12+
"""
13+
|> withOptions ["--typecheck-only"]
14+
|> compile
15+
|> shouldSucceed
16+
17+
[<Fact>]
18+
let ``typecheck-only flag catches type errors``() =
19+
Fsx """
20+
let x: int = "string" // Type error
21+
"""
22+
|> withOptions ["--typecheck-only"]
23+
|> compile
24+
|> shouldFail
25+
|> withDiagnostics [
26+
(Error 1, Line 2, Col 14, Line 2, Col 22, "This expression was expected to have type\n 'int' \nbut here has type\n 'string'")
27+
]
28+
29+
[<Fact>]
30+
let ``typecheck-only flag prevents execution side effects``() =
31+
Fsx """
32+
System.IO.File.WriteAllText("test-file.txt", "should not be created")
33+
let x = 42
34+
"""
35+
|> withOptions ["--typecheck-only"]
36+
|> compile
37+
|> shouldSucceed
38+
// Verify file was not created (test would need additional verification logic)

0 commit comments

Comments
 (0)