Skip to content

Commit 5c94c82

Browse files
author
Hongbo Zhang
committed
fix #639 : better error message
1 parent 9c4aa6e commit 5c94c82

17 files changed

+195
-162
lines changed

jscomp/common/bs_exception.ml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
type error =
2727
| Cmj_not_found of string
2828
| Bs_cyclic_depends of string list
29-
| Bs_duplicated_module of string * string
29+
| Bs_duplicated_module of string * string
30+
| Bs_package_not_found of string
3031
exception Error of error
3132

3233
let error err = raise (Error err)
@@ -42,7 +43,11 @@ let report_error ppf = function
4243
str
4344
| Bs_duplicated_module (a,b)
4445
->
45-
Format.fprintf ppf "The build system does not support two files with same names yet %s, %s" a b
46+
Format.fprintf ppf "The build system does not support two files with same names yet %s, %s" a b
47+
| Bs_package_not_found package
48+
->
49+
Format.fprintf ppf "Pacage %s not found or %s/lib/ocaml does not exist"
50+
package package
4651

4752
let () =
4853
Location.register_error_of_exn

jscomp/common/bs_exception.mli

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
type error =
2626
| Cmj_not_found of string
2727
| Bs_cyclic_depends of string list
28-
| Bs_duplicated_module of string * string
28+
| Bs_duplicated_module of string * string
29+
| Bs_package_not_found of string
2930
val error : error -> 'a

jscomp/common/common.mllib

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
js_config
2-
literals
32
ext_log
43
bs_loc
54
bs_exception

jscomp/ext/ext.mllib

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ ext_io
2020
string_set
2121
string_map
2222
hash_set
23-
ident_set
23+
ident_set
24+
literals

jscomp/ext/ext_filename.ml

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,6 @@ let relative_path file_or_dir_1 file_or_dir_2 =
136136

137137

138138

139-
let node_modules = "node_modules"
140-
let node_modules_length = String.length "node_modules"
141-
let package_json = "package.json"
142139

143140

144141

@@ -153,7 +150,7 @@ let package_json = "package.json"
153150
*)
154151
let node_relative_path (file1 : t)
155152
(`File file2 as dep_file : [`File of string]) =
156-
let v = Ext_string.find file2 ~sub:node_modules in
153+
let v = Ext_string.find file2 ~sub:Literals.node_modules in
157154
let len = String.length file2 in
158155
if v >= 0 then
159156
let rec skip i =
@@ -174,7 +171,7 @@ let node_relative_path (file1 : t)
174171
*)
175172
in
176173
Ext_string.tail_from file2
177-
(skip (v + node_modules_length))
174+
(skip (v + Literals.node_modules_length))
178175
else
179176
relative_path
180177
(absolute_path dep_file)
@@ -184,43 +181,11 @@ let node_relative_path (file1 : t)
184181

185182

186183

187-
(** [resolve cwd module_name],
188-
[cwd] is current working directory, absolute path
189-
Trying to find paths to load [module_name]
190-
it is sepcialized for option [-bs-package-include] which requires
191-
[npm_package_name/lib/ocaml]
192-
*)
193-
let resolve_bs_package ~cwd name =
194-
let sub_path = name // "lib" // "ocaml" in
195-
let rec aux origin cwd name =
196-
let destdir = cwd // node_modules // sub_path in
197-
if Ext_sys.is_directory_no_exn destdir then destdir
198-
else
199-
let cwd' = Filename.dirname cwd in
200-
if String.length cwd' < String.length cwd then
201-
aux origin cwd' name
202-
else
203-
try
204-
let destdir =
205-
Sys.getenv "npm_config_prefix"
206-
// "lib" // node_modules // sub_path in
207-
if Ext_sys.is_directory_no_exn destdir
208-
then destdir
209-
else
210-
Ext_pervasives.failwithf
211-
~loc:__LOC__ " %s not found in %s" name origin
212-
213-
with
214-
Not_found ->
215-
Ext_pervasives.failwithf
216-
~loc:__LOC__ " %s not found in %s" name origin
217-
in
218-
aux cwd cwd name
219184

220185

221186
let find_package_json_dir cwd =
222187
let rec aux cwd =
223-
if Sys.file_exists (cwd // package_json) then cwd
188+
if Sys.file_exists (cwd // Literals.package_json) then cwd
224189
else
225190
let cwd' = Filename.dirname cwd in
226191
if String.length cwd' < String.length cwd then

jscomp/ext/ext_filename.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ val node_relative_path : t -> [`File of string] -> string
5555
val chop_extension : ?loc:string -> string -> string
5656

5757

58-
val resolve_bs_package : cwd:string -> string -> string
58+
5959

6060

6161

jscomp/common/literals.ml renamed to jscomp/ext/literals.ml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,10 @@ let js_fn_runmethod = "js_fn_runmethod"
6868
let bs_deriving = "bs.deriving"
6969
let bs_deriving_dot = "bs.deriving."
7070
let bs_type = "bs.type"
71+
72+
73+
(** nodejs *)
74+
let node_modules = "node_modules"
75+
let node_modules_length = String.length "node_modules"
76+
let package_json = "package.json"
77+

jscomp/common/literals.mli renamed to jscomp/ext/literals.mli

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,9 @@ val js_fn_runmethod : string
6969
val bs_deriving : string
7070
val bs_deriving_dot : string
7171
val bs_type : string
72+
73+
(** nodejs *)
74+
75+
val node_modules : string
76+
val node_modules_length : int
77+
val package_json : string

jscomp/js_main.ml

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,43 @@ let set_main_entry name =
4848

4949

5050

51-
52-
53-
let add_include_path s =
51+
let (//) = Filename.concat
52+
(** [resolve cwd module_name],
53+
[cwd] is current working directory, absolute path
54+
Trying to find paths to load [module_name]
55+
it is sepcialized for option [-bs-package-include] which requires
56+
[npm_package_name/lib/ocaml]
57+
*)
58+
let resolve_bs_package ~cwd name =
59+
let sub_path = name // "lib" // "ocaml" in
60+
let rec aux origin cwd name =
61+
let destdir = cwd // Literals.node_modules // sub_path in
62+
if Ext_sys.is_directory_no_exn destdir then destdir
63+
else
64+
let cwd' = Filename.dirname cwd in
65+
if String.length cwd' < String.length cwd then
66+
aux origin cwd' name
67+
else
68+
try
69+
let destdir =
70+
Sys.getenv "npm_config_prefix"
71+
// "lib" // Literals.node_modules // sub_path in
72+
if Ext_sys.is_directory_no_exn destdir
73+
then destdir
74+
else
75+
Bs_exception.error (Bs_package_not_found name)
76+
with
77+
Not_found ->
78+
Bs_exception.error (Bs_package_not_found name)
79+
in
80+
aux cwd cwd name
81+
82+
let add_package s =
5483
let path =
55-
Ext_filename.resolve_bs_package
84+
resolve_bs_package
5685
~cwd:(Lazy.force Ext_filename.cwd) s in
57-
if Ext_sys.is_directory_no_exn path then
58-
Clflags.include_dirs := path :: ! Clflags.include_dirs
59-
else
60-
Ext_pervasives.failwithf ~loc:__LOC__ "%s is not a directory" s
86+
Clflags.include_dirs := path :: ! Clflags.include_dirs
87+
6188

6289
let set_noassert () =
6390
Js_config.set_no_any_assert ();
@@ -79,7 +106,7 @@ let buckle_script_flags =
79106
" set npm-output-path: [opt_module]:path, for example: 'lib/cjs', 'amdjs:lib/amdjs' and 'goog:lib/gjs'")
80107
::
81108
("-bs-package-include",
82-
Arg.String add_include_path,
109+
Arg.String add_package,
83110
" set package names, for example bs-platform " )
84111
::
85112
("-bs-no-builtin-ppx-ml",

jscomp/test/.depend

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ infer_type_test.cmi : ../runtime/js.cmj
1515
inline_edge_cases.cmi :
1616
inline_map_test.cmi :
1717
js_dyn.cmi :
18+
literals.cmi :
1819
map_test.cmi :
1920
mt.cmi :
2021
mt_global.cmi : mt.cmi
@@ -184,12 +185,12 @@ ext_array.cmj : ../stdlib/list.cmi ../stdlib/array.cmi
184185
ext_array.cmx : ../stdlib/list.cmx ../stdlib/array.cmx
185186
ext_bytes.cmj : ../stdlib/bytes.cmi
186187
ext_bytes.cmx : ../stdlib/bytes.cmx
187-
ext_filename.cmj : ../stdlib/sys.cmi ../stdlib/string.cmi ../stdlib/list.cmi \
188-
../stdlib/lazy.cmi ../stdlib/filename.cmi ext_sys.cmi ext_string.cmj \
189-
ext_pervasives.cmi
190-
ext_filename.cmx : ../stdlib/sys.cmx ../stdlib/string.cmx ../stdlib/list.cmx \
191-
../stdlib/lazy.cmx ../stdlib/filename.cmx ext_sys.cmx ext_string.cmx \
192-
ext_pervasives.cmx
188+
ext_filename.cmj : ../stdlib/sys.cmi ../stdlib/string.cmi literals.cmi \
189+
../stdlib/list.cmi ../stdlib/lazy.cmi ../stdlib/filename.cmi \
190+
ext_string.cmj ext_pervasives.cmi
191+
ext_filename.cmx : ../stdlib/sys.cmx ../stdlib/string.cmx literals.cmx \
192+
../stdlib/list.cmx ../stdlib/lazy.cmx ../stdlib/filename.cmx \
193+
ext_string.cmx ext_pervasives.cmx
193194
ext_list.cmj : ../stdlib/list.cmi ../stdlib/array.cmi
194195
ext_list.cmx : ../stdlib/list.cmx ../stdlib/array.cmx
195196
ext_log.cmj : ../stdlib/format.cmi
@@ -362,6 +363,8 @@ list_test.cmj : ../stdlib/pervasives.cmi mt.cmi ../stdlib/list.cmi \
362363
../stdlib/array.cmi
363364
list_test.cmx : ../stdlib/pervasives.cmx mt.cmx ../stdlib/list.cmx \
364365
../stdlib/array.cmx
366+
literals.cmj : ../stdlib/string.cmi literals.cmi
367+
literals.cmx : ../stdlib/string.cmx literals.cmi
365368
local_class_type.cmj : ../runtime/js.cmj
366369
local_class_type.cmx : ../runtime/js.cmx
367370
local_exception_test.cmj :
@@ -944,12 +947,12 @@ ext_array.cmo : ../stdlib/list.cmi ../stdlib/array.cmi
944947
ext_array.cmj : ../stdlib/list.cmj ../stdlib/array.cmj
945948
ext_bytes.cmo : ../stdlib/bytes.cmi
946949
ext_bytes.cmj : ../stdlib/bytes.cmj
947-
ext_filename.cmo : ../stdlib/sys.cmi ../stdlib/string.cmi ../stdlib/list.cmi \
948-
../stdlib/lazy.cmi ../stdlib/filename.cmi ext_sys.cmi ext_string.cmo \
949-
ext_pervasives.cmi
950-
ext_filename.cmj : ../stdlib/sys.cmj ../stdlib/string.cmj ../stdlib/list.cmj \
951-
../stdlib/lazy.cmj ../stdlib/filename.cmj ext_sys.cmj ext_string.cmj \
952-
ext_pervasives.cmj
950+
ext_filename.cmo : ../stdlib/sys.cmi ../stdlib/string.cmi literals.cmi \
951+
../stdlib/list.cmi ../stdlib/lazy.cmi ../stdlib/filename.cmi \
952+
ext_string.cmo ext_pervasives.cmi
953+
ext_filename.cmj : ../stdlib/sys.cmj ../stdlib/string.cmj literals.cmj \
954+
../stdlib/list.cmj ../stdlib/lazy.cmj ../stdlib/filename.cmj \
955+
ext_string.cmj ext_pervasives.cmj
953956
ext_list.cmo : ../stdlib/list.cmi ../stdlib/array.cmi
954957
ext_list.cmj : ../stdlib/list.cmj ../stdlib/array.cmj
955958
ext_log.cmo : ../stdlib/format.cmi
@@ -1122,6 +1125,8 @@ list_test.cmo : ../stdlib/pervasives.cmi mt.cmi ../stdlib/list.cmi \
11221125
../stdlib/array.cmi
11231126
list_test.cmj : ../stdlib/pervasives.cmj mt.cmj ../stdlib/list.cmj \
11241127
../stdlib/array.cmj
1128+
literals.cmo : ../stdlib/string.cmi literals.cmi
1129+
literals.cmj : ../stdlib/string.cmj literals.cmi
11251130
local_class_type.cmo : ../runtime/js.cmo
11261131
local_class_type.cmj : ../runtime/js.cmj
11271132
local_exception_test.cmo :

0 commit comments

Comments
 (0)