Page: the host checks text before it runs

Job: explanation

Level: mid grug (level 2 of 6)

Source lines:

  • SageFs.FsiHost/FcsQueries.fs:47#checkWithSymbols
  • SageFs.FsiHost/FcsQueries.fs:71#evalConfig

the host checks text before it runs

mid grug (level 2 of 6)explanationteaches: unsaved-edits, parse-check-eval

explanation

this page says why the code has this shape and what a reader must be careful of.

demon watch: complexity spirit demon may lurk here

grug measure SageFs.FsiHost/FcsQueries.fs:

  • reflection in 1 place(s) (line 104): reflection reads types while the program runs, so the compiler does not check what it does

this sign only tells grug where to look. open the file and look before grug says the demon is there.

why this exists

The point of SageFS is feedback on an edit you have not saved. The FSI host takes the text, asks the compiler to parse and check it, and turns diagnostics into wire values. SageFs.FsiHost/FcsQueries.fs:47#checkWithSymbols keeps symbols for the dependency graph, and SageFs.FsiHost/FcsQueries.fs:71#evalConfig uses the same compiler session for configuration text.

source anchor: this line is in the file

run sed -n '71,82p' SageFs.FsiHost/FcsQueries.fs

It prints:

let evalConfig (session: FsiEvaluationSession) (content: string) : ConfigOutcome =
  try
    session.EvalInteractionNonThrowing "open SageFs;;" |> ignore
    let result, diagnostics = session.EvalExpressionNonThrowing content
    let errors = diagnostics |> Array.filter (fun d -> d.Severity = FSharpDiagnosticSeverity.Error)
    match errors.Length > 0 with
    | true -> ConfigRejected(ConfigDoesNotCompile(errors |> Array.map toWire |> Array.toList))
    | false ->
      match result with
      | Choice1Of2(Some value) ->
        match value.ReflectionValue with
        | :? DirectoryConfig as config -> ConfigEvaluated config

The cited file SageFs.FsiHost/FcsQueries.fs ships with this site at the same path. Run the command from the school folder, or open SageFs.FsiHost/FcsQueries.fs.

what breaks

If the host checked only saved files, the feedback loop would follow the editor's save button. If it skipped the check phase, diagnostics would arrive after evaluation instead of before it.

where it fits

Live test routing decides which session a changed file reaches. This host decides what that session learns about the text before it evaluates it.

try it

Run the grep above from this repository root. Then type a bad symbol into an unsaved buffer. The diagnostic comes from this path, before any evaluation result is shown.

reference: every connection to this page
where this fits

This is reference. The next page in the reading order is at the bottom of the page.

path order: repo paths are found at runtime

flowchart LR
  A0["repo paths are found at runtime"]
  H["you are here: the host checks text before it runs"]
  A0 --> H

grill: say it out loud, then tap

Grug asks. Say the answer out loud, then tap. Grug remembers what you miss and asks again in the pop quiz.

a badge says two things: the number is its level, the colour is the kind of answer it asks for. The six levels are level 1 of 6 recall a fact; level 2 of 6 recall a detail; level 3 of 6 trace a path; level 4 of 6 trace a longer path; level 5 of 6 predict or judge; level 6 of 6 judge a transfer. Colour and level are independent: a high-level question can still ask for a fact, and the colour tells you which it is.

level 2 of 6Q1 · An edit is still unsaved. What text does the host check?
try first, then show answer

3. The text sent from the editor buffer

why

The host receives the editor text directly, so a file need not be saved before the compiler sees it.

level 1 of 6Q2 · Which function checks diagnostics and keeps symbols?
try first, then show answer

checkWithSymbols

why

checkWithSymbols returns diagnostics plus the symbol references the live dependency graph uses.

level 2 of 6Q3 · When does the host turn diagnostics into wire values?
try first, then show answer

3. After parse and check, before evaluation

why

Parse and check run on the supplied text first, so diagnostics arrive before evaluation results.

level 1 of 6Q4 · Which grep shows the check door in this lesson?
try first, then show answer

grep -n 'let check (' SageFs.FsiHost/FcsQueries.fs

why

The grep in the lesson prints the exact declaration line for the check door.