Skip to content

Commit

Permalink
Merge pull request #3704 from ncave/rust
Browse files Browse the repository at this point in the history
No writing to stdout from Fable.Transforms
  • Loading branch information
ncave authored Jan 22, 2024
2 parents 0ee2bb6 + 0e6b091 commit 686e806
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 48 deletions.
18 changes: 10 additions & 8 deletions src/Fable.Cli/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ module private Util =
let stack = innerStack ex
$"[ERROR] %s{file}{Log.newLine}%s{ex.Message}{Log.newLine}%s{stack}"

let formatLog rootDir (log: Log) =
let formatLog rootDir (log: LogEntry) =
match log.FileName with
| None -> log.Message
| Some file ->
Expand All @@ -120,7 +120,7 @@ module private Util =
| None ->
$"%s{file}(1,1): %s{severity} %s{log.Tag}: %s{log.Message}"

let logErrors rootDir (logs: Log seq) =
let logErrors rootDir (logs: LogEntry seq) =
logs
|> Seq.filter (fun log -> log.Severity = Severity.Error)
|> Seq.iter (fun log ->
Expand Down Expand Up @@ -153,7 +153,7 @@ module private Util =

let msg = $"%s{er.Message} (code %i{er.ErrorNumber})"

Log.Make(
LogEntry.Make(
severity,
msg,
fileName = er.FileName,
Expand Down Expand Up @@ -529,7 +529,7 @@ type FableCompileResult =
Result<{|
File: string
OutPath: string
Logs: Log[]
Logs: LogEntry[]
InlineExprs: (string * InlineExpr)[]
WatchDependencies: string[]
|}, {|
Expand All @@ -538,7 +538,8 @@ type FableCompileResult =
|}>

type ReplyChannel =
AsyncReplyChannel<Result< (* fsharpLogs *) Log[] * FableCompileResult list, exn>>
AsyncReplyChannel<Result< (* fsharpLogs *) LogEntry[] *
FableCompileResult list, exn>>

type FableCompilerMsg =
| GetFableProject of replyChannel: AsyncReplyChannel<Project>
Expand All @@ -565,7 +566,7 @@ type FableCompilerState =
FilesCheckedButNotCompiled: Set<string>
FableFilesToCompileExpectedCount: int
FableFilesCompiledCount: int
FSharpLogs: Log[]
FSharpLogs: LogEntry[]
FableResults: FableCompileResult list
HasFSharpCompilationFinished: bool
ReplyChannel: ReplyChannel option
Expand Down Expand Up @@ -915,6 +916,7 @@ and FableCompiler
projCracked.ProjectOptions.SourceFiles,
[],
assemblies,
Log.log,
?precompiledInfo =
(projCracked.PrecompiledInfo
|> Option.map (fun i -> i :> _)),
Expand Down Expand Up @@ -1330,12 +1332,12 @@ let private compilationCycle (state: State) (changes: ISet<string>) =
let log =
match e.Exception with
| Fable.FableError msg ->
Log.MakeError(msg, fileName = e.File)
LogEntry.MakeError(msg, fileName = e.File)
| ex ->
let msg =
ex.Message + Log.newLine + ex.StackTrace

Log.MakeError(
LogEntry.MakeError(
msg,
fileName = e.File,
tag = "EXCEPTION"
Expand Down
8 changes: 5 additions & 3 deletions src/Fable.Compiler/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ open System.IO
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.SourceCodeServices
open Fable
open Fable.Compiler.Util
open Fable.Transforms.State
open Fable.Transforms
open Fable.Compiler.ProjectCracker
open Fable.Compiler.Util

type BabelWriter
(
Expand Down Expand Up @@ -154,7 +154,8 @@ let compileProjectToJavaScript
cliArgs.ProjectFile,
crackerResponse.ProjectOptions.SourceFiles,
checkProjectResult.AssemblyContents.ImplementationFiles,
assemblies
assemblies,
Log.log
// ?precompiledInfo =
// (projCracked.PrecompiledInfo |> Option.map (fun i -> i :> _)),
// getPlugin = loadType projCracked.CliArgs
Expand Down Expand Up @@ -233,7 +234,8 @@ let compileFileToJavaScript
cliArgs.ProjectFile,
crackerResponse.ProjectOptions.SourceFiles,
checkProjectResult.AssemblyContents.ImplementationFiles,
assemblies
assemblies,
Log.log
)

let opts = cliArgs.CompilerOptions
Expand Down
12 changes: 12 additions & 0 deletions src/Fable.Compiler/Util.fs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ module Log =
Console.Error.WriteLine(msg)
Console.ResetColor()

let info (msg: string) =
if canLog msg then
Console.ForegroundColor <- ConsoleColor.Gray
Console.Out.WriteLine(msg)
Console.ResetColor()

let log (sev: Fable.Severity) (msg: string) =
match sev with
| Fable.Severity.Info -> info msg
| Fable.Severity.Warning -> warning msg
| Fable.Severity.Error -> error msg

let mutable private femtoMsgShown = false

let showFemtoMsg (show: unit -> bool) : unit =
Expand Down
2 changes: 2 additions & 0 deletions src/Fable.Compiler/Util.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ module Log =
val verbose: msg: Lazy<string> -> unit
val warning: msg: string -> unit
val error: msg: string -> unit
val info: msg: string -> unit
val log: sev: Fable.Severity -> msg: string -> unit
val showFemtoMsg: show: (unit -> bool) -> unit

module File =
Expand Down
8 changes: 3 additions & 5 deletions src/Fable.Transforms/Rust/AST/Rust.AST.Adapters.fs
Original file line number Diff line number Diff line change
Expand Up @@ -248,12 +248,10 @@ type Macros =

static member write(buf: String, fmt: string, [<System.ParamArray>] args) =
buf.push_str (Macros.format (fmt, args))
#if false //DEBUG
static member debug(fmt: string, [<System.ParamArray>] args) =
System.Console.WriteLine(fmt, args)
#else

// static member debug(fmt: string, [<System.ParamArray>] args) =
// System.Console.WriteLine(fmt, args)
static member debug _ = ()
#endif

[<AutoOpen>]
module ArrayHelpers =
Expand Down
41 changes: 17 additions & 24 deletions src/Fable.Transforms/State.fs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ type PluginRef =
TypeFullName: string
}

type Assemblies(getPlugin, fsharpAssemblies: FSharpAssembly list) =
type Assemblies
(
getPlugin,
fsharpAssemblies: FSharpAssembly list,
addLog: Severity -> string -> unit
)
=
let assemblies = Dictionary()
let coreAssemblies = Dictionary()
let entities = ConcurrentDictionary()
Expand Down Expand Up @@ -46,13 +52,7 @@ type Assemblies(getPlugin, fsharpAssemblies: FSharpAssembly list) =
let errorMessage =
$"Could not scan {path} for Fable plugins, skipping this assembly"

#if !FABLE_COMPILER
Console.ForegroundColor <- ConsoleColor.Gray
#endif
Console.WriteLine(errorMessage)
#if !FABLE_COMPILER
Console.ResetColor()
#endif
addLog Severity.Info errorMessage

hasSkippedAssembly <- true
false
Expand Down Expand Up @@ -83,20 +83,12 @@ type Assemblies(getPlugin, fsharpAssemblies: FSharpAssembly list) =
$"Error while loading plugin: {e.FullName}"
""
"This error often happens if you are trying to use a plugin that is not compatible with the current version of Fable."

"If you see this error please open an issue at https://github.com/fable-compiler/Fable/"
"so we can check if we can improve the plugin detection mechanism."
]
|> String.concat "\n"
|> String.concat Environment.NewLine

#if !FABLE_COMPILER
Console.ForegroundColor <-
ConsoleColor.DarkRed
#endif
Console.WriteLine(errorMessage)
#if !FABLE_COMPILER
Console.ResetColor()
#endif
addLog Severity.Error errorMessage

raise ex

Expand All @@ -105,7 +97,7 @@ type Assemblies(getPlugin, fsharpAssemblies: FSharpAssembly list) =

// Add a blank line to separate the error message from the rest of the output
if hasSkippedAssembly then
Console.WriteLine()
addLog Severity.Info "" // or Environment.NewLine

({ MemberDeclarationPlugins = Map.empty }, plugins)
||> Seq.fold (fun acc kv ->
Expand Down Expand Up @@ -219,6 +211,7 @@ type Project
sourceFiles: string[],
fsharpFiles: FSharpImplementationFileContents list,
fsharpAssemblies: FSharpAssembly list,
addLog: Severity -> string -> unit,
?getPlugin: PluginRef -> System.Type,
?precompiledInfo: PrecompiledInfo
)
Expand All @@ -227,7 +220,7 @@ type Project
let getPlugin =
defaultArg getPlugin (fun _ -> failwith "Plugins are not supported")

let assemblies = Assemblies(getPlugin, fsharpAssemblies)
let assemblies = Assemblies(getPlugin, fsharpAssemblies, addLog)

let implFilesMap =
fsharpFiles
Expand Down Expand Up @@ -283,7 +276,7 @@ type Project
member _.Assemblies = assemblies
member _.PrecompiledInfo = precompiledInfo

type Log =
type LogEntry =
{
Message: string
Tag: string
Expand All @@ -302,7 +295,7 @@ type Log =
}

static member MakeError(msg, ?fileName, ?range, ?tag) =
Log.Make(
LogEntry.Make(
Severity.Error,
msg,
?fileName = fileName,
Expand All @@ -321,7 +314,7 @@ type CompilerImpl
?outType: OutputType,
?outDir: string,
?watchDependencies: HashSet<string>,
?logs: ResizeArray<Log>,
?logs: ResizeArray<LogEntry>,
?isPrecompilingInlineFunction: bool
)
=
Expand Down Expand Up @@ -456,7 +449,7 @@ type CompilerImpl
?tag: string
)
=
Log.Make(
LogEntry.Make(
severity,
msg,
?range = range,
Expand Down
7 changes: 5 additions & 2 deletions src/fable-standalone/src/Main.fs
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,14 @@ let makeProject
else
checkResults.AssemblyContents.ImplementationFiles

let addLog (severity: Fable.Severity) (msg: string) = () //TODO: log loading errors

Project.From(
projectOptions.ProjectFileName,
projectOptions.SourceFiles,
implFiles,
checkResults.ProjectContext.GetReferencedAssemblies()
checkResults.ProjectContext.GetReferencedAssemblies(),
addLog
)

let parseAndCheckProject
Expand Down Expand Up @@ -339,7 +342,7 @@ let getCompletionsAtLocation
)
| None -> [||]

let mapFableError (com: Compiler) (log: Log) =
let mapFableError (com: Compiler) (log: LogEntry) =
let r = defaultArg log.Range Fable.AST.SourceLocation.Empty

{
Expand Down
12 changes: 6 additions & 6 deletions tests/Integration/Compiler/Util/Compiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ open Fable.Compiler.Util

module Compiler =

type Result = Log list
type Result = LogEntry list
module Result =
let errors = List.filter (fun (m: Log) -> m.Severity = Severity.Error)
let warnings = List.filter (fun (m: Log) -> m.Severity = Severity.Warning)
let errors = List.filter (fun (m: LogEntry) -> m.Severity = Severity.Error)
let warnings = List.filter (fun (m: LogEntry) -> m.Severity = Severity.Warning)
let wasFailure = errors >> List.isEmpty >> not

type Settings = {
Expand Down Expand Up @@ -123,8 +123,8 @@ module Compiler =

module private Test =
module Is =
let error (msg: Log) = msg.Severity = Severity.Error
let warning (msg: Log) = msg.Severity = Severity.Warning
let error (msg: LogEntry) = msg.Severity = Severity.Error
let warning (msg: LogEntry) = msg.Severity = Severity.Warning
let single = function | [_] -> true | _ -> false
let zero = List.isEmpty
let count n = List.length >> (=) n
Expand All @@ -142,7 +142,7 @@ module Compiler =
let withMsg (txt: string) = List.exists (fun m -> m.Message.Contains txt)
module Text =
let contains (txt: string) msg = msg.Message.Contains(txt, StringComparison.InvariantCultureIgnoreCase)
let isMatch (regex: System.Text.RegularExpressions.Regex) (msg: Log) =
let isMatch (regex: System.Text.RegularExpressions.Regex) (msg: LogEntry) =
regex.IsMatch msg.Message
let private (>&>) a b = fun actual -> a actual && b actual

Expand Down

0 comments on commit 686e806

Please sign in to comment.