dotnet / dotnet/fsharp

When using EntryPoint attribute in fsx script top-level code is not executed

Open
#13,350 2 comments 0 reactions 0 assignees View on GitHub
Area-FSI Bug Impact-Medium
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 22h
Merged PRs (30d)
144

Description

Let bindings still work, but side-effects are not executed.

**Repro steps**

`script.fsx` :

```fsharp
printfn "Hello world"

[]
let main _ = 0
```
`dotnet fsi script.fsx`

**Expected behavior**

We see `Hello world` printed, as it is when there is no `[]` used ([sharplab - no EntryPoint](https://sharplab.io/#v2:DYLgZgzgNALiBOBXAdlAJiA1AHwA7wEtkYxkACAIgAkBTYYAezIHcH5g0KBYAKF4Hp+AbQA8AUWLwAngAUGRGAD4Aur2A0YZALYBDImQD6ZALxkADLyA)).

**Actual behavior**

Nothing is printed. ([sharplab - with EntryPoint](https://sharplab.io/#v2:DYLgZgzgNALiBOBXAdlAJiA1AHwA7wEtkYxkACAIgAkBTYYAezIHcH5g0KBYAKF4G0APAFFi8AJ4AFBkRgA+ALq9gNGGQC2AQyJkA+mQC8ZAAy8gA===))

**Known workarounds**

Don't use `[]` in scripts - as it's not executed anyway, see #1431

**Considerations**

Using `[]` in a script could just raise an error which would resolve this issue. It would cause a breaking change though for scripts that are imported from other scripts, because this currently works as expected:

`script1.fsx`:
```fsharp
let hello = printfn "Hello %s"

[]
let main _ = 0
```

`script2.fsx`:
```fsharp
#load "script1.fsx"
open Script1

hello "world"
```

`dotnet fsi script2.fsx` prints "Hello world"

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.