Skip to content

Native language server in Flow; Python LSP deleted (#693) - #1016

Merged
godofecht merged 11 commits into
mainfrom
port/native-lsp
Sep 29, 2026
Merged

godofecht merged 11 commits into
mainfrom
port/native-lsp

Conversation

@godofecht

Copy link
Copy Markdown
Contributor

Closes the LSP part of #693.

What this does

Replaces the Python language server with one written in Flow, tools/lsp/main.flow (about 6.5k lines across tools/lsp/*.flow). ./flow lsp builds it with the Stage-A compiler on first use (flow_native_tool, same as flow repl) and runs it. ./flow-lsp is now a bash launcher for ./flow lsp, and the VS Code extension starts flow lsp by default.

It speaks JSON-RPC 2.0 over stdio with Content-Length framing and keeps the Python server's JSON layout (key order, ", " / ": " separators, \uXXXX escapes) so the two can be diffed byte for byte.

Methods: initialize, initialized, shutdown, exit, textDocument/didOpen, didChange (full sync, 200 ms debounce as before), didClose, publishDiagnostics, hover, completion, definition, documentSymbol, references, documentHighlight, prepareRename, rename, formatting, rangeFormatting, codeAction. The Python server had a code action handler for idiom fixes but never routed it; the native one does and advertises it.

Diagnostics run in process:

  • parse errors from compiler/src/parser.flow (after the Field DSL, dynamics DSL and flow block expansions, as flowc compiles);
  • type findings from the Stage-A checker (typecheck.flow), with the exports of every module the document reaches through imports seeded first, as the bundle checker does. The checker prints to stdout, so the server keeps the protocol on a dup of the original stdout, points fd 1 at stderr, and captures the checker's output through a scratch file;
  • FIDIOM001 / FIDIOM002 idiom hints.

Imports resolve with the module resolver's rules (relative, std., flow.toml [paths] and [dependencies] via flow_packages/, legacy string paths) and follow export import forwards, so hover and definition on use_hello_lib's imports reach hello_lib/src/lib.flow.

Formatting uses compiler/src/fmt.flow only when its output keeps every token and comment of the source; otherwise it re-indents without touching tokens. fmt.flow drops comments and some declarations and writes let x: void for an untyped let, so a straight pass-through would delete code.

Proof: transcript test

tests/tools/lsp/run.sh replays nine JSON-RPC sessions (tests/tools/lsp/sessions/*.lsp) against the server: lifecycle, diagnostics and code actions, references and rename across two files, hover, completion, definition, imports through re-export chains, formatting, and diagnostics plus document symbols over ten repository files (stdlib, examples including effects, enums, dynamics and a proof file, a tests/lang program, a compiler source file, a package consumer).

Each case checks two things:

  • native output equals <case>.expected;
  • diff <case>.python native equals <case>.accepted.diff, where <case>.python is what the Python server answered to the same session, recorded before deletion.

Result: 205 messages, 150 identical to the Python server's, 55 different. Every difference is classified in tests/tools/lsp/ACCEPTED.md:

  1. capabilities (codeActionProvider, version) in each initialize;
  2. flowc diagnostic wording, and Python's false "returns void" warnings;
  3. idiom hint position (Python's was off by one on one axis) and Python's advisor crashing on any if;
  4. Python's framing bug: it read Content-Length as characters, so any document with non-ASCII text (an em dash in a comment, «» claim paths) lost that message and the next; the native server answers all of them;
  5. symbol range ends (Python's drifted after @attr lines) and imported symbols no longer listed in documentSymbol;
  6. type text as written (ptr<f64>) instead of Python's internal names (ptr<ptr_f64>, array_7_i32);
  7. dyn. files: Python analysed the expanded text but reported a parse error from the raw text;
  8. formatting: Python's formatter replaced loops with <expr>, dropped comments and dropped parentheses ((a + b) * 2 became a + b * 2).

Hover and completion prose is the Python text with U+2014 written as a hyphen; run.sh normalizes that one character before diffing.

$ PATH=build/nopy:$PATH ./tests/tools/run.sh     # python and python3 stubbed to fail
PASS repl/session
tool transcripts: 1 passed, 0 failed
PASS completion
PASS definition
PASS diagnostics
PASS formatting
PASS hover
PASS imports
PASS lifecycle
PASS references_rename
PASS repo_files
lsp transcripts: 9 passed, 0 failed

No python invoked line in any server stderr, and the server was rebuilt from scratch under the stub.

Deleted

  • src/flow/lsp_server.py, lsp_intel.py, lsp_syntax.py, lsp_dynamics.py, lsp_ordering.py
  • scripts/test_lsp_server.py, tests/integration/test_lsp_server.py, tests/unit/test_lsp_hover.py, test_lsp_idioms.py, test_lsp_intel.py, and the LSP class in test_module_reexport.py. Their behavioral checks are sessions in tests/tools/lsp/.

4,607 lines of Python removed. ./scripts/python_ratchet.sh --update run; baseline limit now 92,899.

Branch notes

Verification on the final tree: fixed point OK; bootstrap_from_c.sh --verify PASS; self_host_full.sh ALL PASS (three generations byte-identical); roundtrip.sh ALL PASS; FLOW_HOST=python ./flow run compiler/src/main.flow prints flowc: PASS.

CI

tests/tools/run.sh now calls tests/tools/lsp/run.sh, so the existing native-tools job (Python stubbed out) runs the LSP sessions with no workflow change. It needs jq, which ubuntu-latest has. If a separate job is preferred, it would be the same steps as native-tools with ./tests/tools/lsp/run.sh as the last command, triggered on tools/**, tests/tools/**, flow-driver and compiler/**.

Follow-ups

  • fmt.flow: keep comments, keep parentheses by precedence, omit the type of an untyped let, print imports and externs. The server then takes its output for more files.
  • Imported symbols carry no doc comment in hover (same as the Python server).

@godofecht
godofecht force-pushed the port/native-lsp branch 2 times, most recently from 0c51e16 to 8731b7c Compare September 29, 2026 08:43
@godofecht
godofecht force-pushed the port/native-lsp branch 2 times, most recently from 6c1274e to 67653fb Compare September 29, 2026 09:40
…ness

tools/lsp/main.flow speaks JSON-RPC over stdio and takes diagnostics from
the flowc parser and Stage-A checker in process. ./flow lsp builds and runs
it. tests/tools/lsp/run.sh drives it with recorded sessions; the *.python
files are the Python server's answers to the same sessions.
Formatting takes fmt.flow's output only when it keeps every token and
comment, and otherwise re-indents. The idiom check counts writes by token,
so assignments inside brace-skipped blocks count.
src/flow/lsp_server.py, lsp_intel.py, lsp_syntax.py, lsp_dynamics.py and
lsp_ordering.py are replaced by tools/lsp. The behavioral checks of
scripts/test_lsp_server.py, tests/unit/test_lsp_*.py and the re-export
index test now live in the transcript sessions under tests/tools/lsp.
Python ratchet tightened.
tests/tools/run.sh now runs the language server sessions too, so the
native-tools CI job (Python stubbed out) covers flow lsp.
With #999 on main, flowc expands dyn. blocks, so the server indexes
dynamics files the way it compiles them.
tests/tools/lsp/fixtures/dsl.flow holds a dsys block, so the dynamics
passthrough check asks for an ex_ mirror. The golden is written from the
Python expander at 7527ab37.
tests/tools/lsp/fixtures holds inputs for the LSP transcripts, several of
them broken on purpose to exercise diagnostics. The tier-2 sweep tried to
compile them and failed. The transcripts in tests/tools/run.sh cover them.
@godofecht
godofecht merged commit 9fdcecc into main Sep 29, 2026
26 checks passed
@godofecht
godofecht deleted the port/native-lsp branch October 1, 2026 12:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant