Skip to content
Merged
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
10 changes: 10 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 1.4.0 (2026-11-23)

- An `.mlx` unit is parsed as mlx parsed it, from the AST its preprocessor
wrote, so attribute rules and `[@litany.allow]` work there. Any `.mlx` that
held an attribute was reported degraded ("editable source does not parse"),
since OCaml's own parser can't read JSX. A unit file names that AST in a
`parsed-source` field.
- An `.mlx` in a native library with no ppx is linted; it was skipped as
missing its artifact.

## 1.3.0 (2026-11-22)

- hypha-litany installs its documentation for agents: each page of the
Expand Down
8 changes: 5 additions & 3 deletions agents/pages/guide/build-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ The file is [canonical s-expressions](https://en.wikipedia.org/wiki/Canonical_S-
2. `(complete false)`, when it isn't your whole project.
3. `(cmi-dirs <dir>...)`, where to find the `.cmi` files of dependencies.
4. One `(unit ...)` per unit, with fields in the order `source cmt cmti
pp-source library public kind`. `source` and one of `cmt` or `cmti` are
required; `pp-source` is the preprocessed file, where there is one; `public`
is `true` or `false`; `kind` is `lib`, `exe` or `test`.
pp-source parsed-source intf-source library public kind`. `source` and one
of `cmt` or `cmti` are required; `pp-source` is the preprocessed file, where
there is one; `parsed-source` is the AST a dialect's preprocessor wrote for
a source OCaml can't parse, such as an `.mlx`; `intf-source` is the unit's
`.mli`; `public` is `true` or `false`; `kind` is `lib`, `exe` or `test`.

Anything else is refused. A wrong or stale entry costs a skip, never a finding.

Expand Down
1 change: 1 addition & 0 deletions bin/cli_units.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ let dump roster =
Option.iter (field "cmt") (E.cmt entry);
Option.iter (field "cmti") (E.cmti entry);
Option.iter (field "pp-source") (E.preprocessed_source entry);
Option.iter (field "parsed-source") (E.parsed_source entry);
Option.iter (field "intf-source") (E.interface_source entry);
Option.iter (field "library") (E.library entry);
(match E.visibility entry with
Expand Down
8 changes: 5 additions & 3 deletions docs/src/content/docs/guide/build-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ The file is [canonical s-expressions](https://en.wikipedia.org/wiki/Canonical_S-
2. `(complete false)`, when it isn't your whole project.
3. `(cmi-dirs <dir>...)`, where to find the `.cmi` files of dependencies.
4. One `(unit ...)` per unit, with fields in the order `source cmt cmti
pp-source library public kind`. `source` and one of `cmt` or `cmti` are
required; `pp-source` is the preprocessed file, where there is one; `public`
is `true` or `false`; `kind` is `lib`, `exe` or `test`.
pp-source parsed-source intf-source library public kind`. `source` and one
of `cmt` or `cmti` are required; `pp-source` is the preprocessed file, where
there is one; `parsed-source` is the AST a dialect's preprocessor wrote for
a source OCaml can't parse, such as an `.mlx`; `intf-source` is the unit's
`.mli`; `public` is `true` or `false`; `kind` is `lib`, `exe` or `test`.

Anything else is refused. A wrong or stale entry costs a skip, never a finding.

Expand Down
22 changes: 18 additions & 4 deletions lib/adapter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ module Unit_file = struct
(fun field ->
let o, key, vo, value = unit_field field in
match key with
| "source" | "cmt" | "cmti" | "pp-source" | "intf-source" | "library" ->
| "source" | "cmt" | "cmti" | "pp-source" | "parsed-source"
| "intf-source" | "library" ->
store key o value
| "public" -> (
match value with
Expand Down Expand Up @@ -580,8 +581,8 @@ module Unit_file = struct
in
let kind = Option.bind (get "kind") Roster.kind_of_string in
Roster.Entry.v ~source ?cmt ?cmti ?preprocessed_source:(get "pp-source")
?interface_source:(get "intf-source") ?library:(get "library") ~visibility
?kind ()
?parsed_source:(get "parsed-source") ?interface_source:(get "intf-source")
?library:(get "library") ~visibility ?kind ()

let decode bytes =
match
Expand Down Expand Up @@ -676,6 +677,7 @@ module Unit_file = struct
Option.iter (add_field b "cmt") (E.cmt e);
Option.iter (add_field b "cmti") (E.cmti e);
Option.iter (add_field b "pp-source") (E.preprocessed_source e);
Option.iter (add_field b "parsed-source") (E.parsed_source e);
Option.iter (add_field b "intf-source") (E.interface_source e);
Option.iter (add_field b "library") (E.library e);
(match E.visibility e with
Expand Down Expand Up @@ -1072,6 +1074,7 @@ module Dune = struct
let candidates =
(dir // (leaf ^ ".pp.ml"))
:: (dir // (leaf ^ ".mlx.pp.ml"))
:: (dir // (leaf ^ ".mlx.ml"))
::
(if compiled_by_melange then
let copies = dir // ".melange_src" in
Expand All @@ -1085,6 +1088,17 @@ module Dune = struct
in
List.find_opt exists candidates
in
(* mlx-pp writes its parse of the .mlx beside the copy the
compiler's input was made from, whether or not a ppx ran on it
after. *)
let parsed_source =
match preprocessed_source with
| Some pp when Filename.check_suffix source ".mlx" ->
let leaf = Filename.basename source in
let parsed = Filename.dirname pp // (leaf ^ ".ml") in
if exists parsed then Some parsed else None
| Some _ | None -> None
in
let library, visibility, kind =
match stanza with
| None -> (None, Roster.Unknown, None)
Expand All @@ -1107,7 +1121,7 @@ module Dune = struct
in
Some
(Roster.Entry.v ~source ?cmt ?cmti ?preprocessed_source
?interface_source ?library ~visibility ?kind ()))
?parsed_source ?interface_source ?library ~visibility ?kind ()))
units
in
let local_cmi_dirs =
Expand Down
4 changes: 2 additions & 2 deletions lib/adapter.mli
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@

{v
(unit (source lib/foo.ml) (cmt _build/default/lib/.foo.objs/byte/foo.cmt)
(cmti ...) (pp-source ...) (intf-source lib/foo.mli) (library foo)
(public true) (kind lib))
(cmti ...) (pp-source ...) (parsed-source ...)
(intf-source lib/foo.mli) (library foo) (public true) (kind lib))
v}

Only [source] and one of [cmt]/[cmti] are required in a [unit] form; unknown
Expand Down
15 changes: 9 additions & 6 deletions lib/roster.ml
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,21 @@ module Entry = struct
cmt : string option;
cmti : string option;
preprocessed_source : string option;
parsed_source : string option;
interface_source : string option;
library : string option;
visibility : visibility;
kind : kind option;
}

let v ~source ?cmt ?cmti ?preprocessed_source ?interface_source ?library
?(visibility = Unknown) ?kind () =
let v ~source ?cmt ?cmti ?preprocessed_source ?parsed_source ?interface_source
?library ?(visibility = Unknown) ?kind () =
{
source;
cmt;
cmti;
preprocessed_source;
parsed_source;
interface_source;
library;
visibility;
Expand All @@ -60,6 +62,7 @@ module Entry = struct
let cmt e = e.cmt
let cmti e = e.cmti
let preprocessed_source e = e.preprocessed_source
let parsed_source e = e.parsed_source
let interface_source e = e.interface_source
let library e = e.library
let visibility e = e.visibility
Expand All @@ -72,11 +75,11 @@ module Entry = struct
in
let str = Format.pp_print_string in
Format.fprintf ppf
"@[<hv 2>entry %s@ cmt %a@ cmti %a@ preprocessed %a@ interface %a@ \
library %a@ visibility %a@ kind %a@]"
"@[<hv 2>entry %s@ cmt %a@ cmti %a@ preprocessed %a@ parsed %a@ \
interface %a@ library %a@ visibility %a@ kind %a@]"
e.source (opt str) e.cmt (opt str) e.cmti (opt str) e.preprocessed_source
(opt str) e.interface_source (opt str) e.library pp_visibility
e.visibility (opt pp_kind) e.kind
(opt str) e.parsed_source (opt str) e.interface_source (opt str) e.library
pp_visibility e.visibility (opt pp_kind) e.kind

(* Both metadata fields project rules need; see [project_capable]. *)
let has_project_metadata e = Option.is_some e.library && Option.is_some e.kind
Expand Down
9 changes: 9 additions & 0 deletions lib/roster.mli
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ module Entry : sig
?cmt:string ->
?cmti:string ->
?preprocessed_source:string ->
?parsed_source:string ->
?interface_source:string ->
?library:string ->
?visibility:visibility ->
Expand All @@ -79,6 +80,10 @@ module Entry : sig
- [preprocessed_source]: the built preprocessed source ([<m>.pp.ml]) for
units the compiler read from a built pp file — the anchor of the Derived
witness. Defaults to none, meaning the unit is expected to join Direct.
- [parsed_source]: the editable source as its dialect's preprocessor
parsed it — the marshalled AST mlx-pp writes for an [.mlx], which the
OCaml parser cannot read. [Unit.parsetree] reads it in place of parsing
[source]. Defaults to none: [source] is OCaml.
- [interface_source]: the unit's paired interface source ([.mli]) — the
second editable file of a unit whose [source] is an implementation. Text
rules ([Rule.source]) run over it too; nothing else reads it. Defaults
Expand All @@ -104,6 +109,10 @@ module Entry : sig
(** [preprocessed_source e] is the built preprocessed source path, if the
compiler read one. *)

val parsed_source : t -> string option
(** [parsed_source e] is the path of the AST the source's dialect parsed it
into, if named. *)

val interface_source : t -> string option
(** [interface_source e] is the paired interface source path ([.mli]), if the
adapter named one. Read once at load ([Unit.load]) for the text lane;
Expand Down
32 changes: 27 additions & 5 deletions lib/unit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ type t = {
kind : kind option;
implementation : Typedtree.structure;
cmti : string option;
parsed_source : string option;
scope : Naming.Scope.t;
intra : Shape.Uid.t -> Shape.Uid.t list;
mutable interface : Typedtree.signature option demand;
Expand Down Expand Up @@ -455,6 +456,8 @@ let load ~resolver ~build_current entry =
kind = Roster.Entry.kind entry;
implementation;
cmti = Roster.Entry.cmti entry;
parsed_source =
Roster.Entry.parsed_source entry;
scope =
Naming.Scope.v ~resolver ~intra
~local:implementation.Typedtree.str_type;
Expand Down Expand Up @@ -576,16 +579,35 @@ let interface u =
u.interface <- Loaded v;
v

(* A dialect's preprocessor writes its parse as [Pparse.read_ast] reads it:
the magic, the name of the file it read, then the structure. Read by
hand, because [Pparse.read_ast] also sets [Location.input_name]. *)
let read_parsed_source path =
let magic = Config.ast_impl_magic_number in
match
In_channel.with_open_bin path (fun ic ->
if String.equal (really_input_string ic (String.length magic)) magic
then
let (_ : string) = input_value ic in
Some (input_value ic : Parsetree.structure)
else None)
with
| tree -> tree
| exception (Sys_error _ | End_of_file | Failure _) -> None

let parsetree u =
match u.parsetree with
| Loaded v -> v
| Not_loaded ->
let v =
let lexbuf = Lexing.from_string (Source.contents u.source) in
Lexing.set_filename lexbuf u.path;
match Parse.implementation lexbuf with
| tree -> Some tree
| exception (Syntaxerr.Error _ | Lexer.Error _) -> None
match u.parsed_source with
| Some path -> read_parsed_source path
| None -> (
let lexbuf = Lexing.from_string (Source.contents u.source) in
Lexing.set_filename lexbuf u.path;
match Parse.implementation lexbuf with
| tree -> Some tree
| exception (Syntaxerr.Error _ | Lexer.Error _) -> None)
in
u.parsetree <- Loaded v;
v
Expand Down
10 changes: 6 additions & 4 deletions lib/unit.mli
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,12 @@ val interface : t -> Typedtree.signature option
witness). *)

val parsetree : t -> Parsetree.structure option
(** [parsetree u] is the engine's own pre-PPX parse of {!source}, or [None] when
the editable source does not parse as OCaml (cppo sources and kin — such
units run typed rules only, with a summary note that attribute rules and
attribute suppression are unavailable). Demand-gated and cached; the same
(** [parsetree u] is the pre-PPX parse of {!source}, or [None] when there is
none (cppo sources and kin — such units run typed rules only, with a summary
note that attribute rules and attribute suppression are unavailable). It is
the engine's own parse of the source as OCaml, or, when the entry names a
{!Roster.Entry.parsed_source}, the AST read from that file: [None] when it
is unreadable or not this compiler's AST. Demand-gated and cached; the same
parse serves attribute rules, suppression matching, and the emit contract's
corroboration. *)

Expand Down
7 changes: 7 additions & 0 deletions test/cli/dune
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,10 @@
(deps
%{bin:litany}
(source_tree %{project_root}/examples)))

; An mlx unit read through its dialect's own parse: a nested project whose
; mlx dialect runs the stand-in preprocessor in mlx_pp/.

(cram
(applies_to mlx)
(deps %{bin:litany} mlx_pp/mlx_pp.exe))
49 changes: 49 additions & 0 deletions test/cli/mlx.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
An implementation in mlx's dialect is its .mlx, which the OCaml parser
cannot read: litany reads it as the dialect's preprocessor parsed it, so
attribute suppression and the other parse-fed checks hold there as in a
.ml. The fixture is a real nested dune project whose mlx dialect runs a
stand-in for mlx-pp, which reads <jsx/> as an element.

$ unset GITHUB_ACTIONS
$ MLX_PP=$PWD/../mlx_pp/mlx_pp.exe
$ mkdir proj && cd proj
$ cat > dune-project <<EOP
> (lang dune 3.20)
> (dialect
> (name mlx)
> (implementation
> (extension mlx)
> (preprocess (run $MLX_PP %{input-file}))))
> EOP
$ cat > dune <<'EOP'
> (library (name mlxlib))
> EOP
$ cat > view.mlx <<'EOP'
> let element = <jsx/>
> let is_empty xs = List.length xs = 0
> let is_blank xs = (List.length xs = 0) [@litany.allow "needless-list-length: shown as written"]
> EOP
$ env -u INSIDE_DUNE dune build --root . @check 2>&1

The OCaml parser refuses the source; the build does not.

$ ocamlc -stop-after parsing -impl view.mlx 2>&1 | head -1
File "view.mlx", line 1, characters 14-15:

The unit is linted, not degraded: the unsuppressed finding is reported,
the suppressed one is not.

$ env -u INSIDE_DUNE litany check
File "view.mlx", line 2, characters 18-36:
2 | let is_empty xs = List.length xs = 0
^^^^^^^^^^^^^^^^^^
Warning 0 [needless-list-length]: comparison through List.length is a needless emptiness test

30 rules selected · 2 units · 1 finding · 0 skipped · 1 facts-only · 1 suppressed
[1]

The roster names the AST mlx-pp wrote beside the build's copy of the
source, and a unit file carries it.

$ env -u INSIDE_DUNE litany units --dump | grep -o '(parsed-source [^)]*)'
(parsed-source _build/default/view.mlx.ml)
7 changes: 7 additions & 0 deletions test/cli/mlx_pp/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
; A stand-in for mlx's preprocessor, so the mlx cram runs without mlx
; installed: OCaml with one form the OCaml parser refuses, written out as
; the marshalled AST mlx-pp hands dune. TEST-ONLY, never installed.

(executable
(name mlx_pp)
(libraries compiler-libs.common))
31 changes: 31 additions & 0 deletions test/cli/mlx_pp/mlx_pp.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(* [<jsx/>] stands for an element: it is read as [()] padded to the same
six bytes, so every position in the AST is the .mlx's own, as mlx-pp's
are. The output is what [Pparse.read_ast] reads: the magic, the name of
the file read, then the structure. *)

let element = "<jsx/>"
let unit_padded = "() "

let read_elements source =
let b = Bytes.of_string source in
let n = String.length element in
for i = 0 to String.length source - n do
if String.equal (String.sub source i n) element then
Bytes.blit_string unit_padded 0 b i n
done;
Bytes.to_string b

let () =
match Sys.argv with
| [| _; path |] ->
let source = In_channel.with_open_bin path In_channel.input_all in
let lexbuf = Lexing.from_string (read_elements source) in
Lexing.set_filename lexbuf path;
let ast = Parse.implementation lexbuf in
Out_channel.set_binary_mode stdout true;
output_string stdout Config.ast_impl_magic_number;
output_value stdout path;
output_value stdout (ast : Parsetree.structure)
| _ ->
prerr_endline "usage: mlx_pp FILE";
exit 2
Loading