Skip to content

Commit 6aab920

Browse files
authored
Support rescript.json (#6382)
* add support rescript.json * migrate to rescript.json * add warning message and its test * add CHANGELOG
1 parent 167c586 commit 6aab920

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+191
-93
lines changed

CHANGELOG.md

+1

jscomp/bsb/bsb_build_util.ml

+7-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
@@ -101,7 +101,7 @@ let resolve_bsb_magic_file ~cwd ~desc p : result =
101101

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

104-
(**
104+
(**
105105
{[
106106
mkp "a/b/c/d";;
107107
mkp "/a/b/c/d"
@@ -155,9 +155,8 @@ let extract_pinned_dependencies (map : Ext_json_types.t Map_string.t) : Set_stri
155155

156156
let rec walk_all_deps_aux (visited : string Hash_string.t) (paths : string list)
157157
~(top : top) (dir : string) (queue : _ Queue.t) ~pinned_dependencies =
158-
let bsconfig_json = dir // Literals.bsconfig_json in
159-
match Ext_json_parse.parse_json_from_file bsconfig_json with
160-
| Obj { map; loc } ->
158+
match Bsb_config_load.load_json ~per_proj_dir:dir ~warn_legacy_config:false with
159+
| _, Obj { map; loc } ->
161160
let cur_package_name =
162161
match Map_string.find_opt map Bsb_build_schemas.name with
163162
| Some (Str { str; loc }) ->
@@ -183,7 +182,7 @@ let rec walk_all_deps_aux (visited : string Hash_string.t) (paths : string list)
183182
else
184183
let explore_deps (deps : string) pinned_dependencies =
185184
map
186-
|? ( deps,
185+
|? ( deps,
187186
`Arr
188187
(fun (new_packages : Ext_json_types.t array) ->
189188
Ext_array.iter new_packages (fun js ->
@@ -205,8 +204,8 @@ let rec walk_all_deps_aux (visited : string Hash_string.t) (paths : string list)
205204
| Expect_name n when Set_string.mem pinned_dependencies n -> true
206205
| _ -> false
207206
in
208-
let pinned_dependencies = match is_pinned with
209-
| true ->
207+
let pinned_dependencies = match is_pinned with
208+
| true ->
210209
let transitive_pinned_dependencies = extract_pinned_dependencies map
211210
in
212211
Set_string.union transitive_pinned_dependencies pinned_dependencies

jscomp/bsb/bsb_config_load.ml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
let ( // ) = Ext_path.combine
2+
3+
let load_json ~(per_proj_dir : string) ~(warn_legacy_config : bool)
4+
: string * Ext_json_types.t =
5+
let filename, abs, in_chan =
6+
let filename = Literals.rescript_json in
7+
let abs = (per_proj_dir // filename) in
8+
match open_in abs
9+
with
10+
| in_chan -> (filename, abs, in_chan)
11+
| exception e ->
12+
let filename = Literals.bsconfig_json in
13+
let abs = (per_proj_dir // filename) in
14+
match open_in abs
15+
with
16+
| in_chan -> (filename, abs, in_chan)
17+
| exception _ -> raise e (* forward error from rescript.json *)
18+
in
19+
if warn_legacy_config && filename = Literals.bsconfig_json then
20+
print_endline "Warning: bsconfig.json is deprecated. Migrate it to rescript.json\n";
21+
match Ext_json_parse.parse_json_from_chan abs in_chan
22+
with
23+
| v -> close_in in_chan ; (filename, v)
24+
| exception e -> close_in in_chan ; raise e

jscomp/bsb/bsb_config_load.mli

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val load_json :
2+
per_proj_dir:string -> warn_legacy_config:bool -> string * Ext_json_types.t

jscomp/bsb/bsb_config_parse.ml

+19-14
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ let extract_package_name_and_namespace (map : json_map) : string * string option
7070
- the running bsb need delete stale build artifacts
7171
(kinda check npm upgrade)
7272
73-
Note if the setup is correct:
73+
Note if the setup is correct:
7474
the running compiler and node_modules/rescript
75-
should be the same version,
76-
The exact check is that the running compiler should have a
75+
should be the same version,
76+
The exact check is that the running compiler should have a
7777
compatible runtime version installed, the location of the
7878
compiler is actually not relevant.
7979
We disable the check temporarily
@@ -235,9 +235,13 @@ let extract_js_post_build (map : json_map) cwd : string option =
235235
|> ignore;
236236
!js_post_build_cmd
237237

238-
(** ATT: make sure such function is re-entrant.
238+
(** ATT: make sure such function is re-entrant.
239239
With a given [cwd] it works anywhere*)
240-
let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
240+
let interpret_json
241+
~(filename : string)
242+
~(json : Ext_json_types.t)
243+
~(package_kind : Bsb_package_kind.t)
244+
~(per_proj_dir : string)
241245
: Bsb_config_types.t =
242246
(* we should not resolve it too early,
243247
since it is external configuration, no {!Bsb_build_util.convert_and_resolve_path}
@@ -253,8 +257,7 @@ let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
253257
1. if [build.ninja] does use [ninja] we need set a variable
254258
2. we need store it so that we can call ninja correctly
255259
*)
256-
match
257-
Ext_json_parse.parse_json_from_file (per_proj_dir // Literals.bsconfig_json)
260+
match json
258261
with
259262
| Obj { map } -> (
260263
let package_name, namespace = extract_package_name_and_namespace map in
@@ -349,17 +352,19 @@ let interpret_json ~(package_kind : Bsb_package_kind.t) ~(per_proj_dir : string)
349352
(match package_kind with
350353
| Toplevel -> extract_uncurried map
351354
| Pinned_dependency x | Dependency x -> x.uncurried);
355+
filename;
352356
}
353357
| None ->
354-
Bsb_exception.invalid_spec "no sources specified in bsconfig.json")
355-
| _ -> Bsb_exception.invalid_spec "bsconfig.json expect a json object {}"
358+
Bsb_exception.invalid_spec ("no sources specified in " ^ filename))
359+
| _ -> Bsb_exception.invalid_spec (filename ^ " expect a json object {}")
356360

357361
let deps_from_bsconfig () =
358-
let json = Ext_json_parse.parse_json_from_file Literals.bsconfig_json in
359-
match json with
360-
| Obj { map } ->
361-
( Bsb_package_specs.from_map ~cwd:Bsb_global_paths.cwd map,
362+
let cwd = Bsb_global_paths.cwd in
363+
match Bsb_config_load.load_json ~per_proj_dir:cwd ~warn_legacy_config:false
364+
with
365+
| _, Obj { map } ->
366+
( Bsb_package_specs.from_map ~cwd map,
362367
Bsb_jsx.from_map map,
363368
extract_uncurried map,
364369
Bsb_build_util.extract_pinned_dependencies map )
365-
| _ -> assert false
370+
| _, _ -> assert false

jscomp/bsb/bsb_config_parse.mli

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,8 @@
2525
val deps_from_bsconfig : unit -> Bsb_package_specs.t * Bsb_jsx.t * bool * Set_string.t
2626

2727
val interpret_json :
28-
package_kind:Bsb_package_kind.t -> per_proj_dir:string -> Bsb_config_types.t
28+
filename:string ->
29+
json:Ext_json_types.t ->
30+
package_kind:Bsb_package_kind.t ->
31+
per_proj_dir:string ->
32+
Bsb_config_types.t

jscomp/bsb/bsb_config_types.ml

+2
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,6 @@ type t = {
6565
(* note when used as a dev mode, we will always ignore it *)
6666
gentype_config : gentype_config;
6767
uncurried: bool;
68+
69+
filename: string;
6870
}

jscomp/bsb/bsb_ninja_regen.ml

+7-3
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@ let ( // ) = Ext_path.combine
3030
return None if we dont need regenerate
3131
otherwise return Some info
3232
*)
33-
let regenerate_ninja ~(package_kind : Bsb_package_kind.t) ~forced ~per_proj_dir
33+
let regenerate_ninja ~(package_kind : Bsb_package_kind.t) ~forced ~per_proj_dir ~warn_legacy_config
3434
: Bsb_config_types.t option =
3535
let lib_artifacts_dir = Bsb_config.lib_bs in
3636
let lib_bs_dir = per_proj_dir // lib_artifacts_dir in
3737
let output_deps = lib_bs_dir // bsdeps in
3838
let check_result =
3939
Bsb_ninja_check.check ~package_kind ~per_proj_dir ~forced ~file:output_deps
4040
in
41+
let config_filename, config_json =
42+
Bsb_config_load.load_json ~per_proj_dir ~warn_legacy_config
43+
in
4144
match check_result with
4245
| Good -> None (* Fast path, no need regenerate ninja *)
4346
| Bsb_forced | Bsb_bsc_version_mismatch | Bsb_package_kind_inconsistent
@@ -52,7 +55,8 @@ let regenerate_ninja ~(package_kind : Bsb_package_kind.t) ~forced ~per_proj_dir
5255
Bsb_clean.clean_self per_proj_dir);
5356

5457
let config : Bsb_config_types.t =
55-
Bsb_config_parse.interpret_json ~package_kind ~per_proj_dir
58+
Bsb_config_parse.interpret_json
59+
~filename:config_filename ~json:config_json ~package_kind ~per_proj_dir
5660
in
5761
(* create directory, lib/bs, lib/js, lib/es6 etc *)
5862
Bsb_build_util.mkp lib_bs_dir;
@@ -75,5 +79,5 @@ let regenerate_ninja ~(package_kind : Bsb_package_kind.t) ~forced ~per_proj_dir
7579
since it may add files in the future *)
7680
Bsb_ninja_check.record ~package_kind ~per_proj_dir ~config
7781
~file:output_deps
78-
(Literals.bsconfig_json :: config.file_groups.globbed_dirs);
82+
(config.filename :: config.file_groups.globbed_dirs);
7983
Some config

jscomp/bsb/bsb_ninja_regen.mli

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ val regenerate_ninja :
2626
package_kind:Bsb_package_kind.t ->
2727
forced:bool ->
2828
per_proj_dir:string ->
29+
warn_legacy_config:bool ->
2930
Bsb_config_types.t option
3031
(** Regenerate ninja file by need based on [.bsdeps]
3132
return None if we dont need regenerate

jscomp/bsb/bsb_world.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let make_world_deps cwd (config : Bsb_config_types.t option)
3030
let package_specs, jsx, uncurried, pinned_dependencies =
3131
match config with
3232
| None ->
33-
(* When this running bsb does not read bsconfig.json,
33+
(* When this running bsb does not read rescript.json,
3434
we will read such json file to know which [package-specs]
3535
it wants
3636
*)
@@ -70,6 +70,7 @@ let make_world_deps cwd (config : Bsb_config_types.t option)
7070
(if is_pinned then Pinned_dependency { package_specs; jsx; uncurried }
7171
else Dependency { package_specs; jsx; uncurried })
7272
~per_proj_dir:proj_dir ~forced:false
73+
~warn_legacy_config:false
7374
in
7475
let command =
7576
{ Bsb_unix.cmd = vendor_ninja; cwd = lib_bs_dir; args }

jscomp/bsb_exe/rescript_main.ml

+16-5
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,12 @@ let build_subcommand ~start argv argv_len =
142142
| [| "-h" |] -> ninja_command_exit ninja_args
143143
| _ ->
144144
let config_opt =
145-
Bsb_ninja_regen.regenerate_ninja ~package_kind:Toplevel
146-
~per_proj_dir:Bsb_global_paths.cwd ~forced:!force_regenerate in
145+
Bsb_ninja_regen.regenerate_ninja
146+
~package_kind:Toplevel
147+
~per_proj_dir:Bsb_global_paths.cwd
148+
~forced:!force_regenerate
149+
~warn_legacy_config:true
150+
in
147151
if not !no_deps_mode then Bsb_world.make_world_deps Bsb_global_paths.cwd config_opt ninja_args;
148152
if !do_install then install_target ();
149153
ninja_command_exit ninja_args
@@ -171,8 +175,11 @@ let info_subcommand ~start argv =
171175
| [] -> ());
172176
if !list_files then
173177
match
174-
Bsb_ninja_regen.regenerate_ninja ~package_kind:Toplevel ~forced:true
178+
Bsb_ninja_regen.regenerate_ninja
179+
~package_kind:Toplevel
175180
~per_proj_dir:Bsb_global_paths.cwd
181+
~forced:true
182+
~warn_legacy_config:true
176183
with
177184
| None -> assert false
178185
| Some { file_groups = { files } } ->
@@ -198,8 +205,12 @@ let () =
198205
if argv_len = 1 then (
199206
(* specialize this path which is used in watcher *)
200207
let config_opt =
201-
Bsb_ninja_regen.regenerate_ninja ~package_kind:Toplevel ~forced:false
202-
~per_proj_dir:Bsb_global_paths.cwd in
208+
Bsb_ninja_regen.regenerate_ninja
209+
~package_kind:Toplevel
210+
~per_proj_dir:Bsb_global_paths.cwd
211+
~forced:false
212+
~warn_legacy_config:true
213+
in
203214
Bsb_world.make_world_deps Bsb_global_paths.cwd config_opt [||];
204215
ninja_command_exit [||])
205216
else
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "warn_legacy_config",
3+
"version": "0.1.0",
4+
"sources": {
5+
"dir": "src",
6+
"subdirs": true
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const { spawnSync } = require("child_process");
2+
const assert = require("assert");
3+
const rescript_exe = require("../../../scripts/bin_path").rescript_exe;
4+
5+
const output = spawnSync(rescript_exe, { encoding: "utf8" });
6+
assert(
7+
/^Warning: bsconfig.json is deprecated. Migrate it to rescript.json/.test(
8+
output.stdout
9+
)
10+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
let () = Js.log("Hello, ReScript")
File renamed without changes.
File renamed without changes.

jscomp/ext/ext_path.ml

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(* Copyright (C) 2017 Hongbo Zhang, Authors of ReScript
2-
*
2+
*
33
* This program is free software: you can redistribute it and/or modify
44
* it under the terms of the GNU Lesser General Public License as published by
55
* the Free Software Foundation, either version 3 of the License, or
@@ -17,7 +17,7 @@
1717
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1818
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1919
* GNU Lesser General Public License for more details.
20-
*
20+
*
2121
* You should have received a copy of the GNU Lesser General Public License
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
@@ -117,7 +117,7 @@ let ( // ) x y =
117117
split_aux "//ghosg//ghsogh/";;
118118
- : string * string list = ("/", ["ghosg"; "ghsogh"])
119119
]}
120-
Note that
120+
Note that
121121
{[
122122
Filename.dirname "/a/" = "/"
123123
Filename.dirname "/a/b/" = Filename.dirname "/a/b" = "/a"
@@ -132,7 +132,7 @@ let ( // ) x y =
132132
basename "" = "."
133133
dirname "" = "."
134134
dirname "" = "."
135-
]}
135+
]}
136136
*)
137137
let split_aux p =
138138
let rec go p acc =
@@ -149,11 +149,11 @@ let split_aux p =
149149

150150
go p []
151151

152-
(**
152+
(**
153153
TODO: optimization
154-
if [from] and [to] resolve to the same path, a zero-length string is returned
154+
if [from] and [to] resolve to the same path, a zero-length string is returned
155155
156-
This function is useed in [es6-global] and
156+
This function is useed in [es6-global] and
157157
[amdjs-global] format and tailored for `rollup`
158158
*)
159159
let rel_normalized_absolute_path ~from to_ =
@@ -261,14 +261,17 @@ let concat dirname filename =
261261
let check_suffix_case = Ext_string.ends_with
262262

263263
(* Input must be absolute directory *)
264-
let rec find_root_filename ~cwd filename =
265-
if Sys.file_exists (Filename.concat cwd filename) then cwd
264+
let rec find_root_filename ~cwd filenames =
265+
let file_exists = Ext_list.exists filenames (fun filename ->
266+
Sys.file_exists (Filename.concat cwd filename))
267+
in
268+
if file_exists then cwd
266269
else
267270
let cwd' = Filename.dirname cwd in
268271
if String.length cwd' < String.length cwd then
269-
find_root_filename ~cwd:cwd' filename
270-
else Ext_fmt.failwithf ~loc:__LOC__ "%s not found from %s" filename cwd
272+
find_root_filename ~cwd:cwd' filenames
273+
else Ext_fmt.failwithf ~loc:__LOC__ "%s not found from %s" (List.hd filenames) cwd
271274

272-
let find_package_json_dir cwd = find_root_filename ~cwd Literals.bsconfig_json
275+
let find_config_dir cwd = find_root_filename ~cwd [Literals.rescript_json; Literals.bsconfig_json]
273276

274-
let package_dir = lazy (find_package_json_dir (Lazy.force cwd))
277+
let package_dir = lazy (find_config_dir (Lazy.force cwd))

0 commit comments

Comments
 (0)