Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions .github/actions/setup-linux-dependencies/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,54 @@ runs:
sudo DEBIAN_FRONTEND=noninteractive apt-get install --yes -qq \
--no-install-recommends --no-upgrade \
bubblewrap musl-tools rsync cmake

# The static OCaml switch uses musl-gcc. linux-libc-dev installs Linux
# headers under /usr/include, but musl-gcc searches the musl include dir.
# Link the Linux headers into musl's include path so packages with C stubs
# such as uring can include <linux/...> headers.
- name: Make Linux headers visible to musl-gcc
if: runner.os == 'Linux'
shell: bash
run: |
set -eux

# Get the GNU multiarch triplet for the current machine.
# Examples:
# x86_64-linux-gnu
# aarch64-linux-gnu
GNU_MULTIARCH="$(gcc -print-multiarch)"

# Convert the GNU triplet into the musl include directory name.
# Examples:
# x86_64-linux-gnu -> x86_64-linux-musl
# aarch64-linux-gnu -> aarch64-linux-musl
MUSL_MULTIARCH="${GNU_MULTIARCH%-gnu}-musl"

# musl-gcc searches this include directory.
MUSL_INCLUDE="/usr/include/${MUSL_MULTIARCH}"

# Linux arch-specific asm headers are installed here by linux-libc-dev.
GNU_ASM="/usr/include/${GNU_MULTIARCH}/asm"

# Ensure the musl include directory exists.
sudo mkdir -p "$MUSL_INCLUDE"

# Remove old paths first.
# This avoids silently keeping broken/stale symlinks from previous runs.
sudo rm -rf "$MUSL_INCLUDE/linux"
sudo rm -rf "$MUSL_INCLUDE/asm"
sudo rm -rf "$MUSL_INCLUDE/asm-generic"

# Expose Linux UAPI headers to musl-gcc.
# This fixes packages that include headers like <linux/swab.h>.
sudo ln -s /usr/include/linux "$MUSL_INCLUDE/linux"

# Expose architecture-specific asm headers to musl-gcc.
sudo ln -s "$GNU_ASM" "$MUSL_INCLUDE/asm"

# Expose generic asm headers used by many Linux headers.
sudo ln -s /usr/include/asm-generic "$MUSL_INCLUDE/asm-generic"

# Smoke test: fail early if musl-gcc still cannot find Linux headers.
echo '#include <linux/swab.h>' > /tmp/test.c
musl-gcc -c /tmp/test.c -o /tmp/test.o
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
# tests repeat the linux-x64 job's. OCaml 5.0 does not build with
# GCC 15 (C23 by default), so this job stays on Ubuntu 24.04.
- os: ubuntu-24.04
ocaml_compiler: ocaml-variants.5.0.0+options,ocaml-option-static
ocaml_compiler: ocaml-variants.5.3.0+options,ocaml-option-static

@aspeddro aspeddro Nov 7, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CI compatibility job now uses OCaml 5.3 instead of 5.0 because the current test dependencies cannot be resolved on 5.0.

The compiler requires ppxlib >= 0.38.0. The language server’s ppx_expect dependency pulls in Jane Street PPXs whose versions compatible with that ppxlib require OCaml 5.3 or newer.

node-target: linux-x64
dune-profile: static
skip_rewatch_integration_tests: true
Expand Down Expand Up @@ -267,6 +267,9 @@ jobs:
opam exec -- ./_build/default/tests/rewatch_ounit_tests/rewatch_ounit_tests_main.exe -runner sequential
shell: bash

- name: Run LSP tests
run: opam exec -- make test-lsp

# The canonical suite works in rewatch/testrepo and the focused one in
# directories of its own, so they run side by side; each takes one to
# three and a half minutes. The canonical suite fails when another
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ $(YARN_INSTALL_STAMP): $(YARN_INSTALL_SOURCES)

# Compiler

COMPILER_SOURCE_DIRS := compiler syntax tests analysis tools rewatch-ocaml
COMPILER_SOURCE_DIRS := compiler syntax tests analysis tools rewatch-ocaml lsp
COMPILER_SOURCES = $(shell find $(COMPILER_SOURCE_DIRS) -type f \( -name '*.ml' -o -name '*.mli' -o -name '*.c' -o -name '*.h' -o -name '*.dune' -o -name dune -o -name dune-project \))
COMPILER_BIN_NAMES := bsc rescript-editor-analysis rescript-tools rescript
COMPILER_EXES := $(addsuffix .exe,$(addprefix $(BIN_DIR)/,$(COMPILER_BIN_NAMES)))
Expand Down Expand Up @@ -159,7 +159,10 @@ test-gentype: lib
test-rewatch: lib
./rewatch/tests/suite.sh $(RESCRIPT_EXE)

test-all: test test-gentype test-analysis test-tools test-rewatch
test-lsp: lib
@dune runtest tests/lsp_unit_tests tests/lsp_tests

test-all: test test-gentype test-analysis test-tools test-rewatch test-lsp

# Playground

Expand Down
61 changes: 46 additions & 15 deletions analysis/src/module_resolution.ml

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change makes analysis prefer the runtime installed in the project over the compiler-local runtime.

Previously, resolving @rescript/runtime bypassed the normal node_modules search and immediately returned Runtime_package.get_path (). During local LSP development, this could make “Go to definition” open runtime sources from the compiler checkout instead of the user’s project.

The resolver now searches the project and its ancestor directories first, including the .deno/node_modules and .pnpm/node_modules lookup locations. The compiler-local runtime remains a fallback for development environments where the project has no runtime installation.

This is not a complete Node/Deno/pnpm module resolver; it extends the existing package-directory lookup. The explicit RESCRIPT_RUNTIME override still takes precedence when selecting the analysis runtime.

Module resolution searches each directory and its ancestors for:

  1. node_modules/<package>
  2. node_modules/.deno/node_modules/<package>
  3. node_modules/.pnpm/node_modules/<package>

Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
let ( /+ ) = Filename.concat

let rec resolve_node_module_path ~start_path name =
if name = "@rescript/runtime" then
(* Hack: we need a reliable way to resolve modules in monorepos. *)
Some (Runtime_package.get_path ())
let package_path name =
let scope = Filename.dirname name in
let name = Filename.basename name in
match scope.[0] with
| '@' -> scope /+ name
| _ -> name

let resolve_deno_pnpm ~start_path name =
let create_path folder =
start_path /+ "node_modules" /+ folder /+ "node_modules"
/+ package_path name
in
let deno = create_path ".deno" in
let pnpm = create_path ".pnpm" in
match (Files.if_exists deno, Files.if_exists pnpm) with
| Some deno, None -> Some deno
| None, Some pnpm -> Some pnpm
| Some deno, Some pnpm ->
failwith
(Printf.sprintf
"Failed to resolve path for %s. Conflict: two directories found. one \
for Deno (%s) and one for pnpm (%s)."
name deno pnpm)
| _ -> None

let rec resolve_from_node_modules ~start_path name =
let path = start_path /+ "node_modules" /+ package_path name in
if Files.exists path then Some path
else
let scope = Filename.dirname name in
let name = Filename.basename name in
let name =
match scope.[0] with
| '@' -> scope /+ name
| _ -> name
in
let path = start_path /+ "node_modules" /+ name in
if Files.exists path then Some path
else if Filename.dirname start_path = start_path then None
else resolve_node_module_path ~start_path:(Filename.dirname start_path) name
match resolve_deno_pnpm ~start_path name with
| Some p -> Some p
| None ->
if Filename.dirname start_path = start_path then None
else
resolve_from_node_modules ~start_path:(Filename.dirname start_path) name

let resolve_node_module_path ~start_path name =
match resolve_from_node_modules ~start_path name with
| Some path -> Some path
| None
when name = Runtime_package.name
&& Files.exists (Runtime_package.get_path ()) ->
(* Compiler-local fallback for development builds where the runtime package
is not installed in the target workspace. Real project installs must win
so LSP definition locations point at the workspace's node_modules. *)
Some (Runtime_package.get_path ())
| None -> None
100 changes: 100 additions & 0 deletions analysis/src/shared_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -946,3 +946,103 @@ let extract_exp_apply_args ~args =
| [] -> List.rev acc
in
args |> process_args ~acc:[]

let state_to_yojson (state : state) =
let option_to_yojson f = function
| None -> `Null
| Some value -> f value
in

let string_set_to_yojson set =
`List (set |> File_set.elements |> List.map (fun value -> `String value))
in

let path_to_yojson path = `List (List.map (fun item -> `String item) path) in

let paths_to_yojson = function
| Impl {cmt; res} ->
`Assoc
[("kind", `String "Impl"); ("cmt", `String cmt); ("res", `String res)]
| Namespace {cmt} ->
`Assoc [("kind", `String "Namespace"); ("cmt", `String cmt)]
| IntfAndImpl {cmti; resi; cmt; res} ->
`Assoc
[
("kind", `String "IntfAndImpl");
("cmti", `String cmti);
("resi", `String resi);
("cmt", `String cmt);
("res", `String res);
]
in

let paths_for_module_to_yojson paths_for_module =
paths_for_module |> Hashtbl.to_seq
|> Seq.map (fun (file, paths) -> (file, paths_to_yojson paths))
|> List.of_seq
|> fun fields -> `Assoc fields
in

let autocomplete_to_yojson autocomplete =
autocomplete |> Misc.String_map.bindings
|> List.map (fun (name, files) ->
(name, `List (List.map (fun file -> `String file) files)))
|> fun fields -> `Assoc fields
in

let package_to_yojson (package : package) =
`Assoc
[
( "generic_jsx_module",
option_to_yojson
(fun value -> `String value)
package.generic_jsx_module );
("suffix", `String package.suffix);
("root_path", `String package.root_path);
("project_files", string_set_to_yojson package.project_files);
("dependencies_files", string_set_to_yojson package.dependencies_files);
("paths_for_module", paths_for_module_to_yojson package.paths_for_module);
( "namespace",
option_to_yojson (fun value -> `String value) package.namespace );
("opens", `List (List.map path_to_yojson package.opens));
("autocomplete", autocomplete_to_yojson package.autocomplete);
]
in

let file_to_yojson (file : File.t) =
`Assoc
[
("uri", `String (file.uri |> Lsp.Uri.to_string));
("module_name", `String file.module_name);
("stamps_count", `Int (List.length (Stamps.get_entries file.stamps)));
("structure_name", `String file.structure.name);
( "structure_docstring",
`List (List.map (fun value -> `String value) file.structure.docstring)
);
("structure_items_count", `Int (List.length file.structure.items));
]
in

let cmt_cache =
state.cmt_cache |> Hashtbl.to_seq
|> Seq.map (fun (file_path, file) -> (file_path, file_to_yojson file))
|> List.of_seq
in

let root_for_uri =
state.root_for_uri |> Hashtbl.to_seq |> List.of_seq
|> List.map (fun (uri, str) -> [(Lsp.Uri.to_string uri, `String str)])
|> List.flatten
in

let packages_by_root =
state.packages_by_root |> Hashtbl.to_seq |> List.of_seq
|> List.map (fun (root, package) -> (root, package_to_yojson package))
in

`Assoc
[
("cmt_cache", `Assoc cmt_cache);
("root_for_uri", `Assoc root_for_uri);
("packages_by_root", `Assoc packages_by_root);
]
3 changes: 3 additions & 0 deletions changes/language-server.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- Add an experimental ReScript language server bundled with the compiler, available
through `rescript lsp --stdio`. It reuses compiler analysis for hover,
completion, navigation, rename, diagnostics, formatting, and code actions.
2 changes: 1 addition & 1 deletion dune
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(dirs compiler syntax tests analysis tools rewatch-ocaml)
(dirs compiler syntax tests analysis tools rewatch-ocaml lsp)

; All warnings are enabled, except:
; - 4 (fragile-match): wildcard cases are used deliberately.
Expand Down
18 changes: 18 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,21 @@
(yojson
(= 3.0.0))
(odoc :with-doc)))

(package
(name rescript-language-server)
(synopsis "ReScript LSP")
(depends
(ocaml
(>= 5.0.0))
(lsp
(>= 1.22.0))
(eio
(>= 1.3))
(eio_main
(>= 1.3))
analysis
dune
ppx_deriving_yojson
(ppx_expect
(and :with-test (= v0.17.3)))))
4 changes: 4 additions & 0 deletions licenses/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ The files are generated from `components.json` by
Its version and license text come from the current opam switch. The script
asks dune which opam packages the shipped executables link, and fails when
one is missing from the inventory or listed without being linked.
A metadata-only compatibility package can set `licenseOpam` to the package
providing its implementation and license text (for example, `seq.base` uses
the OCaml standard library's license). Additional bundled-source notices
are included through `licenseFiles`.
- Every other entry is vendored or adapted source, or a platform toolchain,
with its license text in this directory or in the vendored source
directory. Entries with a `platforms` field are included only there: Linux
Expand Down
Loading
Loading