Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rescript.json #6382

Merged
merged 5 commits into from
Oct 9, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
- Untagged variants: Support `bool`. https://github.com/rescript-lang/rescript-compiler/pull/6368
- Support aliased types as payloads to untagged variants. https://github.com/rescript-lang/rescript-compiler/pull/6394
- Support the async component for React Server Component in JSX V4. https://github.com/rescript-lang/rescript-compiler/pull/6399
- Support `rescript.json` configuration file and deprecated `bsconfig.json`. https://github.com/rescript-lang/rescript-compiler/pull/6382

#### :boom: Breaking Change
- Add smart printer for pipe-chains. https://github.com/rescript-lang/rescript-compiler/pull/6411 (the formatter will reformat existing code in certain cases)
Expand Down
15 changes: 7 additions & 8 deletions jscomp/bsb/bsb_build_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
Expand Down Expand Up @@ -101,7 +101,7 @@ let resolve_bsb_magic_file ~cwd ~desc p : result =

(** converting a file from Linux path format to Windows *)

(**
(**
{[
mkp "a/b/c/d";;
mkp "/a/b/c/d"
Expand Down Expand Up @@ -155,9 +155,8 @@ let extract_pinned_dependencies (map : Ext_json_types.t Map_string.t) : Set_stri

let rec walk_all_deps_aux (visited : string Hash_string.t) (paths : string list)
~(top : top) (dir : string) (queue : _ Queue.t) ~pinned_dependencies =
let bsconfig_json = dir // Literals.bsconfig_json in
match Ext_json_parse.parse_json_from_file bsconfig_json with
| Obj { map; loc } ->
match Bsb_config_load.load_json ~per_proj_dir:dir ~warn_legacy_config:false with
| _, Obj { map; loc } ->
let cur_package_name =
match Map_string.find_opt map Bsb_build_schemas.name with
| Some (Str { str; loc }) ->
Expand All @@ -183,7 +182,7 @@ let rec walk_all_deps_aux (visited : string Hash_string.t) (paths : string list)
else
let explore_deps (deps : string) pinned_dependencies =
map
|? ( deps,
|? ( deps,
`Arr
(fun (new_packages : Ext_json_types.t array) ->
Ext_array.iter new_packages (fun js ->
Expand All @@ -205,8 +204,8 @@ let rec walk_all_deps_aux (visited : string Hash_string.t) (paths : string list)
| Expect_name n when Set_string.mem pinned_dependencies n -> true
| _ -> false
in
let pinned_dependencies = match is_pinned with
| true ->
let pinned_dependencies = match is_pinned with
| true ->
let transitive_pinned_dependencies = extract_pinned_dependencies map
in
Set_string.union transitive_pinned_dependencies pinned_dependencies
Expand Down
24 changes: 24 additions & 0 deletions jscomp/bsb/bsb_config_load.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let ( // ) = Ext_path.combine

let load_json ~(per_proj_dir : string) ~(warn_legacy_config : bool)
: string * Ext_json_types.t =
let filename, abs, in_chan =
let filename = Literals.rescript_json in
let abs = (per_proj_dir // filename) in
match open_in abs
with
| in_chan -> (filename, abs, in_chan)
| exception e ->
let filename = Literals.bsconfig_json in
let abs = (per_proj_dir // filename) in
match open_in abs
with
| in_chan -> (filename, abs, in_chan)
| exception _ -> raise e (* forward error from rescript.json *)
in
if warn_legacy_config && filename = Literals.bsconfig_json then
print_endline "Warning: bsconfig.json is deprecated. Migrate it to rescript.json\n";
match Ext_json_parse.parse_json_from_chan abs in_chan
with
| v -> close_in in_chan ; (filename, v)
| exception e -> close_in in_chan ; raise e
2 changes: 2 additions & 0 deletions jscomp/bsb/bsb_config_load.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
val load_json :
per_proj_dir:string -> warn_legacy_config:bool -> string * Ext_json_types.t
33 changes: 19 additions & 14 deletions jscomp/bsb/bsb_config_parse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ let extract_package_name_and_namespace (map : json_map) : string * string option
- the running bsb need delete stale build artifacts
(kinda check npm upgrade)

Note if the setup is correct:
Note if the setup is correct:
the running compiler and node_modules/rescript
should be the same version,
The exact check is that the running compiler should have a
should be the same version,
The exact check is that the running compiler should have a
compatible runtime version installed, the location of the
compiler is actually not relevant.
We disable the check temporarily
Expand Down Expand Up @@ -235,9 +235,13 @@ let extract_js_post_build (map : json_map) cwd : string option =
|> ignore;
!js_post_build_cmd

(** ATT: make sure such function is re-entrant.
(** ATT: make sure such function is re-entrant.
With a given [cwd] it works anywhere*)
let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
let interpret_json
~(filename : string)
~(json : Ext_json_types.t)
~(package_kind : Bsb_package_kind.t)
~(per_proj_dir : string)
: Bsb_config_types.t =
(* we should not resolve it too early,
since it is external configuration, no {!Bsb_build_util.convert_and_resolve_path}
Expand All @@ -253,8 +257,7 @@ let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
1. if [build.ninja] does use [ninja] we need set a variable
2. we need store it so that we can call ninja correctly
*)
match
Ext_json_parse.parse_json_from_file (per_proj_dir // Literals.bsconfig_json)
match json
with
| Obj { map } -> (
let package_name, namespace = extract_package_name_and_namespace map in
Expand Down Expand Up @@ -349,17 +352,19 @@ let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
(match package_kind with
| Toplevel -> extract_uncurried map
| Pinned_dependency x | Dependency x -> x.uncurried);
filename;
}
| None ->
Bsb_exception.invalid_spec "no sources specified in bsconfig.json")
| _ -> Bsb_exception.invalid_spec "bsconfig.json expect a json object {}"
Bsb_exception.invalid_spec ("no sources specified in " ^ filename))
| _ -> Bsb_exception.invalid_spec (filename ^ " expect a json object {}")

let deps_from_bsconfig () =
let json = Ext_json_parse.parse_json_from_file Literals.bsconfig_json in
match json with
| Obj { map } ->
( Bsb_package_specs.from_map ~cwd:Bsb_global_paths.cwd map,
let cwd = Bsb_global_paths.cwd in
match Bsb_config_load.load_json ~per_proj_dir:cwd ~warn_legacy_config:false
with
| _, Obj { map } ->
( Bsb_package_specs.from_map ~cwd map,
Bsb_jsx.from_map map,
extract_uncurried map,
Bsb_build_util.extract_pinned_dependencies map )
| _ -> assert false
| _, _ -> assert false
6 changes: 5 additions & 1 deletion jscomp/bsb/bsb_config_parse.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@
val deps_from_bsconfig : unit -> Bsb_package_specs.t * Bsb_jsx.t * bool * Set_string.t

val interpret_json :
package_kind:Bsb_package_kind.t -> per_proj_dir:string -> Bsb_config_types.t
filename:string ->
json:Ext_json_types.t ->
package_kind:Bsb_package_kind.t ->
per_proj_dir:string ->
Bsb_config_types.t
2 changes: 2 additions & 0 deletions jscomp/bsb/bsb_config_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,6 @@ type t = {
(* note when used as a dev mode, we will always ignore it *)
gentype_config : gentype_config;
uncurried: bool;

filename: string;
}
10 changes: 7 additions & 3 deletions jscomp/bsb/bsb_ninja_regen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ let ( // ) = Ext_path.combine
return None if we dont need regenerate
otherwise return Some info
*)
let regenerate_ninja ~(package_kind : Bsb_package_kind.t) ~forced ~per_proj_dir
let regenerate_ninja ~(package_kind : Bsb_package_kind.t) ~forced ~per_proj_dir ~warn_legacy_config
: Bsb_config_types.t option =
let lib_artifacts_dir = Bsb_config.lib_bs in
let lib_bs_dir = per_proj_dir // lib_artifacts_dir in
let output_deps = lib_bs_dir // bsdeps in
let check_result =
Bsb_ninja_check.check ~package_kind ~per_proj_dir ~forced ~file:output_deps
in
let config_filename, config_json =
Bsb_config_load.load_json ~per_proj_dir ~warn_legacy_config
in
match check_result with
| Good -> None (* Fast path, no need regenerate ninja *)
| Bsb_forced | Bsb_bsc_version_mismatch | Bsb_package_kind_inconsistent
Expand All @@ -52,7 +55,8 @@ let regenerate_ninja ~(package_kind : Bsb_package_kind.t) ~forced ~per_proj_dir
Bsb_clean.clean_self per_proj_dir);

let config : Bsb_config_types.t =
Bsb_config_parse.interpret_json ~package_kind ~per_proj_dir
Bsb_config_parse.interpret_json
~filename:config_filename ~json:config_json ~package_kind ~per_proj_dir
in
(* create directory, lib/bs, lib/js, lib/es6 etc *)
Bsb_build_util.mkp lib_bs_dir;
Expand All @@ -75,5 +79,5 @@ let regenerate_ninja ~(package_kind : Bsb_package_kind.t) ~forced ~per_proj_dir
since it may add files in the future *)
Bsb_ninja_check.record ~package_kind ~per_proj_dir ~config
~file:output_deps
(Literals.bsconfig_json :: config.file_groups.globbed_dirs);
(config.filename :: config.file_groups.globbed_dirs);
Some config
1 change: 1 addition & 0 deletions jscomp/bsb/bsb_ninja_regen.mli
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ val regenerate_ninja :
package_kind:Bsb_package_kind.t ->
forced:bool ->
per_proj_dir:string ->
warn_legacy_config:bool ->
Bsb_config_types.t option
(** Regenerate ninja file by need based on [.bsdeps]
return None if we dont need regenerate
Expand Down
3 changes: 2 additions & 1 deletion jscomp/bsb/bsb_world.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let make_world_deps cwd (config : Bsb_config_types.t option)
let package_specs, jsx, uncurried, pinned_dependencies =
match config with
| None ->
(* When this running bsb does not read bsconfig.json,
(* When this running bsb does not read rescript.json,
we will read such json file to know which [package-specs]
it wants
*)
Expand Down Expand Up @@ -70,6 +70,7 @@ let make_world_deps cwd (config : Bsb_config_types.t option)
(if is_pinned then Pinned_dependency { package_specs; jsx; uncurried }
else Dependency { package_specs; jsx; uncurried })
~per_proj_dir:proj_dir ~forced:false
~warn_legacy_config:false
in
let command =
{ Bsb_unix.cmd = vendor_ninja; cwd = lib_bs_dir; args }
Expand Down
21 changes: 16 additions & 5 deletions jscomp/bsb_exe/rescript_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,12 @@ let build_subcommand ~start argv argv_len =
| [| "-h" |] -> ninja_command_exit ninja_args
| _ ->
let config_opt =
Bsb_ninja_regen.regenerate_ninja ~package_kind:Toplevel
~per_proj_dir:Bsb_global_paths.cwd ~forced:!force_regenerate in
Bsb_ninja_regen.regenerate_ninja
~package_kind:Toplevel
~per_proj_dir:Bsb_global_paths.cwd
~forced:!force_regenerate
~warn_legacy_config:true
in
if not !no_deps_mode then Bsb_world.make_world_deps Bsb_global_paths.cwd config_opt ninja_args;
if !do_install then install_target ();
ninja_command_exit ninja_args
Expand Down Expand Up @@ -171,8 +175,11 @@ let info_subcommand ~start argv =
| [] -> ());
if !list_files then
match
Bsb_ninja_regen.regenerate_ninja ~package_kind:Toplevel ~forced:true
Bsb_ninja_regen.regenerate_ninja
~package_kind:Toplevel
~per_proj_dir:Bsb_global_paths.cwd
~forced:true
~warn_legacy_config:true
with
| None -> assert false
| Some { file_groups = { files } } ->
Expand All @@ -198,8 +205,12 @@ let () =
if argv_len = 1 then (
(* specialize this path which is used in watcher *)
let config_opt =
Bsb_ninja_regen.regenerate_ninja ~package_kind:Toplevel ~forced:false
~per_proj_dir:Bsb_global_paths.cwd in
Bsb_ninja_regen.regenerate_ninja
~package_kind:Toplevel
~per_proj_dir:Bsb_global_paths.cwd
~forced:false
~warn_legacy_config:true
in
Bsb_world.make_world_deps Bsb_global_paths.cwd config_opt [||];
ninja_command_exit [||])
else
Expand Down
8 changes: 8 additions & 0 deletions jscomp/build_tests/warn_legacy_config/bsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "warn_legacy_config",
"version": "0.1.0",
"sources": {
"dir": "src",
"subdirs": true
}
}
10 changes: 10 additions & 0 deletions jscomp/build_tests/warn_legacy_config/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { spawnSync } = require("child_process");
const assert = require("assert");
const rescript_exe = require("../../../scripts/bin_path").rescript_exe;

const output = spawnSync(rescript_exe, { encoding: "utf8" });
assert(
/^Warning: bsconfig.json is deprecated. Migrate it to rescript.json/.test(
output.stdout
)
);
1 change: 1 addition & 0 deletions jscomp/build_tests/warn_legacy_config/src/demo.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let () = Js.log("Hello, ReScript")
29 changes: 16 additions & 13 deletions jscomp/ext/ext_path.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(* Copyright (C) 2017 Hongbo Zhang, Authors of ReScript
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
Expand All @@ -17,7 +17,7 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
Expand Down Expand Up @@ -117,7 +117,7 @@ let ( // ) x y =
split_aux "//ghosg//ghsogh/";;
- : string * string list = ("/", ["ghosg"; "ghsogh"])
]}
Note that
Note that
{[
Filename.dirname "/a/" = "/"
Filename.dirname "/a/b/" = Filename.dirname "/a/b" = "/a"
Expand All @@ -132,7 +132,7 @@ let ( // ) x y =
basename "" = "."
dirname "" = "."
dirname "" = "."
]}
]}
*)
let split_aux p =
let rec go p acc =
Expand All @@ -149,11 +149,11 @@ let split_aux p =

go p []

(**
(**
TODO: optimization
if [from] and [to] resolve to the same path, a zero-length string is returned
if [from] and [to] resolve to the same path, a zero-length string is returned

This function is useed in [es6-global] and
This function is useed in [es6-global] and
[amdjs-global] format and tailored for `rollup`
*)
let rel_normalized_absolute_path ~from to_ =
Expand Down Expand Up @@ -261,14 +261,17 @@ let concat dirname filename =
let check_suffix_case = Ext_string.ends_with

(* Input must be absolute directory *)
let rec find_root_filename ~cwd filename =
if Sys.file_exists (Filename.concat cwd filename) then cwd
let rec find_root_filename ~cwd filenames =
let file_exists = Ext_list.exists filenames (fun filename ->
Sys.file_exists (Filename.concat cwd filename))
in
if file_exists then cwd
else
let cwd' = Filename.dirname cwd in
if String.length cwd' < String.length cwd then
find_root_filename ~cwd:cwd' filename
else Ext_fmt.failwithf ~loc:__LOC__ "%s not found from %s" filename cwd
find_root_filename ~cwd:cwd' filenames
else Ext_fmt.failwithf ~loc:__LOC__ "%s not found from %s" (List.hd filenames) cwd

let find_package_json_dir cwd = find_root_filename ~cwd Literals.bsconfig_json
let find_config_dir cwd = find_root_filename ~cwd [Literals.rescript_json; Literals.bsconfig_json]

let package_dir = lazy (find_package_json_dir (Lazy.force cwd))
let package_dir = lazy (find_config_dir (Lazy.force cwd))
Loading