Skip to content
Closed
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
48 changes: 32 additions & 16 deletions jscomp/bsb/bsb_pkg.ml
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,49 @@ type t = Bsb_pkg_types.t
when resolving [ppx-flags]
*)
let make_sub_path (x : t) : string =
Literals.node_modules // Bsb_pkg_types.to_string x

Literals.node_modules // Bsb_pkg_types.to_string x

let node_path_delimiter =
if Sys.win32 then
';'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is Sys.win32

else
':'

(** It makes sense to have this function raise, when [bsb] could not resolve a package, it used to mean
a failure
*)
let resolve_bs_package_aux ~cwd (pkg : t) =
(* First try to resolve recursively from the current working directory *)
let sub_path = make_sub_path pkg in
let rec aux cwd =
let abs_marker = cwd // sub_path in
if Sys.file_exists abs_marker then abs_marker
let abs_marker = cwd // sub_path in
if Sys.file_exists abs_marker then Some(abs_marker)
else
let another_cwd = Filename.dirname cwd in (* TODO: may non-terminating when see symlinks *)
if String.length another_cwd < String.length cwd then
aux another_cwd
else (* To the end try other possiblilities *)
begin match Sys.getenv "npm_config_prefix"
// "lib" // sub_path with
| abs_marker when Sys.file_exists abs_marker ->
abs_marker
| _ ->
Bsb_exception.package_not_found ~pkg ~json:None
| exception Not_found ->
Bsb_exception.package_not_found ~pkg ~json:None
end
aux another_cwd
else
None
in
aux cwd
match aux cwd with
| Some(package_dir) -> package_dir
(* If the package can not be resolved then check if NODE_PATH is set and if set then search there*)
| None ->
let node_path =
match Sys.getenv "NODE_PATH" with
| node_path ->
Ext_string.split node_path node_path_delimiter
| exception Not_found ->
Bsb_exception.package_not_found ~pkg ~json:None
in
let check_dir dir =
match Sys.file_exists dir with
| true -> Some(dir)
| false -> None
in
match Ext_list.find_opt node_path (fun dir -> check_dir (dir // Bsb_pkg_types.to_string pkg)) with
| Some(resolved_dir) -> resolved_dir
| None -> Bsb_exception.package_not_found ~pkg ~json:None

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkout Ext_list.find_opt

module Coll = Hashtbl_make.Make(struct
type nonrec t = t
Expand Down
29 changes: 29 additions & 0 deletions jscomp/build_tests/bs_dependencies_node_path_override/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
*.exe
*.obj
*.out
*.compile
*.native
*.byte
*.cmo
*.annot
*.cmi
*.cmx
*.cmt
*.cmti
*.cma
*.a
*.cmxa
*.obj
*~
*.annot
*.cmj
*.bak
lib/bs
*.mlast
*.mliast
.vscode
.merlin
**/*.js
!node_modules
!input.js
!testcase.js
16 changes: 16 additions & 0 deletions jscomp/build_tests/bs_dependencies_node_path_override/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


# Build
```
npm run build
```

# Watch

```
npm run watch
```


# Editor
If you use `vscode`, Press `Windows + Shift + B` it will build automatically
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "bs_dependencies_node_path_override",
"version": "0.1.0",
"sources": [
{
"dir": "src",
"subdirs" : true
},
{
"dir": "examples",
"type" : "dev",
"subdirs" : true
}
],
"package-specs" : {
"module": "commonjs",
"in-source": true
},
"namespace": true,
"bs-dependencies" : [
"liba"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let v = Demo.name
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//@ts-check
var path = require('path')
var p = require('child_process')
var node_path = path.join(__dirname, "nothing_exists_here") + ":" + path.join(__dirname, "overridden_node_modules")
p.execSync(`NODE_PATH=${node_path} node ./testcase.js`, { cwd: __dirname, shell: true, encoding: 'utf8' })

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions jscomp/build_tests/bs_dependencies_node_path_override/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "bs_dependencies_node_path_override",
"version": "0.1.0",
"scripts": {
"clean": "bsb -clean-world",
"build": "bsb -make-world",
"watch": "bsb -make-world -w"
},
"keywords": [
"BuckleScript"
],
"license": "MIT",
"devDependencies": {
"bs-platform": "^5.1.0-dev.3"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@


let name = __FILE__ ^ Liba.Demo.name
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//@ts-check
var assert = require('assert')
var path = require('path')
var p = require('child_process')
p.execSync(`bsb -make-world`, { cwd: __dirname, shell: true, encoding: 'utf8' })
var u = require("./examples/test.js")
assert.equal(path.basename(u.v), 'demo.mldemo.ml')
48 changes: 32 additions & 16 deletions lib/4.02.3/bsb.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5214,33 +5214,49 @@ type t = Bsb_pkg_types.t
when resolving [ppx-flags]
*)
let make_sub_path (x : t) : string =
Literals.node_modules // Bsb_pkg_types.to_string x

Literals.node_modules // Bsb_pkg_types.to_string x

let node_path_delimiter =
if Sys.win32 then
';'
else
':'

(** It makes sense to have this function raise, when [bsb] could not resolve a package, it used to mean
a failure
*)
let resolve_bs_package_aux ~cwd (pkg : t) =
(* First try to resolve recursively from the current working directory *)
let sub_path = make_sub_path pkg in
let rec aux cwd =
let abs_marker = cwd // sub_path in
if Sys.file_exists abs_marker then abs_marker
let abs_marker = cwd // sub_path in
if Sys.file_exists abs_marker then Some(abs_marker)
else
let another_cwd = Filename.dirname cwd in (* TODO: may non-terminating when see symlinks *)
if String.length another_cwd < String.length cwd then
aux another_cwd
else (* To the end try other possiblilities *)
begin match Sys.getenv "npm_config_prefix"
// "lib" // sub_path with
| abs_marker when Sys.file_exists abs_marker ->
abs_marker
| _ ->
Bsb_exception.package_not_found ~pkg ~json:None
| exception Not_found ->
Bsb_exception.package_not_found ~pkg ~json:None
end
aux another_cwd
else
None
in
aux cwd
match aux cwd with
| Some(package_dir) -> package_dir
(* If the package can not be resolved then check if NODE_PATH is set and if set then search there*)
| None ->
let node_path =
match Sys.getenv "NODE_PATH" with
| node_path ->
Ext_string.split node_path node_path_delimiter
| exception Not_found ->
Bsb_exception.package_not_found ~pkg ~json:None
in
let check_dir dir =
match Sys.file_exists dir with
| true -> Some(dir)
| false -> None
in
match Ext_list.find_opt node_path (fun dir -> check_dir (dir // Bsb_pkg_types.to_string pkg)) with
| Some(resolved_dir) -> resolved_dir
| None -> Bsb_exception.package_not_found ~pkg ~json:None

module Coll = Hashtbl_make.Make(struct
type nonrec t = t
Expand Down
Loading