Skip to content

Commit 09e26b0

Browse files
authored
Merge pull request #594 from alfonsogarciacaro/file-order
Fix ProjectFileNames order when getting project options from script
2 parents e3afab2 + d06a424 commit 09e26b0

File tree

13 files changed

+79
-30
lines changed

13 files changed

+79
-30
lines changed

src/fsharp/CompileOps.fs

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4976,37 +4976,32 @@ module private ScriptPreprocessClosure =
49764976
match closureDirective with
49774977
| ClosedSourceFile _ as csf -> [csf]
49784978
| SourceFile(filename,m,source) ->
4979-
let filename = FileSystem.GetFullPathShim(filename)
4980-
if observedSources.HaveSeen(filename) then []
4981-
else
4982-
observedSources.SetSeen(filename)
4983-
4984-
let errors = ref []
4985-
let warnings = ref []
4986-
let errorLogger =
4987-
{ new ErrorLogger("FindClosure") with
4988-
member x.ErrorSinkImpl(e) = errors := e :: !errors
4989-
member x.WarnSinkImpl(e) = warnings := e :: !warnings
4990-
member x.ErrorCount = (!errors).Length }
4991-
4992-
use unwindEL = PushErrorLoggerPhaseUntilUnwind (fun _ -> errorLogger)
4993-
let pathOfMetaCommandSource = Path.GetDirectoryName(filename)
4994-
match ParseScriptText(filename,source,!tcConfig,codeContext,lexResourceManager,errorLogger) with
4995-
| Some(input) ->
4996-
let tcConfigResult, noWarns = ApplyMetaCommandsFromInputToTcConfigAndGatherNoWarn !tcConfig (input,pathOfMetaCommandSource)
4997-
tcConfig := tcConfigResult
4979+
let errors = ref []
4980+
let warnings = ref []
4981+
let errorLogger =
4982+
{ new ErrorLogger("FindClosure") with
4983+
member x.ErrorSinkImpl(e) = errors := e :: !errors
4984+
member x.WarnSinkImpl(e) = warnings := e :: !warnings
4985+
member x.ErrorCount = (!errors).Length }
4986+
4987+
use unwindEL = PushErrorLoggerPhaseUntilUnwind (fun _ -> errorLogger)
4988+
let pathOfMetaCommandSource = Path.GetDirectoryName(filename)
4989+
match ParseScriptText(filename,source,!tcConfig,codeContext,lexResourceManager,errorLogger) with
4990+
| Some(input) ->
4991+
let tcConfigResult, noWarns = ApplyMetaCommandsFromInputToTcConfigAndGatherNoWarn !tcConfig (input,pathOfMetaCommandSource)
4992+
tcConfig := tcConfigResult
49984993

4999-
let AddFileIfNotSeen(m,filename) =
5000-
if observedSources.HaveSeen(filename) then []
5001-
else
5002-
if IsScript(filename) then SourceFileOfFilename(filename,m,tcConfigResult.inputCodePage)
5003-
else
5004-
observedSources.SetSeen(filename)
5005-
[ClosedSourceFile(filename,m,None,[],[],[])] // Don't traverse into .fs leafs.
4994+
let AddFileIfNotSeen(m,filename) =
4995+
if observedSources.HaveSeen(filename) then []
4996+
else
4997+
observedSources.SetSeen(filename)
4998+
if IsScript(filename) then SourceFileOfFilename(filename,m,tcConfigResult.inputCodePage)
4999+
else [ClosedSourceFile(filename,m,None,[],[],[])] // Don't traverse into .fs leafs.
50065000

5007-
let loadedSources = (!tcConfig).GetAvailableLoadedSources() |> List.rev |> List.map AddFileIfNotSeen |> List.concat
5008-
ClosedSourceFile(filename,m,Some(input),!errors,!warnings,!noWarns) :: loadedSources |> List.map FindClosure |> List.concat // Final closure is in reverse order. Keep the closed source at the top.
5009-
| None -> [ClosedSourceFile(filename,m,None,!errors,!warnings,[])]
5001+
let loadedSources = (!tcConfig).GetAvailableLoadedSources() |> List.map AddFileIfNotSeen |> List.concat
5002+
(loadedSources |> List.map FindClosure |> List.concat)
5003+
@ [ClosedSourceFile(filename,m,Some(input),!errors,!warnings,!noWarns)]
5004+
| None -> [ClosedSourceFile(filename,m,None,!errors,!warnings,[])]
50105005

50115006
closureDirectives |> List.map FindClosure |> List.concat, !tcConfig
50125007

@@ -5024,7 +5019,7 @@ module private ScriptPreprocessClosure =
50245019
let sourceFiles = ref []
50255020
let sourceInputs = ref []
50265021
let globalNoWarns = ref []
5027-
for directive in closureDirectives do
5022+
for directive in List.rev closureDirectives do
50285023
match directive with
50295024
| ClosedSourceFile(filename,m,input,_,_,noWarns) ->
50305025
let filename = FileSystem.GetFullPathShim(filename)

tests/service/ProjectOptionsTests.fs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,22 @@ let ``Project file parsing -- report files``() =
418418
for f in Directory.EnumerateFiles(@"C:\Program Files (x86)\Microsoft SDKs\F#\4.0\","*",SearchOption.AllDirectories) do
419419
printfn "File: %s" f
420420

421+
[<Test>]
422+
let ``Test ProjectFileNames order for GetProjectOptionsFromScript`` () = // See #594
423+
let test scriptName expected =
424+
let scriptPath = __SOURCE_DIRECTORY__ + @"/data/ScriptProject/" + scriptName + ".fsx"
425+
let scriptSource = File.ReadAllText scriptPath
426+
let projOpts =
427+
checker.GetProjectOptionsFromScript(scriptPath, scriptSource)
428+
|> Async.RunSynchronously
429+
projOpts.ProjectFileNames
430+
|> Array.map Path.GetFileNameWithoutExtension
431+
|> (=) expected
432+
|> shouldEqual true
433+
test "Main1" [|"BaseLib1"; "Lib1"; "Lib2"; "Main1"|]
434+
test "Main2" [|"BaseLib1"; "Lib1"; "Lib2"; "Lib3"; "Main2"|]
435+
test "Main3" [|"Lib3"; "Lib4"; "Main3"|]
436+
test "Main4" [|"BaseLib2"; "Lib5"; "BaseLib1"; "Lib1"; "Lib2"; "Main4"|]
421437

422438
#endif
423439

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module BaseLib1
2+
3+
let add2 x = x + 2
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module BaseLib2
2+
3+
let add10 x = x + 10
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#load "BaseLib1.fs"
2+
let add3 = BaseLib1.add2 >> ((+) 1)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#load "BaseLib1.fs"
2+
let add4 = BaseLib1.add2 >> ((+) 2)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Lib3
2+
3+
let add6 = ((+) 6)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Lib4
2+
3+
let add8 = ((+) 8)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#load "BaseLib2.fs"
2+
let add12 = BaseLib2.add10 >> ((+) 2)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#load "Lib1.fsx"
2+
#load "Lib2.fsx"
3+
Lib1.add3 5 |> printfn "%i"
4+
Lib2.add4 5 |> printfn "%i"

0 commit comments

Comments
 (0)