From a6a00fe78cfd0ffbb4fde50e83976f1801e2ce12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Poul=20Purkh=C3=BAs?= Date: Wed, 24 Jul 2019 15:15:56 +0000 Subject: [PATCH 01/10] Respect NODE_PATH when resolving modules This allows the usage of NODE_PATH(https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders) when `bsb` is resolving modules --- jscomp/bsb/bsb_pkg.ml | 25 +++++++++++++---- .../.gitignore | 28 +++++++++++++++++++ .../README.md | 16 +++++++++++ .../bsconfig.json | 23 +++++++++++++++ .../examples/test.ml | 1 + .../input.js | 6 ++++ .../overridden_node_modules/liba/.gitignore | 25 +++++++++++++++++ .../overridden_node_modules/liba/README.md | 16 +++++++++++ .../liba/bsconfig.json | 17 +++++++++++ .../liba/examples/xx/test.ml | 1 + .../liba/examples/yy.ml | 1 + .../overridden_node_modules/liba/package.json | 16 +++++++++++ .../overridden_node_modules/liba/src/demo.ml | 3 ++ .../liba/src/hi/fib.ml | 1 + .../package.json | 16 +++++++++++ .../src/demo.ml | 3 ++ 16 files changed, 193 insertions(+), 5 deletions(-) create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/.gitignore create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/README.md create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/bsconfig.json create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/examples/test.ml create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/input.js create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/.gitignore create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/README.md create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/bsconfig.json create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/examples/xx/test.ml create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/examples/yy.ml create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/package.json create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/src/demo.ml create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/src/hi/fib.ml create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/package.json create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/src/demo.ml diff --git a/jscomp/bsb/bsb_pkg.ml b/jscomp/bsb/bsb_pkg.ml index ddbb169255..fec7b4f1f7 100644 --- a/jscomp/bsb/bsb_pkg.ml +++ b/jscomp/bsb/bsb_pkg.ml @@ -39,10 +39,11 @@ let make_sub_path (x : t) : string = 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 + 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 @@ -51,14 +52,28 @@ let resolve_bs_package_aux ~cwd (pkg : t) = begin match Sys.getenv "npm_config_prefix" // "lib" // sub_path with | abs_marker when Sys.file_exists abs_marker -> - abs_marker + Some(abs_marker) | _ -> - Bsb_exception.package_not_found ~pkg ~json:None + None | exception Not_found -> - Bsb_exception.package_not_found ~pkg ~json:None + None end 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 -> + Str.split (Str.regexp ";") node_path + | exception Not_found -> + Bsb_exception.package_not_found ~pkg ~json:None + in + match List.find (fun dir -> Sys.file_exists (dir // Bsb_pkg_types.to_string pkg)) node_path with + | resolved_dir -> resolved_dir // Bsb_pkg_types.to_string pkg + | exception Not_found -> + Bsb_exception.package_not_found ~pkg ~json:None module Coll = Hashtbl_make.Make(struct type nonrec t = t diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/.gitignore b/jscomp/build_tests/bs_dependencies_node_path_override/.gitignore new file mode 100644 index 0000000000..55ca6f15e9 --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/.gitignore @@ -0,0 +1,28 @@ +*.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 \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/README.md b/jscomp/build_tests/bs_dependencies_node_path_override/README.md new file mode 100644 index 0000000000..1c02d2a072 --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/README.md @@ -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 \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/bsconfig.json b/jscomp/build_tests/bs_dependencies_node_path_override/bsconfig.json new file mode 100644 index 0000000000..9ba8ba4e0c --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/bsconfig.json @@ -0,0 +1,23 @@ +{ + "name": "dev", + "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" + ] +} \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/examples/test.ml b/jscomp/build_tests/bs_dependencies_node_path_override/examples/test.ml new file mode 100644 index 0000000000..f5f8f9d8c1 --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/examples/test.ml @@ -0,0 +1 @@ +let v = Demo.name \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/input.js b/jscomp/build_tests/bs_dependencies_node_path_override/input.js new file mode 100644 index 0000000000..2f0981f7ef --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/input.js @@ -0,0 +1,6 @@ +//@ts-check +var assert = require('assert') +var path = require('path') +var p = require('child_process') +var node_path = path.join(__dirname, "overridden_node_modules") +p.execSync(`NODE_PATH=${node_path} node ./testcase.js`, {cwd:__dirname,shell:true,encoding:'utf8'}) \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/.gitignore b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/.gitignore new file mode 100644 index 0000000000..8f464d0527 --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/.gitignore @@ -0,0 +1,25 @@ +*.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 \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/README.md b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/README.md new file mode 100644 index 0000000000..1c02d2a072 --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/README.md @@ -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 \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/bsconfig.json b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/bsconfig.json new file mode 100644 index 0000000000..f34d7ed09e --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/bsconfig.json @@ -0,0 +1,17 @@ +{ + "name": "liba", + "version": "0.1.0", + "sources": [ + { + "dir": "src", + "subdirs" : true + }, + { + "dir" : "examples", + "type": "dev", + "subdirs" : true + } + ], + "namespace": true + +} \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/examples/xx/test.ml b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/examples/xx/test.ml new file mode 100644 index 0000000000..af158a09e6 --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/examples/xx/test.ml @@ -0,0 +1 @@ +let name = __FILE__ + 3 \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/examples/yy.ml b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/examples/yy.ml new file mode 100644 index 0000000000..d32a6d0646 --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/examples/yy.ml @@ -0,0 +1 @@ +let name = __FILE__ \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/package.json b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/package.json new file mode 100644 index 0000000000..63c69448e0 --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/package.json @@ -0,0 +1,16 @@ +{ + "name": "liba", + "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": "1.9.3" + } +} \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/src/demo.ml b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/src/demo.ml new file mode 100644 index 0000000000..ca8c589c96 --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/src/demo.ml @@ -0,0 +1,3 @@ + + +let name = __FILE__ \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/src/hi/fib.ml b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/src/hi/fib.ml new file mode 100644 index 0000000000..d32a6d0646 --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/overridden_node_modules/liba/src/hi/fib.ml @@ -0,0 +1 @@ +let name = __FILE__ \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/package.json b/jscomp/build_tests/bs_dependencies_node_path_override/package.json new file mode 100644 index 0000000000..a3a03aa1d2 --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/package.json @@ -0,0 +1,16 @@ +{ + "name": "dev", + "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": "1.9.3" + } +} \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/src/demo.ml b/jscomp/build_tests/bs_dependencies_node_path_override/src/demo.ml new file mode 100644 index 0000000000..1b2a37152f --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/src/demo.ml @@ -0,0 +1,3 @@ + + +let name = __FILE__ ^ Liba.Demo.name \ No newline at end of file From d25b99d5838e338945fe361ad25b14aa0f2faa33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Poul=20Purkh=C3=BAs?= Date: Wed, 24 Jul 2019 15:49:38 +0000 Subject: [PATCH 02/10] Add missing file --- .../bs_dependencies_node_path_override/.gitignore | 3 ++- .../bs_dependencies_node_path_override/testcase.js | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 jscomp/build_tests/bs_dependencies_node_path_override/testcase.js diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/.gitignore b/jscomp/build_tests/bs_dependencies_node_path_override/.gitignore index 55ca6f15e9..71c60460d7 100644 --- a/jscomp/build_tests/bs_dependencies_node_path_override/.gitignore +++ b/jscomp/build_tests/bs_dependencies_node_path_override/.gitignore @@ -25,4 +25,5 @@ lib/bs .merlin **/*.js !node_modules -!input.js \ No newline at end of file +!input.js +!testcase.js \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js b/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js new file mode 100644 index 0000000000..05c614f7e9 --- /dev/null +++ b/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js @@ -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') \ No newline at end of file From ad3ecbf35072b714832492cfc0c24826160c4cbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Poul=20Purkh=C3=BAs?= Date: Wed, 24 Jul 2019 16:20:24 +0000 Subject: [PATCH 03/10] Set bs-platform version --- .../build_tests/bs_dependencies_node_path_override/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/package.json b/jscomp/build_tests/bs_dependencies_node_path_override/package.json index a3a03aa1d2..ddf20950d8 100644 --- a/jscomp/build_tests/bs_dependencies_node_path_override/package.json +++ b/jscomp/build_tests/bs_dependencies_node_path_override/package.json @@ -11,6 +11,6 @@ ], "license": "MIT", "devDependencies": { - "bs-platform": "1.9.3" + "bs-platform": "^5.1.0-dev.3" } } \ No newline at end of file From a058ff54cc017efa627d59b062cf6c843c8b7adf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Poul=20Purkh=C3=BAs?= Date: Fri, 26 Jul 2019 11:22:54 +0000 Subject: [PATCH 04/10] Removed `npm_config_prefix` option when resolving packages Fixes after review --- jscomp/bsb/bsb_pkg.ml | 41 ++++++++++--------- .../bsconfig.json | 2 +- .../input.js | 5 +-- .../package.json | 2 +- .../testcase.js | 2 +- 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/jscomp/bsb/bsb_pkg.ml b/jscomp/bsb/bsb_pkg.ml index fec7b4f1f7..59fbe5a2aa 100644 --- a/jscomp/bsb/bsb_pkg.ml +++ b/jscomp/bsb/bsb_pkg.ml @@ -32,8 +32,13 @@ 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.os_type = "Win32" then + ';' + else + ':' (** It makes sense to have this function raise, when [bsb] could not resolve a package, it used to mean a failure @@ -42,22 +47,14 @@ 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 + 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 -> - Some(abs_marker) - | _ -> - None - | exception Not_found -> - None - end + aux another_cwd + else + None in match aux cwd with | Some(package_dir) -> package_dir @@ -66,14 +63,20 @@ let resolve_bs_package_aux ~cwd (pkg : t) = let node_path = match Sys.getenv "NODE_PATH" with | node_path -> - Str.split (Str.regexp ";") node_path + print_endline node_path; + print_endline (Char.escaped node_path_delimiter); + Ext_string.split node_path node_path_delimiter | exception Not_found -> Bsb_exception.package_not_found ~pkg ~json:None in - match List.find (fun dir -> Sys.file_exists (dir // Bsb_pkg_types.to_string pkg)) node_path with - | resolved_dir -> resolved_dir // Bsb_pkg_types.to_string pkg - | exception Not_found -> - Bsb_exception.package_not_found ~pkg ~json:None + 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 diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/bsconfig.json b/jscomp/build_tests/bs_dependencies_node_path_override/bsconfig.json index 9ba8ba4e0c..5eb6a09cf2 100644 --- a/jscomp/build_tests/bs_dependencies_node_path_override/bsconfig.json +++ b/jscomp/build_tests/bs_dependencies_node_path_override/bsconfig.json @@ -1,5 +1,5 @@ { - "name": "dev", + "name": "bs_dependencies_node_path_override", "version": "0.1.0", "sources": [ { diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/input.js b/jscomp/build_tests/bs_dependencies_node_path_override/input.js index 2f0981f7ef..863ab0f765 100644 --- a/jscomp/build_tests/bs_dependencies_node_path_override/input.js +++ b/jscomp/build_tests/bs_dependencies_node_path_override/input.js @@ -1,6 +1,5 @@ //@ts-check -var assert = require('assert') var path = require('path') var p = require('child_process') -var node_path = path.join(__dirname, "overridden_node_modules") -p.execSync(`NODE_PATH=${node_path} node ./testcase.js`, {cwd:__dirname,shell:true,encoding:'utf8'}) \ No newline at end of file +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',stdio:"inherit"}) \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/package.json b/jscomp/build_tests/bs_dependencies_node_path_override/package.json index ddf20950d8..8d24a775bb 100644 --- a/jscomp/build_tests/bs_dependencies_node_path_override/package.json +++ b/jscomp/build_tests/bs_dependencies_node_path_override/package.json @@ -1,5 +1,5 @@ { - "name": "dev", + "name": "bs_dependencies_node_path_override", "version": "0.1.0", "scripts": { "clean": "bsb -clean-world", diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js b/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js index 05c614f7e9..fdf1c492a7 100644 --- a/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js +++ b/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js @@ -2,6 +2,6 @@ var assert = require('assert') var path = require('path') var p = require('child_process') -p.execSync(`bsb -make-world`, {cwd:__dirname,shell:true,encoding:'utf8'}) +p.execSync(`bsb -make-world`, {cwd:__dirname,shell:true,encoding:'utf8',stdio:"inherit"}) var u = require("./examples/test.js") assert.equal(path.basename(u.v),'demo.mldemo.ml') \ No newline at end of file From 2bcdd1e431314d6209a94576170a04d525617e5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Poul=20Purkh=C3=BAs?= Date: Fri, 26 Jul 2019 11:25:02 +0000 Subject: [PATCH 05/10] Removed unneccessary printing --- jscomp/bsb/bsb_pkg.ml | 2 -- 1 file changed, 2 deletions(-) diff --git a/jscomp/bsb/bsb_pkg.ml b/jscomp/bsb/bsb_pkg.ml index 59fbe5a2aa..488d0bba03 100644 --- a/jscomp/bsb/bsb_pkg.ml +++ b/jscomp/bsb/bsb_pkg.ml @@ -63,8 +63,6 @@ let resolve_bs_package_aux ~cwd (pkg : t) = let node_path = match Sys.getenv "NODE_PATH" with | node_path -> - print_endline node_path; - print_endline (Char.escaped node_path_delimiter); Ext_string.split node_path node_path_delimiter | exception Not_found -> Bsb_exception.package_not_found ~pkg ~json:None From 49f27e66499f6a1cc2230e20282b72aaf87f8771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Poul=20Purkh=C3=BAs?= Date: Sat, 27 Jul 2019 11:03:08 +0000 Subject: [PATCH 06/10] Use Sys.win32 --- jscomp/bsb/bsb_pkg.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jscomp/bsb/bsb_pkg.ml b/jscomp/bsb/bsb_pkg.ml index 488d0bba03..3da622e649 100644 --- a/jscomp/bsb/bsb_pkg.ml +++ b/jscomp/bsb/bsb_pkg.ml @@ -35,7 +35,7 @@ let make_sub_path (x : t) : string = Literals.node_modules // Bsb_pkg_types.to_string x let node_path_delimiter = - if Sys.os_type = "Win32" then + if Sys.win32 then ';' else ':' From 03e0bdc67ab4616086d659a91187d0062c58df4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Poul=20Purkh=C3=BAs?= Date: Sat, 27 Jul 2019 11:03:20 +0000 Subject: [PATCH 07/10] Debug ci build --- jscomp/main/bsb_main.ml | 1 + 1 file changed, 1 insertion(+) diff --git a/jscomp/main/bsb_main.ml b/jscomp/main/bsb_main.ml index ffadccf32c..ac7473bc0b 100644 --- a/jscomp/main/bsb_main.ml +++ b/jscomp/main/bsb_main.ml @@ -43,6 +43,7 @@ let bs_version_string = Bs_version.version let print_version_string () = print_string bs_version_string; + print_endline "Flurfenderf"; print_newline (); exit 0 From d92c2ed0306d7d0af74963becc77b0be28c10f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Poul=20Purkh=C3=BAs?= Date: Sat, 27 Jul 2019 11:12:12 +0000 Subject: [PATCH 08/10] Debug ci build --- .../bs_dependencies_node_path_override/testcase.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js b/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js index fdf1c492a7..5ba4a08436 100644 --- a/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js +++ b/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js @@ -2,6 +2,6 @@ var assert = require('assert') var path = require('path') var p = require('child_process') -p.execSync(`bsb -make-world`, {cwd:__dirname,shell:true,encoding:'utf8',stdio:"inherit"}) +p.execSync(`bsb -version; bsb -make-world`, { cwd: __dirname, shell: true, encoding: 'utf8', stdio: "inherit" }) var u = require("./examples/test.js") -assert.equal(path.basename(u.v),'demo.mldemo.ml') \ No newline at end of file +assert.equal(path.basename(u.v), 'demo.mldemo.ml') \ No newline at end of file From 360eb73392ba37d9ac6863f2153a4c20635c50bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Poul=20Purkh=C3=BAs?= Date: Sun, 28 Jul 2019 15:13:13 +0000 Subject: [PATCH 09/10] Commit /lib --- jscomp/main/bsb_main.ml | 1 - lib/4.02.3/bsb.ml | 48 ++++++++++++++++++++----------- lib/4.02.3/unstable/bsb_native.ml | 48 ++++++++++++++++++++----------- 3 files changed, 64 insertions(+), 33 deletions(-) diff --git a/jscomp/main/bsb_main.ml b/jscomp/main/bsb_main.ml index ac7473bc0b..ffadccf32c 100644 --- a/jscomp/main/bsb_main.ml +++ b/jscomp/main/bsb_main.ml @@ -43,7 +43,6 @@ let bs_version_string = Bs_version.version let print_version_string () = print_string bs_version_string; - print_endline "Flurfenderf"; print_newline (); exit 0 diff --git a/lib/4.02.3/bsb.ml b/lib/4.02.3/bsb.ml index aba61b5860..0f8b63e312 100644 --- a/lib/4.02.3/bsb.ml +++ b/lib/4.02.3/bsb.ml @@ -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 diff --git a/lib/4.02.3/unstable/bsb_native.ml b/lib/4.02.3/unstable/bsb_native.ml index 58a0abdc96..2faafd9808 100644 --- a/lib/4.02.3/unstable/bsb_native.ml +++ b/lib/4.02.3/unstable/bsb_native.ml @@ -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 From 94ffa31fe7626182dfb98d27473d0a6c946450dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Poul=20Purkh=C3=BAs?= Date: Sun, 28 Jul 2019 15:15:56 +0000 Subject: [PATCH 10/10] Cleanup --- jscomp/build_tests/bs_dependencies_node_path_override/input.js | 2 +- .../build_tests/bs_dependencies_node_path_override/testcase.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/input.js b/jscomp/build_tests/bs_dependencies_node_path_override/input.js index 863ab0f765..cda1016b08 100644 --- a/jscomp/build_tests/bs_dependencies_node_path_override/input.js +++ b/jscomp/build_tests/bs_dependencies_node_path_override/input.js @@ -2,4 +2,4 @@ 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',stdio:"inherit"}) \ No newline at end of file +p.execSync(`NODE_PATH=${node_path} node ./testcase.js`, { cwd: __dirname, shell: true, encoding: 'utf8' }) \ No newline at end of file diff --git a/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js b/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js index 5ba4a08436..798d8f2165 100644 --- a/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js +++ b/jscomp/build_tests/bs_dependencies_node_path_override/testcase.js @@ -2,6 +2,6 @@ var assert = require('assert') var path = require('path') var p = require('child_process') -p.execSync(`bsb -version; bsb -make-world`, { cwd: __dirname, shell: true, encoding: 'utf8', stdio: "inherit" }) +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') \ No newline at end of file