Skip to content

Compile Error when last file doesn't specify module or namespace #2365

Description

@Booksbaum

Description

When the last F# file doesn't begin with module or namespace declaration, the compilation fails with:

[...]/Program.fs(5,1): (6,1) error FSHARP: Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule'. Only the last source file of an application may omit such a declaration. (code 222)

Repro code

(create directory for testing: mkdir tmp && cd tmp)

dotnet new console -lang f#

dotnet new tool-manifest

dotnet tool install fable

dotnet build

[...]
Build succeeded. 

dotnet fable

Fable: F# to JS compiler 3.1.2
Thanks to the contributor! @GordonBGood
.> cmd /C dotnet restore tmp.fsproj
  Determining projects to restore...
  All projects are up-to-date for restore.
Parsing tmp.fsproj...
Initializing F# compiler...
Compiling tmp.fsproj...
F# compilation finished in 1218ms
[...]/tmp/Program.fs(3,1): (4,1) error FSHARP: Files in libraries or multiple-file applications must begin with a namespace or module declaration, e.g. 'namespace SomeNamespace.SubNamespace' or 'module SomeNamespace.SomeModule'. Only the last source file of an application may omit such a declaration. (code 222)
Compilation failed

Expected and actual results

Expected: should compile -- just like the .NET version.

Actual: compilation error (see output above)

The default code for a new F# project is:

// Learn more about F# at http://docs.microsoft.com/dotnet/fsharp

open System

// Define a function to construct a message to print
let from whom =
    sprintf "from %s" whom

[<EntryPoint>]
let main argv =
    let message = from "F#" // Call the function
    printfn "Hello world %s" message
    0 // return an integer exit code

-> doesn't begin with namespace or module declaration.

Program.fs is the last (and only) file in the project -> according to the error message it should compile.

Adding module A to the top results in a successful build:

dotnet fable

Fable: F# to JS compiler 3.1.2
Thanks to the contributor! @anchann
.> cmd /C dotnet restore tmp.fsproj
  Determining projects to restore...
  All projects are up-to-date for restore.
Parsing tmp.fsproj...
Initializing F# compiler...
Compiling tmp.fsproj...
F# compilation finished in 1198ms
Compiled Program.fs
Fable compilation finished in 225ms

Related information

  • Fable version: dotnet fable --version: 3.1.2
  • Dotnet version: dotnet --version: 5.0.102

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions