navigation

Compiler Services: Virtualized File System

The FSharp.Compiler.Service component has a global variable representing the file system. By setting this variable you can host the compiler in situations where a file system is not available.

NOTE: The FSharp.Compiler.Service API is subject to change when later versions of the nuget package are published.

Setting the FileSystem

In the example below, we set the file system to an implementation which reads from disk

#r "FSharp.Compiler.Service.dll"
open System
open System.IO
open System.Collections.Generic
open System.Text
open FSharp.Compiler.AbstractIL.Internal.Library

let defaultFileSystem = Shim.FileSystem

let fileName1 = @"c:\mycode\test1.fs" // note, the path doesn't exist
let fileName2 = @"c:\mycode\test2.fs" // note, the path doesn't exist

type MyFileSystem() =
    let file1 = """
module File1

let A = 1"""
    let file2 = """
module File2
let B = File1.A + File1.A"""
    let files = dict [(fileName1, file1); (fileName2, file2)]

    interface IFileSystem with
        // Implement the service to open files for reading and writing
        member __.FileStreamReadShim(fileName) =
            match files.TryGetValue fileName with
            | true, text -> new MemoryStream(Encoding.UTF8.GetBytes(text)) :> Stream
            | _ -> defaultFileSystem.FileStreamReadShim(fileName)

        member __.FileStreamCreateShim(fileName) =
            defaultFileSystem.FileStreamCreateShim(fileName)

        member __.FileStreamWriteExistingShim(fileName) =
            defaultFileSystem.FileStreamWriteExistingShim(fileName)

        member __.ReadAllBytesShim(fileName) =
            match files.TryGetValue fileName with
            | true, text -> Encoding.UTF8.GetBytes(text)
            | _ -> defaultFileSystem.ReadAllBytesShim(fileName)

        // Implement the service related to temporary paths and file time stamps
        member __.GetTempPathShim() =
            defaultFileSystem.GetTempPathShim()

        member __.GetLastWriteTimeShim(fileName) =
            defaultFileSystem.GetLastWriteTimeShim(fileName)

        member __.GetFullPathShim(fileName) =
            defaultFileSystem.GetFullPathShim(fileName)

        member __.IsInvalidPathShim(fileName) =
            defaultFileSystem.IsInvalidPathShim(fileName)

        member __.IsPathRootedShim(fileName) =
            defaultFileSystem.IsPathRootedShim(fileName)

        member __.IsStableFileHeuristic(fileName) =
            defaultFileSystem.IsStableFileHeuristic(fileName)

        // Implement the service related to file existence and deletion
        member __.SafeExists(fileName) =
            files.ContainsKey(fileName) || defaultFileSystem.SafeExists(fileName)

        member __.FileDelete(fileName) =
            defaultFileSystem.FileDelete(fileName)

        // Implement the service related to assembly loading, used to load type providers
        // and for F# interactive.
        member __.AssemblyLoadFrom(fileName) =
            defaultFileSystem.AssemblyLoadFrom fileName

        member __.AssemblyLoad(assemblyName) =
            defaultFileSystem.AssemblyLoad assemblyName

let myFileSystem = MyFileSystem()
Shim.FileSystem <- MyFileSystem()

Doing a compilation with the FileSystem

open FSharp.Compiler.SourceCodeServices

let checker = FSharpChecker.Create()

let projectOptions =
    let sysLib nm =
        if System.Environment.OSVersion.Platform = System.PlatformID.Win32NT then // file references only valid on Windows
            System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86) +
            @"\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\" + nm + ".dll"
        else
            let sysDir = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()
            let (++) a b = System.IO.Path.Combine(a,b)
            sysDir ++ nm + ".dll"

    let fsCore4300() =
        if System.Environment.OSVersion.Platform = System.PlatformID.Win32NT then // file references only valid on Windows
            System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFilesX86) +
            @"\Reference Assemblies\Microsoft\FSharp\.NETFramework\v4.0\4.3.0.0\FSharp.Core.dll"
        else
            sysLib "FSharp.Core"

    let allFlags =
        [| yield "--simpleresolution";
           yield "--noframework";
           yield "--debug:full";
           yield "--define:DEBUG";
           yield "--optimize-";
           yield "--doc:test.xml";
           yield "--warn:3";
           yield "--fullpaths";
           yield "--flaterrors";
           yield "--target:library";
           let references =
             [ sysLib "mscorlib"
               sysLib "System"
               sysLib "System.Core"
               fsCore4300() ]
           for r in references do
                 yield "-r:" + r |]

    { ProjectFileName = @"c:\mycode\compilation.fsproj" // Make a name that is unique in this directory.
      ProjectId = None
      SourceFiles = [| fileName1; fileName2 |]
      OriginalLoadReferences = []
      ExtraProjectInfo=None
      Stamp = None
      OtherOptions = allFlags
      ReferencedProjects = [| |]
      IsIncompleteTypeCheckEnvironment = false
      UseScriptResolutionRules = true
      LoadTime = System.DateTime.Now // Note using 'Now' forces reloading
      UnresolvedReferences = None }

let results = checker.ParseAndCheckProject(projectOptions) |> Async.RunSynchronously

results.Errors
results.AssemblySignature.Entities.Count //2
results.AssemblySignature.Entities.[0].MembersFunctionsAndValues.Count //1
results.AssemblySignature.Entities.[0].MembersFunctionsAndValues.[0].DisplayName // "B"

Summary

In this tutorial, we've seen how to globally customize the view of the file system used by the FSharp.Compiler.Service component.

At the time of writing, the following System.IO operations are not considered part of the virtualized file system API. Future iterations on the compiler service implementation may add these to the API.

  • Path.Combine
  • Path.DirectorySeparatorChar
  • Path.GetDirectoryName
  • Path.GetFileName
  • Path.GetFileNameWithoutExtension
  • Path.HasExtension
  • Path.GetRandomFileName (used only in generation compiled win32 resources in assemblies)

NOTE: Several operations in the SourceCodeServices API accept the contents of a file to parse or check as a parameter, in addition to a file name. In these cases, the file name is only used for error reporting.

NOTE: Type provider components do not use the virtualized file system.

NOTE: The compiler service may use MSBuild for assembly resolutions unless --simpleresolution is provided. When using the FileSystem API you will normally want to specify --simpleresolution as one of your compiler flags. Also specify --noframework. You will need to supply explicit resolutions of all referenced .NET assemblies.

namespace System
namespace System.IO
namespace System.Collections
namespace System.Collections.Generic
namespace System.Text
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
namespace FSharp.Compiler
namespace FSharp.Compiler.AbstractIL
namespace FSharp.Compiler.AbstractIL.Internal
module Library

from FSharp.Compiler.AbstractIL.Internal
val defaultFileSystem : IFileSystem
module Shim

from FSharp.Compiler.AbstractIL.Internal.Library
val mutable FileSystem : IFileSystem
val fileName1 : string
val fileName2 : string
Multiple items
type MyFileSystem =
  interface IFileSystem
  new : unit -> MyFileSystem

--------------------
new : unit -> MyFileSystem
val file1 : string
val file2 : string
val files : IDictionary<string,string>
val dict : keyValuePairs:seq<'Key * 'Value> -> IDictionary<'Key,'Value> (requires equality)
type IFileSystem =
  interface
    abstract member AssemblyLoad : assemblyName:AssemblyName -> Assembly
    abstract member AssemblyLoadFrom : fileName:string -> Assembly
    abstract member FileDelete : fileName:string -> unit
    abstract member FileStreamCreateShim : fileName:string -> Stream
    abstract member FileStreamReadShim : fileName:string -> Stream
    abstract member FileStreamWriteExistingShim : fileName:string -> Stream
    abstract member GetFullPathShim : fileName:string -> string
    abstract member GetLastWriteTimeShim : fileName:string -> DateTime
    abstract member GetTempPathShim : unit -> string
    abstract member IsInvalidPathShim : filename:string -> bool
    ...
  end
val fileName : string
val text : string
Multiple items
type MemoryStream =
  inherit Stream
  new : unit -> MemoryStream + 6 overloads
  member CanRead : bool
  member CanSeek : bool
  member CanWrite : bool
  member Capacity : int with get, set
  member CopyTo : destination:Stream * bufferSize:int -> unit
  member CopyToAsync : destination:Stream * bufferSize:int * cancellationToken:CancellationToken -> Task
  member Flush : unit -> unit
  member FlushAsync : cancellationToken:CancellationToken -> Task
  member GetBuffer : unit -> byte[]
  ...

--------------------
MemoryStream() : MemoryStream
MemoryStream(capacity: int) : MemoryStream
MemoryStream(buffer: byte []) : MemoryStream
MemoryStream(buffer: byte [], writable: bool) : MemoryStream
MemoryStream(buffer: byte [], index: int, count: int) : MemoryStream
MemoryStream(buffer: byte [], index: int, count: int, writable: bool) : MemoryStream
MemoryStream(buffer: byte [], index: int, count: int, writable: bool, publiclyVisible: bool) : MemoryStream
type Encoding =
  member BodyName : string
  member Clone : unit -> obj
  member CodePage : int
  member DecoderFallback : DecoderFallback with get, set
  member EncoderFallback : EncoderFallback with get, set
  member EncodingName : string
  member Equals : value:obj -> bool
  member GetByteCount : chars:char[] -> int + 5 overloads
  member GetBytes : chars:char[] -> byte[] + 7 overloads
  member GetCharCount : bytes:byte[] -> int + 3 overloads
  ...
property Encoding.UTF8: Encoding with get
type Stream =
  inherit MarshalByRefObject
  member BeginRead : buffer:byte[] * offset:int * count:int * callback:AsyncCallback * state:obj -> IAsyncResult
  member BeginWrite : buffer:byte[] * offset:int * count:int * callback:AsyncCallback * state:obj -> IAsyncResult
  member CanRead : bool
  member CanSeek : bool
  member CanTimeout : bool
  member CanWrite : bool
  member Close : unit -> unit
  member CopyTo : destination:Stream -> unit + 1 overload
  member CopyToAsync : destination:Stream -> Task + 3 overloads
  member Dispose : unit -> unit
  ...
val __ : MyFileSystem
val assemblyName : Reflection.AssemblyName
val myFileSystem : MyFileSystem
namespace FSharp.Compiler.SourceCodeServices
val checker : FSharpChecker
type FSharpChecker =
  member CheckFileInProject : parsed:FSharpParseFileResults * filename:string * fileversion:int * sourceText:ISourceText * options:FSharpProjectOptions * ?textSnapshotInfo:obj * ?userOpName:string -> Async<FSharpCheckFileAnswer>
  member CheckProjectInBackground : options:FSharpProjectOptions * ?userOpName:string -> unit
  member ClearLanguageServiceRootCachesAndCollectAndFinalizeAllTransients : unit -> unit
  member Compile : argv:string [] * ?userOpName:string -> Async<FSharpErrorInfo [] * int>
  member Compile : ast:ParsedInput list * assemblyName:string * outFile:string * dependencies:string list * ?pdbFile:string * ?executable:bool * ?noframework:bool * ?userOpName:string -> Async<FSharpErrorInfo [] * int>
  member CompileToDynamicAssembly : otherFlags:string [] * execute:(TextWriter * TextWriter) option * ?userOpName:string -> Async<FSharpErrorInfo [] * int * Assembly option>
  member CompileToDynamicAssembly : ast:ParsedInput list * assemblyName:string * dependencies:string list * execute:(TextWriter * TextWriter) option * ?debug:bool * ?noframework:bool * ?userOpName:string -> Async<FSharpErrorInfo [] * int * Assembly option>
  member FindBackgroundReferencesInFile : filename:string * options:FSharpProjectOptions * symbol:FSharpSymbol * ?userOpName:string -> Async<seq<range>>
  member GetBackgroundCheckResultsForFileInProject : filename:string * options:FSharpProjectOptions * ?userOpName:string -> Async<FSharpParseFileResults * FSharpCheckFileResults>
  member GetBackgroundParseResultsForFileInProject : filename:string * options:FSharpProjectOptions * ?userOpName:string -> Async<FSharpParseFileResults>
  ...
static member FSharpChecker.Create : ?projectCacheSize:int * ?keepAssemblyContents:bool * ?keepAllBackgroundResolutions:bool * ?legacyReferenceResolver:FSharp.Compiler.ReferenceResolver.Resolver * ?tryGetMetadataSnapshot:FSharp.Compiler.AbstractIL.ILBinaryReader.ILReaderTryGetMetadataSnapshot * ?suggestNamesForErrors:bool * ?keepAllBackgroundSymbolUses:bool * ?enableBackgroundItemKeyStoreAndSemanticClassification:bool -> FSharpChecker
val projectOptions : FSharpProjectOptions
val sysLib : (string -> string)
val nm : string
type Environment =
  static member CommandLine : string
  static member CurrentDirectory : string with get, set
  static member CurrentManagedThreadId : int
  static member Exit : exitCode:int -> unit
  static member ExitCode : int with get, set
  static member ExpandEnvironmentVariables : name:string -> string
  static member FailFast : message:string -> unit + 2 overloads
  static member GetCommandLineArgs : unit -> string[]
  static member GetEnvironmentVariable : variable:string -> string + 1 overload
  static member GetEnvironmentVariables : unit -> IDictionary + 1 overload
  ...
  nested type SpecialFolder
  nested type SpecialFolderOption
property Environment.OSVersion: OperatingSystem with get
type PlatformID =
  | Win32S = 0
  | Win32Windows = 1
  | Win32NT = 2
  | WinCE = 3
  | Unix = 4
  | Xbox = 5
  | MacOSX = 6
field PlatformID.Win32NT: PlatformID = 2
Environment.GetFolderPath(folder: Environment.SpecialFolder) : string
Environment.GetFolderPath(folder: Environment.SpecialFolder, option: Environment.SpecialFolderOption) : string
type SpecialFolder =
  | ApplicationData = 26
  | CommonApplicationData = 35
  | LocalApplicationData = 28
  | Cookies = 33
  | Desktop = 0
  | Favorites = 6
  | History = 34
  | InternetCache = 32
  | Programs = 2
  | MyComputer = 17
  ...
field Environment.SpecialFolder.ProgramFilesX86: Environment.SpecialFolder = 42
val sysDir : string
namespace System.Runtime
namespace System.Runtime.InteropServices
type RuntimeEnvironment =
  static member FromGlobalAccessCache : a:Assembly -> bool
  static member GetRuntimeDirectory : unit -> string
  static member GetRuntimeInterfaceAsIntPtr : clsid:Guid * riid:Guid -> nativeint
  static member GetRuntimeInterfaceAsObject : clsid:Guid * riid:Guid -> obj
  static member GetSystemVersion : unit -> string
  static member SystemConfigurationFile : string
Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() : string
val a : string
val b : string
type Path =
  static val DirectorySeparatorChar : char
  static val AltDirectorySeparatorChar : char
  static val VolumeSeparatorChar : char
  static val PathSeparator : char
  static val InvalidPathChars : char[]
  static member ChangeExtension : path:string * extension:string -> string
  static member Combine : [<ParamArray>] paths:string[] -> string + 3 overloads
  static member EndsInDirectorySeparator : path:ReadOnlySpan<char> -> bool + 1 overload
  static member GetDirectoryName : path:string -> string + 1 overload
  static member GetExtension : path:string -> string + 1 overload
  ...
Path.Combine([<ParamArray>] paths: string []) : string
Path.Combine(path1: string, path2: string) : string
Path.Combine(path1: string, path2: string, path3: string) : string
Path.Combine(path1: string, path2: string, path3: string, path4: string) : string
val fsCore4300 : (unit -> string)
val allFlags : string []
val references : string list
val r : string
union case Option.None: Option<'T>
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

--------------------
DateTime ()
   (+0 other overloads)
DateTime(ticks: int64) : DateTime
   (+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : DateTime
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : DateTime
   (+0 other overloads)
property DateTime.Now: DateTime with get
val results : FSharpCheckProjectResults
Multiple items
type Async =
  static member AsBeginEnd : computation:('Arg -> Async<'T>) -> ('Arg * AsyncCallback * obj -> IAsyncResult) * (IAsyncResult -> 'T) * (IAsyncResult -> unit)
  static member AwaitEvent : event:IEvent<'Del,'T> * ?cancelAction:(unit -> unit) -> Async<'T> (requires delegate and 'Del :> Delegate)
  static member AwaitIAsyncResult : iar:IAsyncResult * ?millisecondsTimeout:int -> Async<bool>
  static member AwaitTask : task:Task -> Async<unit>
  static member AwaitTask : task:Task<'T> -> Async<'T>
  static member AwaitWaitHandle : waitHandle:WaitHandle * ?millisecondsTimeout:int -> Async<bool>
  static member CancelDefaultToken : unit -> unit
  static member Catch : computation:Async<'T> -> Async<Choice<'T,exn>>
  static member Choice : computations:seq<Async<'T option>> -> Async<'T option>
  static member FromBeginEnd : beginAction:(AsyncCallback * obj -> IAsyncResult) * endAction:(IAsyncResult -> 'T) * ?cancelAction:(unit -> unit) -> Async<'T>
  ...

--------------------
type Async<'T> =
static member Async.RunSynchronously : computation:Async<'T> * ?timeout:int * ?cancellationToken:Threading.CancellationToken -> 'T