From bf6218626b44003c3c35eb53eb86c6dd12890e05 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Wed, 4 Sep 2024 11:41:24 +0800 Subject: [PATCH 01/10] fsxc: avoid empty commented lines Previous fixes for line numbers were introducing empty commented lines. This refactoring allows the lines to actually have the real content they were carrying, but commented out. --- fsxc/Fsxc.fs | 63 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 21 deletions(-) diff --git a/fsxc/Fsxc.fs b/fsxc/Fsxc.fs index 53a2996..240abf6 100644 --- a/fsxc/Fsxc.fs +++ b/fsxc/Fsxc.fs @@ -170,10 +170,16 @@ module Program = | Ref of string | NugetRef of name: string * version: Option + type LineKind = + | Commented + | Normal + | PreProcessorAction of action: PreProcessorAction + type LineAction = - | Comment - | Normal of line: string - | PreProcessorAction of line: string * action: PreProcessorAction + { + Line: string + LineKind: LineKind + } type FsxScript = { @@ -297,18 +303,26 @@ module Program = let rest = Seq.tail lines let newAcc, newState = - let normalOperation() = + let noStateChange() = if isFsiPreProcessorAction line then let lineAction = - LineAction.PreProcessorAction( - line, - (readPreprocessorLine line) - ) + { + Line = line + LineKind = + LineKind.PreProcessorAction( + readPreprocessorLine line + ) + } let newAcc = lineAction :: acc newAcc, readState else - let lineAction = LineAction.Normal line + let lineAction = + { + Line = line + LineKind = LineKind.Normal + } + let newAcc = lineAction :: acc newAcc, readState @@ -321,7 +335,12 @@ module Program = elif line.Trim() = "#endif" then acc, NormalOperation else - LineAction.Comment :: acc, readState + { + Line = line + LineKind = LineKind.Commented + } + :: acc, + readState newAcc, newState | DeliverLinesUntilNextPreProcessorConditional -> @@ -330,7 +349,7 @@ module Program = elif line.Trim() = "#endif" then acc, NormalOperation else - normalOperation() + noStateChange() | NormalOperation -> let trimmedLine = line.Trim() @@ -355,7 +374,7 @@ module Program = failwith "Only simple ifdef statements are supported for the LEGACY_FRAMEWORK define" else - normalOperation() + noStateChange() readLines rest newState newAcc | None -> acc @@ -386,8 +405,8 @@ module Program = yield script.LastWriteTime for maybeDep in scriptContents do - match maybeDep with - | LineAction.PreProcessorAction(_line, preProcessorAction) -> + match maybeDep.LineKind with + | LineKind.PreProcessorAction preProcessorAction -> match preProcessorAction with | PreProcessorAction.Load file -> let fileInfo = @@ -569,13 +588,15 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } let startCommentInFSharp = "// " for maybeDep in contents do - match maybeDep with - | LineAction.Comment -> + let line = maybeDep.Line + + match maybeDep.LineKind with + | LineKind.Commented -> File.AppendAllText( autogeneratedFile.FullName, - startCommentInFSharp.TrimEnd() + Environment.NewLine + startCommentInFSharp + line + Environment.NewLine ) - | LineAction.Normal line -> + | LineKind.Normal -> // TODO: remove ".fs" from __SOURCE_FILE__ too, see https://learn.microsoft.com/en-us/dotnet/fsharp/language-reference/source-line-file-path-identifiers let newLine = line.Replace( @@ -587,7 +608,7 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } autogeneratedFile.FullName, newLine + Environment.NewLine ) - | LineAction.PreProcessorAction(line, action) -> + | LineKind.PreProcessorAction action -> File.AppendAllText( autogeneratedFile.FullName, startCommentInFSharp + line + Environment.NewLine @@ -709,8 +730,8 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } let rec iterate(lines: List) : unit = match lines with | head :: tail -> - match head with - | LineAction.PreProcessorAction(_line, action) -> + match head.LineKind with + | LineKind.PreProcessorAction action -> match action with | PreProcessorAction.NugetRef ( From 88c79a1009e47efe61763a8756d3a4364d977ac0 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Wed, 4 Sep 2024 11:47:47 +0800 Subject: [PATCH 02/10] fsxc: refactoring --- fsxc/Fsxc.fs | 109 +++++++++++++++++++++++++++++---------------------- 1 file changed, 63 insertions(+), 46 deletions(-) diff --git a/fsxc/Fsxc.fs b/fsxc/Fsxc.fs index 240abf6..fbea64b 100644 --- a/fsxc/Fsxc.fs +++ b/fsxc/Fsxc.fs @@ -170,6 +170,15 @@ module Program = | Ref of string | NugetRef of name: string * version: Option + type PreProcessorConditional = + | If + | Else + | EndIf + + type PreProcessorElement = + | Action of PreProcessorAction + | Conditional of PreProcessorConditional + type LineKind = | Commented | Normal @@ -217,9 +226,13 @@ module Program = let ReadScriptContents(origScript: FileInfo) : List = - let readPreprocessorLine(line: string) : PreProcessorAction = - if (line.StartsWith("#!")) then - PreProcessorAction.Skip + let readPreprocessorLine(line: string) : Option = + let trimmedLine = line.TrimStart() + + if not(trimmedLine.StartsWith "#") then + None + elif (line.StartsWith "#!") then + PreProcessorAction.Skip |> PreProcessorElement.Action |> Some elif (line.StartsWith LOAD_PREPROCESSOR) then let fileToLoad = line.Substring( @@ -228,6 +241,8 @@ module Program = ) PreProcessorAction.Load fileToLoad + |> PreProcessorElement.Action + |> Some elif line.StartsWith REFNUGET_PREPROCESSOR then let libToRef = line.Substring( @@ -270,6 +285,8 @@ module Program = libToRef, None PreProcessorAction.NugetRef(libName, maybeVersion) + |> PreProcessorElement.Action + |> Some elif (line.StartsWith REF_PREPROCESSOR) then let libToRef = @@ -279,6 +296,20 @@ module Program = ) PreProcessorAction.Ref libToRef + |> PreProcessorElement.Action + |> Some + elif trimmedLine.StartsWith "#else" then + PreProcessorConditional.Else + |> PreProcessorElement.Conditional + |> Some + elif trimmedLine.StartsWith "#endif" then + PreProcessorConditional.EndIf + |> PreProcessorElement.Conditional + |> Some + elif trimmedLine.StartsWith "#if" then + PreProcessorConditional.If + |> PreProcessorElement.Conditional + |> Some else failwithf "Unrecognized preprocessor line: %s" line @@ -288,43 +319,27 @@ module Program = (acc: List) : List = - let isFsiPreProcessorAction(line: string) = - if not(line.StartsWith "#") then - false - elif line.StartsWith "#if" - || line.StartsWith "#else" - || line.StartsWith "#endif" then - false - else - true - match Seq.tryHead lines with | Some line -> let rest = Seq.tail lines let newAcc, newState = let noStateChange() = - if isFsiPreProcessorAction line then - let lineAction = - { - Line = line - LineKind = - LineKind.PreProcessorAction( - readPreprocessorLine line - ) - } - - let newAcc = lineAction :: acc - newAcc, readState - else - let lineAction = - { - Line = line - LineKind = LineKind.Normal - } - - let newAcc = lineAction :: acc - newAcc, readState + let lineKind = + match readPreprocessorLine line with + | Some(PreProcessorElement.Action action) -> + LineKind.PreProcessorAction action + | None -> LineKind.Normal + | _ -> LineKind.Commented + + let newAcc = + { + Line = line + LineKind = lineKind + } + :: acc + + newAcc, readState match readState with | IgnoreLinesUntilNextPreProcessorConditional -> @@ -355,24 +370,26 @@ module Program = if trimmedLine.StartsWith "#if" && line.Contains "LEGACY_FRAMEWORK" then - match trimmedLine with - | "#if LEGACY_FRAMEWORK" -> + let newState = + match trimmedLine with + | "#if LEGACY_FRAMEWORK" -> #if LEGACY_FRAMEWORK - acc, - DeliverLinesUntilNextPreProcessorConditional + DeliverLinesUntilNextPreProcessorConditional #else - acc, IgnoreLinesUntilNextPreProcessorConditional + IgnoreLinesUntilNextPreProcessorConditional #endif - | "#if !LEGACY_FRAMEWORK" -> + | "#if !LEGACY_FRAMEWORK" -> #if LEGACY_FRAMEWORK - acc, IgnoreLinesUntilNextPreProcessorConditional + IgnoreLinesUntilNextPreProcessorConditional #else - acc, - DeliverLinesUntilNextPreProcessorConditional + DeliverLinesUntilNextPreProcessorConditional #endif - | _ -> - failwith - "Only simple ifdef statements are supported for the LEGACY_FRAMEWORK define" + + | _ -> + failwith + "Only simple ifdef statements are supported for the LEGACY_FRAMEWORK define" + + acc, newState else noStateChange() From 7b406146525502aa2c7b8f680b420b4328f0d1c1 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Wed, 4 Sep 2024 14:03:20 +0800 Subject: [PATCH 03/10] fsxc: always deliver a LineKind for each line There were still some issues with line numbers when reporting errors so I discovered that in some cases we were swallowing some lines instead of generating LineKind.Normal or .Commented for them. --- fsxc/Fsxc.fs | 123 ++++++++++++++++++++++++++------------------------- 1 file changed, 63 insertions(+), 60 deletions(-) diff --git a/fsxc/Fsxc.fs b/fsxc/Fsxc.fs index fbea64b..0f234b9 100644 --- a/fsxc/Fsxc.fs +++ b/fsxc/Fsxc.fs @@ -315,7 +315,7 @@ module Program = let rec readLines (lines: seq) - (readState: ReadState) + (currentReadState: ReadState) (acc: List) : List = @@ -324,74 +324,77 @@ module Program = let rest = Seq.tail lines let newAcc, newState = - let noStateChange() = - let lineKind = - match readPreprocessorLine line with - | Some(PreProcessorElement.Action action) -> - LineKind.PreProcessorAction action - | None -> LineKind.Normal - | _ -> LineKind.Commented - - let newAcc = - { - Line = line - LineKind = lineKind - } - :: acc - - newAcc, readState - - match readState with - | IgnoreLinesUntilNextPreProcessorConditional -> - let newAcc, newState = - if line.Trim() = "#else" then - acc, - DeliverLinesUntilNextPreProcessorConditional - elif line.Trim() = "#endif" then - acc, NormalOperation + let lineKind, newState = + match readPreprocessorLine line with + | Some(PreProcessorElement.Action action) -> + if currentReadState + <> IgnoreLinesUntilNextPreProcessorConditional then + LineKind.PreProcessorAction action, + currentReadState else - { - Line = line - LineKind = LineKind.Commented - } - :: acc, - readState - - newAcc, newState - | DeliverLinesUntilNextPreProcessorConditional -> - if line.Trim() = "#else" then - acc, IgnoreLinesUntilNextPreProcessorConditional - elif line.Trim() = "#endif" then - acc, NormalOperation - else - noStateChange() - | NormalOperation -> - let trimmedLine = line.Trim() - - if trimmedLine.StartsWith "#if" - && line.Contains "LEGACY_FRAMEWORK" then - let newState = - match trimmedLine with - | "#if LEGACY_FRAMEWORK" -> + LineKind.Commented, currentReadState + | Some(PreProcessorElement.Conditional cond) -> + match cond, currentReadState with + | PreProcessorConditional.Else, + IgnoreLinesUntilNextPreProcessorConditional -> + LineKind.Commented, + DeliverLinesUntilNextPreProcessorConditional + | PreProcessorConditional.Else, + DeliverLinesUntilNextPreProcessorConditional -> + LineKind.Commented, + IgnoreLinesUntilNextPreProcessorConditional + | PreProcessorConditional.EndIf, + IgnoreLinesUntilNextPreProcessorConditional -> + LineKind.Commented, NormalOperation + | PreProcessorConditional.EndIf, + DeliverLinesUntilNextPreProcessorConditional -> + LineKind.Commented, NormalOperation + | PreProcessorConditional.EndIf, NormalOperation -> + LineKind.Normal, NormalOperation + | PreProcessorConditional.If, _ -> + if not(line.Contains "LEGACY_FRAMEWORK") then + match currentReadState with + | IgnoreLinesUntilNextPreProcessorConditional -> + LineKind.Commented, currentReadState + | _ -> LineKind.Normal, currentReadState + else + let newState = + match line.Trim() with + | "#if LEGACY_FRAMEWORK" -> #if LEGACY_FRAMEWORK - DeliverLinesUntilNextPreProcessorConditional + DeliverLinesUntilNextPreProcessorConditional #else - IgnoreLinesUntilNextPreProcessorConditional + IgnoreLinesUntilNextPreProcessorConditional #endif - | "#if !LEGACY_FRAMEWORK" -> + | "#if !LEGACY_FRAMEWORK" -> #if LEGACY_FRAMEWORK - IgnoreLinesUntilNextPreProcessorConditional + IgnoreLinesUntilNextPreProcessorConditional #else - DeliverLinesUntilNextPreProcessorConditional + DeliverLinesUntilNextPreProcessorConditional #endif - | _ -> - failwith - "Only simple ifdef statements are supported for the LEGACY_FRAMEWORK define" + | _ -> + failwith + "Only simple ifdef statements are supported for the LEGACY_FRAMEWORK define" - acc, newState - else - noStateChange() + LineKind.Commented, newState + | _, NormalOperation -> + LineKind.Normal, currentReadState + + | None -> + match currentReadState with + | IgnoreLinesUntilNextPreProcessorConditional -> + LineKind.Commented, currentReadState + | _ -> LineKind.Normal, currentReadState + + let newAcc = + { + Line = line + LineKind = lineKind + } + :: acc + + newAcc, newState readLines rest newState newAcc | None -> acc From 0c2150c24f5768adf3dae2955d792b7f7d376f80 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Wed, 4 Sep 2024 14:47:45 +0800 Subject: [PATCH 04/10] fsxc: print fs autogenerated file (debug) To be reverted temporarily, this commit just prints the F# autogenerated sources for debugging purposes. --- fsxc/Fsxc.fs | 20 +++++++++++++++++--- scripts/runTests.fsx | 3 ++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/fsxc/Fsxc.fs b/fsxc/Fsxc.fs index 0f234b9..746a8a6 100644 --- a/fsxc/Fsxc.fs +++ b/fsxc/Fsxc.fs @@ -477,7 +477,7 @@ module Program = let preprocessScriptContents (origScript: FileInfo) (contents: List) - : List = + : List * FileInfo = #if LEGACY_FRAMEWORK // from "Microsoft.Build", "16.11.0" to .../packages/Microsoft.Build.16.11.0/lib/net472/Microsoft.Build.dll @@ -718,7 +718,8 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } } ) } - |> List.ofSeq + |> List.ofSeq, + autogeneratedFile #if LEGACY_FRAMEWORK let getSourceFiles(flags: seq) : seq = @@ -849,7 +850,18 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } Console.WriteLine(sprintf "Building %s" script.FullName) let binFolder = GetBinFolderForAScript script - let compilerInputs = preprocessScriptContents script contents + + let compilerInputs, autoGenFile = + preprocessScriptContents script contents + + Console.WriteLine( + "_________________START AUTOGEN FILE_________________" + ) + + for line in File.ReadAllLines(autoGenFile.FullName) do + Console.WriteLine line + + Console.WriteLine("_________________END AUTOGEN FILE_________________") #if !LEGACY_FRAMEWORK let projectFile = generateProjectFile script contents #endif @@ -914,6 +926,8 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } | _ -> "fsharpc" #endif + Console.WriteLine(">>>>>>>>>>>>>>>COMPFLAGS: " + fscompilerflags) + let proc = Process.Execute( { diff --git a/scripts/runTests.fsx b/scripts/runTests.fsx index 5727523..a1dff96 100755 --- a/scripts/runTests.fsx +++ b/scripts/runTests.fsx @@ -347,11 +347,12 @@ let dotnetFsiCmd = Process.Execute(dotnetFsiCmd, Echo.All) |> UnwrapDefault #endif +(* let processTest = Path.Combine(TestDir.FullName, "testProcess.fsx") |> FileInfo Process.Execute(CreateCommand(processTest, String.Empty), Echo.All) |> UnwrapDefault - +*) (* this is actually only really useful for when process spits both stdout & stderr let processConcurrencyTest = Path.Combine(TestDir.FullName, "testProcessConcurrency.fsx") |> FileInfo From e883ca7faace70cae43314f82906966b0469bee2 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Thu, 5 Sep 2024 11:54:13 +0800 Subject: [PATCH 05/10] Revert "fsxc: also write comments for other preprocessors" This reverts commit a8662eea100ee62784a38c235c2fd052d07eb533 because it was actually duplicating the line (these actions were already being printed as a comment). --- fsxc/Fsxc.fs | 38 ++------------------------------------ 1 file changed, 2 insertions(+), 36 deletions(-) diff --git a/fsxc/Fsxc.fs b/fsxc/Fsxc.fs index 746a8a6..1a74b9b 100644 --- a/fsxc/Fsxc.fs +++ b/fsxc/Fsxc.fs @@ -635,21 +635,8 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } ) match action with - | PreProcessorAction.Skip -> - File.AppendAllText( - autogeneratedFile.FullName, - startCommentInFSharp - + line - + Environment.NewLine - ) + | PreProcessorAction.Skip -> () | PreProcessorAction.Load fileName -> - File.AppendAllText( - autogeneratedFile.FullName, - startCommentInFSharp - + line - + Environment.NewLine - ) - let file = FileInfo( Path.Combine( @@ -662,14 +649,6 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } #if LEGACY_FRAMEWORK | PreProcessorAction.NugetRef(nugetPkgName, version) -> - - File.AppendAllText( - autogeneratedFile.FullName, - startCommentInFSharp - + line - + Environment.NewLine - ) - let downloadedRef = nugetRefToNormalRef origScript @@ -680,22 +659,9 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } #else | PreProcessorAction.NugetRef(_nugetPkgName, _version) -> - File.AppendAllText( - autogeneratedFile.FullName, - startCommentInFSharp - + line - + Environment.NewLine - ) + () #endif | PreProcessorAction.Ref refName -> - - File.AppendAllText( - autogeneratedFile.FullName, - startCommentInFSharp - + line - + Environment.NewLine - ) - let maybeFile = FileInfo( Path.Combine( From ddc5d79cf0eca595475f322cb46d3c83df9c3f86 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Thu, 5 Sep 2024 12:43:51 +0800 Subject: [PATCH 06/10] fsxc: try to reduce fsiStub number of lines The last issue with bad line numbers reported is our need to inject this fsi stub. Let's at least make it as small as possible. --- fsxc/Fsxc.fs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/fsxc/Fsxc.fs b/fsxc/Fsxc.fs index 1a74b9b..5dddfd7 100644 --- a/fsxc/Fsxc.fs +++ b/fsxc/Fsxc.fs @@ -581,13 +581,9 @@ module Program = #endif let initialInjectedContents = - """ -module FsxScript - -type FsiStub = - { CommandLineArgs: array } + """module FsxScript +type FsiStub = { CommandLineArgs: array } let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } - """ let binFolder, autogeneratedFile = From bc3acc4727f22b5aa3654f01bad7ce38cb00e049 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Thu, 5 Sep 2024 13:11:25 +0800 Subject: [PATCH 07/10] fsxc: refactoring to avoid unnecessary tuple The bin folder could be extracted anyway from the .Directory property of the returned FileInfo instance. --- fsxc/Fsxc.fs | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/fsxc/Fsxc.fs b/fsxc/Fsxc.fs index 5dddfd7..7eb8bc0 100644 --- a/fsxc/Fsxc.fs +++ b/fsxc/Fsxc.fs @@ -222,7 +222,7 @@ module Program = let autogeneratedFile = FileInfo(Path.Combine(binDir.FullName, autogeneratedFileName)) - binDir, autogeneratedFile + autogeneratedFile let ReadScriptContents(origScript: FileInfo) : List = @@ -586,8 +586,9 @@ type FsiStub = { CommandLineArgs: array } let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } """ - let binFolder, autogeneratedFile = - GetAutoGenerationTargets origScript "fs" + let autogeneratedFile = GetAutoGenerationTargets origScript "fs" + + let binFolder = autogeneratedFile.Directory if not binFolder.Exists then Directory.CreateDirectory binFolder.FullName |> ignore @@ -707,8 +708,7 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } (origScript: FileInfo) (contents: List) : FileInfo = - let _binFolder, projectFile = - GetAutoGenerationTargets origScript "fsproj" + let projectFile = GetAutoGenerationTargets origScript "fsproj" let rec iterate(lines: List) : unit = match lines with @@ -829,7 +829,7 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } #endif let exitCode, exeTarget = - let _, exeTarget = GetAutoGenerationTargets script "exe" + let exeTarget = GetAutoGenerationTargets script "exe" #if LEGACY_FRAMEWORK let filesToCompile = getSourceFiles compilerInputs let sourceFiles = String.Join(" ", filesToCompile) @@ -947,9 +947,10 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } let GetAlreadyBuiltExecutable (exeTarget: FileInfo) - (binFolder: DirectoryInfo) (lastWriteTimeOfSourceFile: DateTime) : Option = + let binFolder = exeTarget.Directory + if not binFolder.Exists then None elif binFolder.LastWriteTime < lastWriteTimeOfSourceFile then @@ -1038,14 +1039,10 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } Build parsedArgs generateArtifacts scriptContents verbose |> ignore Environment.Exit 0 - let binFolder, exeTarget = - GetAutoGenerationTargets parsedArgs.Script "exe" + let exeTarget = GetAutoGenerationTargets parsedArgs.Script "exe" let maybeExe = - GetAlreadyBuiltExecutable - exeTarget - binFolder - lastWriteTimeOfSourceFiles + GetAlreadyBuiltExecutable exeTarget lastWriteTimeOfSourceFiles if maybeExe.IsNone then Build parsedArgs true scriptContents verbose |> ignore From ac9cfaf9615cdba19b963b1ee7767e3c9b140867 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Thu, 5 Sep 2024 14:27:30 +0800 Subject: [PATCH 08/10] fsxc: refactoring (DRY) Tiny DRY-respect improvement. --- fsxc/Fsxc.fs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/fsxc/Fsxc.fs b/fsxc/Fsxc.fs index 7eb8bc0..a2a6c3c 100644 --- a/fsxc/Fsxc.fs +++ b/fsxc/Fsxc.fs @@ -710,6 +710,17 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } : FileInfo = let projectFile = GetAutoGenerationTargets origScript "fsproj" + let addFile relativeOrAbsoluteFilePath = + let fsProjFragment = + sprintf + "" + relativeOrAbsoluteFilePath + + File.AppendAllText( + projectFile.FullName, + fsProjFragment + Environment.NewLine + ) + let rec iterate(lines: List) : unit = match lines with | head :: tail -> @@ -741,16 +752,13 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } + Environment.NewLine ) | PreProcessorAction.Load fileName -> - let fsProjFragment = + addFile( sprintf - "" + "..%c%s" Path.DirectorySeparatorChar fileName - - File.AppendAllText( - projectFile.FullName, - fsProjFragment + Environment.NewLine ) + | PreProcessorAction.Ref refName -> let fsProjFragment = if refName.ToLower().EndsWith(".dll") then @@ -797,13 +805,9 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } iterate contents - File.AppendAllText( - projectFile.FullName, - " - - " - .Replace("{userScriptFileName}", origScript.Name) - ) + addFile(sprintf "%s.fs" origScript.Name) + + File.AppendAllText(projectFile.FullName, "") projectFile #endif From 2cab8f355c2d58fc35e7edb7abdbba6abb9551bc Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Thu, 5 Sep 2024 13:43:53 +0800 Subject: [PATCH 09/10] fsxc: use external file for fsiStub This way we will not add 3 extra lines to the beginning of the file, which would make line numbers to be off in case of errors. --- fsxc/Fsxc.fs | 49 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/fsxc/Fsxc.fs b/fsxc/Fsxc.fs index a2a6c3c..80699cc 100644 --- a/fsxc/Fsxc.fs +++ b/fsxc/Fsxc.fs @@ -45,6 +45,8 @@ type ProgramInvocationType = | FsxLauncherScript | FsxcPureInvocation +type ExtraExtension = string + exception NoScriptProvided module Program = @@ -210,7 +212,7 @@ module Program = let GetBinFolderForAScript(script: FileInfo) = DirectoryInfo(Path.Combine(script.Directory.FullName, "bin")) - let GetAutoGenerationTargets (orig: FileInfo) (extension: string) = + let GetAutoGenerationTargets (orig: FileInfo) (extension: ExtraExtension) = let binDir = GetBinFolderForAScript(orig) let autogeneratedFileName = @@ -477,7 +479,7 @@ module Program = let preprocessScriptContents (origScript: FileInfo) (contents: List) - : List * FileInfo = + : List * List * FileInfo = #if LEGACY_FRAMEWORK // from "Microsoft.Build", "16.11.0" to .../packages/Microsoft.Build.16.11.0/lib/net472/Microsoft.Build.dll @@ -580,13 +582,17 @@ module Program = | Some location -> location #endif - let initialInjectedContents = - """module FsxScript + let initialFileContent = + """[] +module FsxScriptInit type FsiStub = { CommandLineArgs: array } let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } """ - let autogeneratedFile = GetAutoGenerationTargets origScript "fs" + let extraExtensionForMain: ExtraExtension = "fs" + + let autogeneratedFile = + GetAutoGenerationTargets origScript extraExtensionForMain let binFolder = autogeneratedFile.Directory @@ -597,11 +603,28 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } File.WriteAllText( autogeneratedFile.FullName, - initialInjectedContents + """module FsxScript +""" ) - seq { + let extraExtensionForInitFile: ExtraExtension = "init.fs" + + let initFile = + GetAutoGenerationTargets origScript extraExtensionForInitFile + File.WriteAllText(initFile.FullName, initialFileContent) + + let extraExtensions = + [ + extraExtensionForInitFile + extraExtensionForMain + ] + + seq { + // for !LEGACY_FRAMEWORK, this init file will be added to the fsproj file +#if LEGACY_FRAMEWORK + yield CompilerInput.SourceFile initFile +#endif let startCommentInFSharp = "// " for maybeDep in contents do @@ -682,6 +705,7 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } ) } |> List.ofSeq, + extraExtensions, autogeneratedFile #if LEGACY_FRAMEWORK @@ -707,6 +731,7 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } let generateProjectFile (origScript: FileInfo) (contents: List) + (extraExtensions: seq) : FileInfo = let projectFile = GetAutoGenerationTargets origScript "fsproj" @@ -805,7 +830,8 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } iterate contents - addFile(sprintf "%s.fs" origScript.Name) + for ext in extraExtensions do + addFile(sprintf "%s.%s" origScript.Name ext) File.AppendAllText(projectFile.FullName, "") @@ -817,7 +843,7 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } let binFolder = GetBinFolderForAScript script - let compilerInputs, autoGenFile = + let compilerInputs, extraExtensions, autoGenFile = preprocessScriptContents script contents Console.WriteLine( @@ -829,7 +855,10 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } Console.WriteLine("_________________END AUTOGEN FILE_________________") #if !LEGACY_FRAMEWORK - let projectFile = generateProjectFile script contents + let projectFile = generateProjectFile script contents extraExtensions +#else // dummy for to just prevent "unused" warning, meh + for _ext in extraExtensions do + () #endif let exitCode, exeTarget = From 0133549075723e059f15e832023c212657cb4af0 Mon Sep 17 00:00:00 2001 From: "Andres G. Aragoneses" Date: Thu, 5 Sep 2024 15:36:37 +0800 Subject: [PATCH 10/10] Revert "fsxc: print fs autogenerated file (debug)" This reverts commit 0c2150c24f5768adf3dae2955d792b7f7d376f80 because we don't need the debug output anymore. --- fsxc/Fsxc.fs | 18 +++--------------- scripts/runTests.fsx | 3 +-- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/fsxc/Fsxc.fs b/fsxc/Fsxc.fs index 80699cc..81ede61 100644 --- a/fsxc/Fsxc.fs +++ b/fsxc/Fsxc.fs @@ -479,7 +479,7 @@ module Program = let preprocessScriptContents (origScript: FileInfo) (contents: List) - : List * List * FileInfo = + : List * List = #if LEGACY_FRAMEWORK // from "Microsoft.Build", "16.11.0" to .../packages/Microsoft.Build.16.11.0/lib/net472/Microsoft.Build.dll @@ -705,8 +705,7 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } ) } |> List.ofSeq, - extraExtensions, - autogeneratedFile + extraExtensions #if LEGACY_FRAMEWORK let getSourceFiles(flags: seq) : seq = @@ -843,17 +842,8 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } let binFolder = GetBinFolderForAScript script - let compilerInputs, extraExtensions, autoGenFile = + let compilerInputs, extraExtensions = preprocessScriptContents script contents - - Console.WriteLine( - "_________________START AUTOGEN FILE_________________" - ) - - for line in File.ReadAllLines(autoGenFile.FullName) do - Console.WriteLine line - - Console.WriteLine("_________________END AUTOGEN FILE_________________") #if !LEGACY_FRAMEWORK let projectFile = generateProjectFile script contents extraExtensions #else // dummy for to just prevent "unused" warning, meh @@ -921,8 +911,6 @@ let fsi = { CommandLineArgs = System.Environment.GetCommandLineArgs() } | _ -> "fsharpc" #endif - Console.WriteLine(">>>>>>>>>>>>>>>COMPFLAGS: " + fscompilerflags) - let proc = Process.Execute( { diff --git a/scripts/runTests.fsx b/scripts/runTests.fsx index a1dff96..5727523 100755 --- a/scripts/runTests.fsx +++ b/scripts/runTests.fsx @@ -347,12 +347,11 @@ let dotnetFsiCmd = Process.Execute(dotnetFsiCmd, Echo.All) |> UnwrapDefault #endif -(* let processTest = Path.Combine(TestDir.FullName, "testProcess.fsx") |> FileInfo Process.Execute(CreateCommand(processTest, String.Empty), Echo.All) |> UnwrapDefault -*) + (* this is actually only really useful for when process spits both stdout & stderr let processConcurrencyTest = Path.Combine(TestDir.FullName, "testProcessConcurrency.fsx") |> FileInfo