dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.94k stars 788 forks source link

Can't #load recursive namespaces into FSI #13516

Open mciccottidmg opened 2 years ago

mciccottidmg commented 2 years ago

A file with a recursive namespace reference can't be loaded in FSI. This is similar to #237.

It does compile as part of a project.

Steps to reproduce:

  1. Create a file named "test.fs" containing:
    
    namespace rec FakeNamespaceName

module AType =

type InnerType =
    member x.AMethod: FakeNamespaceName.AType2 = { AField = "test" }

type AType2 = { AField: string }

2. Load the file in FSI:

load "test.fs"



**Expected behavior**

The file is loaded into FSI.

**Actual behavior**

FSI gives this error:
test.fs(6,25): error FS0039: The namespace or module 'FakeNamespaceName' is not defined.

**Known workarounds**

* Don't use recursive namespaces with FSI
* Compile the code into an assembly and load that into FSI

**Related information**

Provide any related information (optional):
* Microsoft (R) F# Interactive version 12.0.4.0 for F# 6.0
KevinRansom commented 2 years ago

Interesting,

namespace isn't really valid in fsx files, use module rec SomeNamespace. I can't really explain the rationale though, and I had never noticed this behaviour before.

c:\kevinransom\fsharp>copy con foo.fsx
namespace HelloWorld
^Z
        1 file(s) copied.

c:\kevinransom\fsharp>dotnet fsi foo.fsx

foo.fsx(1,1): error FS0010: Unexpected keyword 'namespace' in interaction

c:\kevinransom\fsharp>

And also:

c:\kevinransom\fsharp>ue foo.fsx

c:\kevinransom\fsharp>fsc foo.fsx
Microsoft (R) F# Compiler version 12.0.4.0 for F# 6.0
Copyright (c) Microsoft Corporation. All Rights Reserved.

foo.fsx(1,21): warning FS0988: Main module of program is empty: nothing will happen when it is run

c:\kevinransom\fsharp>fsi foo.fsx

foo.fsx(1,1): error FS0010: Unexpected keyword 'namespace' in interaction
mciccottidmg commented 2 years ago

Yeah, I wouldn't deliberately use namespaces in FSI. Its generated code though, so unfortunately I don't have a great alternative.