Skip to content

Commit 01d656e

Browse files
committed
continue clean up path handling
1 parent 4db21e8 commit 01d656e

10 files changed

+126
-235
lines changed

jscomp/bin/all_ounit_tests.ml

+18-7
Original file line numberDiff line numberDiff line change
@@ -11258,12 +11258,18 @@ type t =
1125811258

1125911259
val sep_char : char
1126011260

11261-
val node_relative_path :
11261+
(* val node_relative_path :
1126211262
from:t ->
1126311263
t ->
11264-
string
11264+
string *)
1126511265

11266-
val node_concat : dir:string -> string -> string
11266+
(* val node_concat : dir:string -> string -> string *)
11267+
11268+
val node_rebase_file :
11269+
from:string ->
11270+
to_:string ->
11271+
string ->
11272+
string
1126711273

1126811274
(**
1126911275
1. add some simplifications when concatenating
@@ -11405,7 +11411,12 @@ let node_relative_path
1140511411
let node_concat ~dir base =
1140611412
dir ^ Literals.node_sep ^ base
1140711413

11408-
11414+
let node_rebase_file ~from ~to_ file =
11415+
node_concat
11416+
~dir:(node_relative_path ~from:(Dir from) (Dir to_))
11417+
file
11418+
11419+
1140911420
(***
1141011421
{[
1141111422
Filename.concat "." "";;
@@ -11519,7 +11530,7 @@ let rel_normalized_absolute_path ~from to_ =
1151911530
List.fold_left (fun acc _ -> acc // Ext_string.parent_dir_lit )
1152011531
Ext_string.parent_dir_lit xs in
1152111532
let v = go paths1 paths2 in
11522-
11533+
1152311534
if Ext_string.is_empty v then Literals.node_current
1152411535
else
1152511536
if
@@ -11722,7 +11733,7 @@ let suites =
1172211733
"./node_modules/xx/./xx.js" =~ "./node_modules/xx/xx.js"
1172311734
end;
1172411735

11725-
__LOC__ >:: begin fun _ ->
11736+
(* __LOC__ >:: begin fun _ ->
1172611737
Ext_path.node_relative_path
1172711738
(Dir "lib/js/src/a")
1172811739
~from:(Dir "lib/js/src") =~ "./a" ;
@@ -11748,7 +11759,7 @@ let suites =
1174811759
(Dir "lib/js/src/a/")
1174911760
~from:(Dir "lib/js/src/a/")
1175011761
=~ "."
11751-
end
11762+
end *)
1175211763
]
1175311764

1175411765
end

jscomp/bin/bsb.ml

+16-55
Original file line numberDiff line numberDiff line change
@@ -4748,12 +4748,18 @@ type t =
47484748

47494749
val sep_char : char
47504750

4751-
val node_relative_path :
4751+
(* val node_relative_path :
47524752
from:t ->
47534753
t ->
4754-
string
4754+
string *)
4755+
4756+
(* val node_concat : dir:string -> string -> string *)
47554757

4756-
val node_concat : dir:string -> string -> string
4758+
val node_rebase_file :
4759+
from:string ->
4760+
to_:string ->
4761+
string ->
4762+
string
47574763

47584764
(**
47594765
1. add some simplifications when concatenating
@@ -4895,7 +4901,12 @@ let node_relative_path
48954901
let node_concat ~dir base =
48964902
dir ^ Literals.node_sep ^ base
48974903

4898-
4904+
let node_rebase_file ~from ~to_ file =
4905+
node_concat
4906+
~dir:(node_relative_path ~from:(Dir from) (Dir to_))
4907+
file
4908+
4909+
48994910
(***
49004911
{[
49014912
Filename.concat "." "";;
@@ -5009,7 +5020,7 @@ let rel_normalized_absolute_path ~from to_ =
50095020
List.fold_left (fun acc _ -> acc // Ext_string.parent_dir_lit )
50105021
Ext_string.parent_dir_lit xs in
50115022
let v = go paths1 paths2 in
5012-
5023+
50135024
if Ext_string.is_empty v then Literals.node_current
50145025
else
50155026
if
@@ -8986,10 +8997,6 @@ module Ext_filename : sig
89868997
just treat it as a library instead
89878998
*)
89888999

8989-
(* val node_relative_path :
8990-
bool ->
8991-
from:Ext_path.t ->
8992-
string -> string *)
89939000
val cwd : string Lazy.t
89949001

89959002
(* It is lazy so that it will not hit errors when in script mode *)
@@ -9037,52 +9044,6 @@ type t = Ext_path.t
90379044
let cwd = lazy (Sys.getcwd ())
90389045

90399046

9040-
(** path2: a/b
9041-
path1: a
9042-
result: ./b
9043-
TODO: [Filename.concat] with care
9044-
9045-
[file1] is currently compilation file
9046-
[file2] is the dependency
9047-
9048-
TODO: this is a hackish function: FIXME
9049-
*)
9050-
let node_relative_path node_modules_shorten
9051-
~from:(file1 : Ext_path.t)
9052-
(file2 : string) =
9053-
let v = Ext_string.find file2 ~sub:Literals.node_modules in
9054-
let len = String.length file2 in
9055-
if node_modules_shorten && v >= 0 then
9056-
9057-
let rec skip i =
9058-
if i >= len then
9059-
Ext_pervasives.failwithf ~loc:__LOC__ "invalid path: %s" file2
9060-
else
9061-
(* https://en.wikipedia.org/wiki/Path_(computing))
9062-
most path separator are a single char
9063-
*)
9064-
let curr_char = String.unsafe_get file2 i in
9065-
if curr_char = Ext_path.sep_char || curr_char = '.' then
9066-
skip (i + 1)
9067-
else i
9068-
(*
9069-
TODO: we need do more than this suppose user
9070-
input can be
9071-
{[
9072-
"xxxghsoghos/ghsoghso/node_modules/../buckle-stdlib/list.js"
9073-
]}
9074-
This seems weird though
9075-
*)
9076-
in
9077-
Ext_string.tail_from file2
9078-
(skip (v + Literals.node_modules_length))
9079-
else
9080-
Ext_path.node_relative_path
9081-
(File (Ext_path.absolute_path cwd file2))
9082-
~from:(Ext_path.absolute cwd file1)
9083-
^ Literals.node_sep ^
9084-
(Filename.basename file2)
9085-
90869047

90879048

90889049
(* Input must be absolute directory *)

jscomp/bin/bsb_helper.ml

+16-5
Original file line numberDiff line numberDiff line change
@@ -1321,12 +1321,18 @@ type t =
13211321

13221322
val sep_char : char
13231323

1324-
val node_relative_path :
1324+
(* val node_relative_path :
13251325
from:t ->
13261326
t ->
1327-
string
1327+
string *)
13281328

1329-
val node_concat : dir:string -> string -> string
1329+
(* val node_concat : dir:string -> string -> string *)
1330+
1331+
val node_rebase_file :
1332+
from:string ->
1333+
to_:string ->
1334+
string ->
1335+
string
13301336

13311337
(**
13321338
1. add some simplifications when concatenating
@@ -1468,7 +1474,12 @@ let node_relative_path
14681474
let node_concat ~dir base =
14691475
dir ^ Literals.node_sep ^ base
14701476

1471-
1477+
let node_rebase_file ~from ~to_ file =
1478+
node_concat
1479+
~dir:(node_relative_path ~from:(Dir from) (Dir to_))
1480+
file
1481+
1482+
14721483
(***
14731484
{[
14741485
Filename.concat "." "";;
@@ -1582,7 +1593,7 @@ let rel_normalized_absolute_path ~from to_ =
15821593
List.fold_left (fun acc _ -> acc // Ext_string.parent_dir_lit )
15831594
Ext_string.parent_dir_lit xs in
15841595
let v = go paths1 paths2 in
1585-
1596+
15861597
if Ext_string.is_empty v then Literals.node_current
15871598
else
15881599
if

jscomp/bin/whole_compiler.ml

+37-83
Original file line numberDiff line numberDiff line change
@@ -23417,12 +23417,18 @@ type t =
2341723417

2341823418
val sep_char : char
2341923419

23420-
val node_relative_path :
23420+
(* val node_relative_path :
2342123421
from:t ->
2342223422
t ->
23423-
string
23423+
string *)
23424+
23425+
(* val node_concat : dir:string -> string -> string *)
2342423426

23425-
val node_concat : dir:string -> string -> string
23427+
val node_rebase_file :
23428+
from:string ->
23429+
to_:string ->
23430+
string ->
23431+
string
2342623432

2342723433
(**
2342823434
1. add some simplifications when concatenating
@@ -23564,7 +23570,12 @@ let node_relative_path
2356423570
let node_concat ~dir base =
2356523571
dir ^ Literals.node_sep ^ base
2356623572

23567-
23573+
let node_rebase_file ~from ~to_ file =
23574+
node_concat
23575+
~dir:(node_relative_path ~from:(Dir from) (Dir to_))
23576+
file
23577+
23578+
2356823579
(***
2356923580
{[
2357023581
Filename.concat "." "";;
@@ -23678,7 +23689,7 @@ let rel_normalized_absolute_path ~from to_ =
2367823689
List.fold_left (fun acc _ -> acc // Ext_string.parent_dir_lit )
2367923690
Ext_string.parent_dir_lit xs in
2368023691
let v = go paths1 paths2 in
23681-
23692+
2368223693
if Ext_string.is_empty v then Literals.node_current
2368323694
else
2368423695
if
@@ -59130,10 +59141,6 @@ module Ext_filename : sig
5913059141
just treat it as a library instead
5913159142
*)
5913259143

59133-
(* val node_relative_path :
59134-
bool ->
59135-
from:Ext_path.t ->
59136-
string -> string *)
5913759144
val cwd : string Lazy.t
5913859145

5913959146
(* It is lazy so that it will not hit errors when in script mode *)
@@ -59181,52 +59188,6 @@ type t = Ext_path.t
5918159188
let cwd = lazy (Sys.getcwd ())
5918259189

5918359190

59184-
(** path2: a/b
59185-
path1: a
59186-
result: ./b
59187-
TODO: [Filename.concat] with care
59188-
59189-
[file1] is currently compilation file
59190-
[file2] is the dependency
59191-
59192-
TODO: this is a hackish function: FIXME
59193-
*)
59194-
let node_relative_path node_modules_shorten
59195-
~from:(file1 : Ext_path.t)
59196-
(file2 : string) =
59197-
let v = Ext_string.find file2 ~sub:Literals.node_modules in
59198-
let len = String.length file2 in
59199-
if node_modules_shorten && v >= 0 then
59200-
59201-
let rec skip i =
59202-
if i >= len then
59203-
Ext_pervasives.failwithf ~loc:__LOC__ "invalid path: %s" file2
59204-
else
59205-
(* https://en.wikipedia.org/wiki/Path_(computing))
59206-
most path separator are a single char
59207-
*)
59208-
let curr_char = String.unsafe_get file2 i in
59209-
if curr_char = Ext_path.sep_char || curr_char = '.' then
59210-
skip (i + 1)
59211-
else i
59212-
(*
59213-
TODO: we need do more than this suppose user
59214-
input can be
59215-
{[
59216-
"xxxghsoghos/ghsoghso/node_modules/../buckle-stdlib/list.js"
59217-
]}
59218-
This seems weird though
59219-
*)
59220-
in
59221-
Ext_string.tail_from file2
59222-
(skip (v + Literals.node_modules_length))
59223-
else
59224-
Ext_path.node_relative_path
59225-
(File (Ext_path.absolute_path cwd file2))
59226-
~from:(Ext_path.absolute cwd file1)
59227-
^ Literals.node_sep ^
59228-
(Filename.basename file2)
59229-
5923059191

5923159192

5923259193
(* Input must be absolute directory *)
@@ -63449,15 +63410,18 @@ let string_of_module_id
6344963410
Bs_exception.error (Dependency_script_module_dependent_not js_file)
6345063411
| (Package_script | Package_found _ ), Package_not_found -> assert false
6345163412

63413+
| Package_found(dep_package_name, dep_path),
63414+
Package_script
63415+
->
63416+
dep_package_name // dep_path // js_file
6345263417

6345363418
| Package_found(dep_package_name, dep_path),
6345463419
Package_found(cur_package_name, cur_path) ->
6345563420
if cur_package_name = dep_package_name then
63456-
Ext_path.node_concat
63457-
~dir:(Ext_path.node_relative_path
63458-
~from:(Dir cur_path)
63459-
(Dir dep_path )
63460-
) js_file
63421+
Ext_path.node_rebase_file
63422+
~from:cur_path
63423+
~to_:dep_path
63424+
js_file
6346163425
(** TODO: we assume that both [x] and [path] could only be relative path
6346263426
which is guaranteed by [-bs-package-output]
6346363427
*)
@@ -63484,33 +63448,23 @@ let string_of_module_id
6348463448
(Filename.dirname (Filename.dirname cmj_path))) // dep_path // js_file)
6348563449
end
6348663450
end
63487-
| Package_found(dep_package_name, dep_path),
63488-
Package_script
63489-
->
63490-
dep_package_name // dep_path // js_file
63491-
|
63492-
Package_script ,
63451+
| Package_script ,
6349363452
Package_script
6349463453
->
6349563454
begin match Config_util.find_opt js_file with
6349663455
| Some file ->
63497-
(* let package_dir = Lazy.force Ext_filename.package_dir in *)
63498-
(* let rebase ~dependency ~different_package package_dir=
63499-
Ext_filename.node_relative_path
63500-
different_package
63501-
~from:(Dir output_dir)
63502-
dependency
63503-
in *)
63504-
(* rebase ~different_package:true package_dir ~dependency:file *)
63505-
Ext_path.node_concat
63506-
~dir:(Ext_path.node_relative_path
63507-
~from:(Ext_path.absolute
63508-
Ext_filename.cwd (Dir output_dir))
63509-
(File(Ext_path.absolute_path Ext_filename.cwd file)))
63510-
(Filename.basename file)
63511-
(* Code path: when dependency is commonjs
63512-
while depedent is Empty or PackageScript
63513-
*)
63456+
let basename = Filename.basename file in
63457+
let dirname = Filename.dirname file in
63458+
Ext_path.node_rebase_file
63459+
~from:(
63460+
Ext_path.absolute_path
63461+
Ext_filename.cwd output_dir)
63462+
~to_:(
63463+
Ext_path.absolute_path
63464+
Ext_filename.cwd
63465+
dirname
63466+
)
63467+
basename
6351463468
| None ->
6351563469
Bs_exception.error (Js_not_found js_file)
6351663470
end

0 commit comments

Comments
 (0)