-
Notifications
You must be signed in to change notification settings - Fork 155
/
CompilerServiceInterface.fs
406 lines (323 loc) · 13.8 KB
/
CompilerServiceInterface.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
namespace FsAutoComplete
open System.IO
open FSharp.Compiler.CodeAnalysis
open Utils
open FSharp.Compiler.Text
open FsAutoComplete.Logging
open Ionide.ProjInfo.ProjectSystem
open FSharp.UMX
open FSharp.Compiler.EditorServices
open FSharp.Compiler.Symbols
type Version = int
type FSharpCompilerServiceChecker(hasAnalyzers) =
let checker =
FSharpChecker.Create(
projectCacheSize = 200,
keepAllBackgroundResolutions = true,
keepAssemblyContents = hasAnalyzers,
suggestNamesForErrors = true,
enablePartialTypeChecking = not hasAnalyzers,
enableBackgroundItemKeyStoreAndSemanticClassification = true
)
// we only want to let people hook onto the underlying checker event if there's not a background service actually compiling things for us
let safeFileCheckedEvent = checker.FileChecked
// /// FCS only accepts absolute file paths, so this ensures that by
// /// rooting relative paths onto HOME on *nix and %HOMRDRIVE%%HOMEPATH% on windows
// let ensureAbsolutePath path =
// if (try Path.GetFullPath path |> ignore; true with _ -> false) then path
// else
// match Environment.OSVersion.Platform with
// | PlatformID.Unix
// | PlatformID.MacOSX -> Environment.GetEnvironmentVariable "HOME"
// | _ -> Environment.ExpandEnvironmentVariables "%HOMEDRIVE%%HOMEPATH%"
// </> Path.GetFileName path
let entityCache = EntityCache()
let sdkRefsLogger = LogProvider.getLoggerByName "SdkRefs"
let checkerLogger = LogProvider.getLoggerByName "Checker"
let optsLogger = LogProvider.getLoggerByName "Opts"
/// the root path to the dotnet sdk installations, eg /usr/local/share/dotnet
let mutable sdkRoot: DirectoryInfo option = None
let mutable sdkFsharpCore: FileInfo option = None
let mutable sdkFsiAuxLib: FileInfo option = None
/// additional arguments that are added to typechecking of scripts
let mutable fsiAdditionalArguments = Array.empty
let mutable fsiAdditionalFiles = Array.empty
/// This event is raised when any data that impacts script typechecking
/// is changed. This can potentially invalidate existing project options
/// so we must purge any typecheck results for scripts.
let scriptTypecheckRequirementsChanged = Event<_>()
let mutable disableInMemoryProjectReferences = false
let fixupFsharpCoreAndFSIPaths (p: FSharpProjectOptions) =
match sdkFsharpCore, sdkFsiAuxLib with
| None, _
| _, None -> p
| Some fsc, Some fsi ->
let toReplace, otherOpts =
p.OtherOptions
|> Array.partition (fun opt ->
opt.EndsWith "FSharp.Core.dll"
|| opt.EndsWith "FSharp.Compiler.Interactive.Settings.dll")
{ p with OtherOptions = Array.append otherOpts [| $"-r:%s{fsc.FullName}"; $"-r:%s{fsi.FullName}" |] }
let (|StartsWith|_|) (prefix: string) (s: string) =
if s.StartsWith(prefix) then
Some(s.[prefix.Length ..])
else
None
let processFSIArgs args =
(([||], [||]), args)
||> Array.fold (fun (args, files) arg ->
match arg with
| StartsWith "--use:" file
| StartsWith "--load:" file -> args, Array.append files [| file |]
| arg -> Array.append args [| arg |], files)
let clearProjectReferences (opts: FSharpProjectOptions) =
if disableInMemoryProjectReferences then
{ opts with ReferencedProjects = [||] }
else
opts
let filterBadRuntimeRefs =
let badRefs =
[ "System.Private"
"System.Runtime.WindowsRuntime"
"System.Runtime.WindowsRuntime.UI.Xaml"
"mscorlib" ]
|> List.map (fun p -> p + ".dll")
let containsBadRef (s: string) =
badRefs |> List.exists (fun r -> s.EndsWith r)
fun (projOptions: FSharpProjectOptions) ->
{ projOptions with OtherOptions = projOptions.OtherOptions |> Array.where (containsBadRef >> not) }
/// ensures that any user-configured include/load files are added to the typechecking context
let addLoadedFiles (projectOptions: FSharpProjectOptions) =
let files = Array.append fsiAdditionalFiles projectOptions.SourceFiles
optsLogger.info (
Log.setMessage "Source file list is {files}"
>> Log.addContextDestructured "files" files
)
{ projectOptions with SourceFiles = files }
let (|Reference|_|) (opt: string) =
if opt.StartsWith "-r:" then Some(opt.[3..]) else None
/// ensures that all file paths are absolute before being sent to the compiler, because compilation of scripts fails with relative paths
let resolveRelativeFilePaths (projectOptions: FSharpProjectOptions) =
{ projectOptions with
SourceFiles = projectOptions.SourceFiles |> Array.map Path.GetFullPath
OtherOptions =
projectOptions.OtherOptions
|> Array.map (fun opt ->
match opt with
| Reference r -> $"-r:{Path.GetFullPath r}"
| opt -> opt) }
member __.DisableInMemoryProjectReferences
with get () = disableInMemoryProjectReferences
and set (value) = disableInMemoryProjectReferences <- value
member __.GetDependingProjects (file: string<LocalPath>) (options: seq<string * FSharpProjectOptions>) =
let project =
options
|> Seq.tryFind (fun (k, _) -> (UMX.untag k).ToUpperInvariant() = (UMX.untag file).ToUpperInvariant())
project
|> Option.map (fun (_, option) ->
option,
[ yield!
options
|> Seq.map snd
|> Seq.distinctBy (fun o -> o.ProjectFileName)
|> Seq.filter (fun o ->
o.ReferencedProjects
|> Array.map (fun p -> Path.GetFullPath p.OutputFile)
|> Array.contains option.ProjectFileName) ])
member private __.GetNetFxScriptOptions(file: string<LocalPath>, source) =
async {
optsLogger.info (
Log.setMessage "Getting NetFX options for script file {file}"
>> Log.addContextDestructured "file" file
)
let allFlags = Array.append [| "--targetprofile:mscorlib" |] fsiAdditionalArguments
let! (opts, errors) =
checker.GetProjectOptionsFromScript(
UMX.untag file,
source,
assumeDotNetFramework = true,
useFsiAuxLib = true,
otherFlags = allFlags,
userOpName = "getNetFrameworkScriptOptions"
)
let allModifications = addLoadedFiles >> resolveRelativeFilePaths
return allModifications opts, errors
}
member private __.GetNetCoreScriptOptions(file: string<LocalPath>, source) =
async {
optsLogger.info (
Log.setMessage "Getting NetCore options for script file {file}"
>> Log.addContextDestructured "file" file
)
let allFlags =
Array.append [| "--targetprofile:netstandard" |] fsiAdditionalArguments
let! (opts, errors) =
checker.GetProjectOptionsFromScript(
UMX.untag file,
source,
assumeDotNetFramework = false,
useSdkRefs = true,
useFsiAuxLib = true,
otherFlags = allFlags,
userOpName = "getNetCoreScriptOptions"
)
optsLogger.trace (
Log.setMessage "Got NetCore options {opts} for file {file} with errors {errors}"
>> Log.addContextDestructured "file" file
>> Log.addContextDestructured "opts" opts
>> Log.addContextDestructured "errors" errors
)
let allModifications =
// filterBadRuntimeRefs >>
addLoadedFiles >> resolveRelativeFilePaths >> fixupFsharpCoreAndFSIPaths
let modified = allModifications opts
optsLogger.trace (
Log.setMessage "Replaced options to {opts}"
>> Log.addContextDestructured "opts" modified
)
return modified, errors
}
member self.GetProjectOptionsFromScript(file: string<LocalPath>, source, tfm) =
async {
let! (projOptions, errors) =
match tfm with
| FSIRefs.TFM.NetFx -> self.GetNetFxScriptOptions(file, source)
| FSIRefs.TFM.NetCore -> self.GetNetCoreScriptOptions(file, source)
match errors with
| [] -> ()
| errs ->
optsLogger.info (
Log.setLogLevel LogLevel.Error
>> Log.setMessage "Resolved {opts} with {errors}"
>> Log.addContextDestructured "opts" projOptions
>> Log.addContextDestructured "errors" errs
)
return projOptions
}
member __.GetBackgroundCheckResultsForFileInProject(fn: string<LocalPath>, opt) =
checkerLogger.info (
Log.setMessage "GetBackgroundCheckResultsForFileInProject - {file}"
>> Log.addContextDestructured "file" fn
)
let opt = clearProjectReferences opt
checker.GetBackgroundCheckResultsForFileInProject(UMX.untag fn, opt)
|> Async.map (fun (pr, cr) -> ParseAndCheckResults(pr, cr, entityCache))
member __.FileChecked: IEvent<string<LocalPath> * FSharpProjectOptions> =
safeFileCheckedEvent
|> Event.map (fun (fileName, blob) -> UMX.tag fileName, blob) //path comes from the compiler, so it's safe to assume the tag in this case
member __.ScriptTypecheckRequirementsChanged =
scriptTypecheckRequirementsChanged.Publish
member __.ParseFile(fn: string<LocalPath>, source, fpo) =
checkerLogger.info (Log.setMessage "ParseFile - {file}" >> Log.addContextDestructured "file" fn)
let path = UMX.untag fn
checker.ParseFile(path, source, fpo)
member __.ParseAndCheckFileInProject(filePath: string<LocalPath>, version, source: NamedText, options) =
async {
let opName = sprintf "ParseAndCheckFileInProject - %A" filePath
checkerLogger.info (Log.setMessage "{opName}" >> Log.addContextDestructured "opName" opName)
let options = clearProjectReferences options
let path = UMX.untag filePath
try
let! (p, c) = checker.ParseAndCheckFileInProject(path, version, source, options, userOpName = opName)
let parseErrors = p.Diagnostics |> Array.map (fun p -> p.Message)
match c with
| FSharpCheckFileAnswer.Aborted ->
checkerLogger.info (
Log.setMessage "{opName} completed with errors: {errors}"
>> Log.addContextDestructured "opName" opName
>> Log.addContextDestructured "errors" (List.ofArray p.Diagnostics)
)
return ResultOrString.Error(sprintf "Check aborted (%A). Errors: %A" c parseErrors)
| FSharpCheckFileAnswer.Succeeded (c) ->
checkerLogger.info (
Log.setMessage "{opName} completed successfully"
>> Log.addContextDestructured "opName" opName
)
return Ok(ParseAndCheckResults(p, c, entityCache))
with ex ->
return ResultOrString.Error(ex.ToString())
}
member _.CheckProject(opts) = checker.ParseAndCheckProject(opts)
member __.TryGetRecentCheckResultsForFile(file: string<LocalPath>, options, source: NamedText) =
let opName = sprintf "TryGetRecentCheckResultsForFile - %A" file
checkerLogger.info (Log.setMessage "{opName}" >> Log.addContextDestructured "opName" opName)
let options = clearProjectReferences options
checker.TryGetRecentCheckResultsForFile(UMX.untag file, options, sourceText = source, userOpName = opName)
|> Option.map (fun (pr, cr, _) -> ParseAndCheckResults(pr, cr, entityCache))
member x.GetUsesOfSymbol
(
file: string<LocalPath>,
options: (string * FSharpProjectOptions) seq,
symbol: FSharpSymbol
) =
async {
checkerLogger.info (
Log.setMessage "GetUsesOfSymbol - {file}"
>> Log.addContextDestructured "file" file
)
let projects = x.GetDependingProjects file options
return!
match projects with
| None -> async { return [||] }
| Some (p, projects) ->
async {
let! res =
p :: projects
|> Seq.map (fun (opts) ->
async {
let opts = clearProjectReferences opts
let! res = checker.ParseAndCheckProject opts
return res.GetUsesOfSymbol symbol
})
|> Async.Parallel
return res |> Array.concat
}
}
member _.FindReferencesForSymbolInFile(file, project, symbol) =
checker.FindBackgroundReferencesInFile(
file,
project,
symbol,
canInvalidateProject = false,
userOpName = "find references"
)
member __.GetDeclarations(fileName: string<LocalPath>, source, options, version) =
async {
checkerLogger.info (
Log.setMessage "GetDeclarations - {file}"
>> Log.addContextDestructured "file" fileName
)
let! parseResult = checker.ParseFile(UMX.untag fileName, source, options)
return parseResult.GetNavigationItems().Declarations
}
member __.Compile = checker.Compile
member internal __.GetFSharpChecker() = checker
member __.SetDotnetRoot(dotnetBinary: FileInfo, cwd: DirectoryInfo) =
match Ionide.ProjInfo.SdkDiscovery.versionAt cwd dotnetBinary with
| Ok sdkVersion ->
let sdks = Ionide.ProjInfo.SdkDiscovery.sdks dotnetBinary
match sdks |> Array.tryFind (fun sdk -> sdk.Version = sdkVersion) with
| Some sdk ->
sdkRoot <- Some sdk.Path
let fsharpDir = Path.Combine(sdk.Path.FullName, "FSharp")
let dll = Path.Combine(fsharpDir, "FSharp.Core.dll")
let fi = FileInfo(dll)
if fi.Exists then
sdkFsharpCore <- Some fi
let dll = Path.Combine(fsharpDir, "FSharp.Compiler.Interactive.Settings.dll")
let fi = FileInfo(dll)
if fi.Exists then
sdkFsiAuxLib <- Some fi
| None -> ()
scriptTypecheckRequirementsChanged.Trigger()
| Error _ -> ()
member __.GetDotnetRoot() = sdkRoot
member __.SetFSIAdditionalArguments args =
//TODO: UX - if preview-required features are set, then auto-add langversion:preview for the user.
if fsiAdditionalArguments = args then
()
else
let additionalArgs, files = processFSIArgs args
fsiAdditionalArguments <- additionalArgs
fsiAdditionalFiles <- files
scriptTypecheckRequirementsChanged.Trigger()