dotnet / dotnet/fsharp

F# interactive duplicates type definition if implicitly loaded 2+ times from other .fsx files

Open
#15,701 4 comments 0 reactions 0 assignees View on GitHub
Area-FSI Feature Request Needs-design
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

I have a hierarchy of .fsx scripts where entry point script `#load` a few helper scripts. Some of the leaf-level scripts loaded twice or more times which F# interactive refuses to compile.

**Repro steps**

1. Create a temp folder for fsx scripts , unpack this archive here and go to step 6 (or create files manually in steps 2 to 5) [SharedTypeFsiBug.zip](https://github.com/dotnet/fsharp/files/12190644/SharedTypeFsiBug.zip)
2. Create `1_SharedType.fsx` file with this code

```
namespace BugDemo.Shared

module Types =
type SharedType = Hello | World
```

3. Create `2_Func1_Return_SharedType.fsx` file with this code

```
#load "./1_SharedType.fsx"

namespace BugDemo.Shared

open BugDemo.Shared.Types

module Funcs2 =
let func2 (_: int) : SharedType = SharedType.World
```

4. Create `2_Func2_Return_SharedType.fsx` file with this code

```
#load "./1_SharedType.fsx"

namespace BugDemo.Shared

open BugDemo.Shared.Types

module Funcs2 =
let func2 (_: int) : SharedType = SharedType.World
```

5. Create `3_Main.fsx` file with this code:

```
#load "1_SharedType.fsx"
#load "./2_Func1_Return_SharedType.fsx"
#load "./2_Func2_Return_SharedType.fsx"

open BugDemo.Shared.Types
open BugDemo.Shared.Funcs1
open BugDemo.Shared.Funcs2

let x: SharedType = func1 1
let y: SharedType = func2 2
System.Console.WriteLine ("Are x and y equal? " + (x = y).ToString())
```

6. Run `dotnet fsi ./3_Main.fsx`

**Expected behavior**

Script compiles runs and prints "Are x and y equal? False"

**Actual behavior**

Compilation fails with the error:

```
3_Main.fsx(9,21): error FS0001: This expression was expected to have type
'FSI_0003.BugDemo.Shared.Types.SharedType'
but here has type
'FSI_0002.BugDemo.Shared.Types.SharedType'
```

**Known workarounds**

Need to create yet another top level script that has one line `#load "./3_Main.fsx"` and run it.
This way F# interactive somehow properly deduplicates repeated #load directives during compilation and runs script successfully

**Related information**

Provide any related information (optional):

* Windows 11
* .NET SDK 7.0.306

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.