Skip to content

Commit

Permalink
[cli] try to get --each + --cwd under control
Browse files Browse the repository at this point in the history
closes #11354
  • Loading branch information
Simn committed Nov 8, 2023
1 parent 2597298 commit 7511a01
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/compiler/compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -541,14 +541,13 @@ module HighLevel = struct
lines

(* Returns a list of contexts, but doesn't do anything yet *)
let process_params server_api create each_params has_display is_server pl =
let curdir = Unix.getcwd () in
let process_params server_api create each_args has_display is_server args =
(* We want the loop below to actually see all the --each params, so let's prepend them *)
let args = !each_args @ args in
let added_libs = Hashtbl.create 0 in
let server_mode = ref SMNone in
let create_context args =
let ctx = create (server_api.on_context_create()) args in
(* --cwd triggers immediately, so let's reset *)
Unix.chdir curdir;
ctx
in
let rec find_subsequent_libs acc args = match args with
Expand All @@ -559,16 +558,16 @@ module HighLevel = struct
in
let rec loop acc = function
| [] ->
[],Some (create_context (!each_params @ (List.rev acc)))
[],Some (create_context (List.rev acc))
| "--next" :: l when acc = [] -> (* skip empty --next *)
loop [] l
| "--next" :: l ->
let ctx = create_context (!each_params @ (List.rev acc)) in
let ctx = create_context (List.rev acc) in
ctx.has_next <- true;
l,Some ctx
| "--each" :: l ->
each_params := List.rev acc;
loop [] l
each_args := List.rev acc;
loop acc l
| "--cwd" :: dir :: l | "-C" :: dir :: l ->
(* we need to change it immediately since it will affect hxml loading *)
(try Unix.chdir dir with _ -> raise (Arg.Bad ("Invalid directory: " ^ dir)));
Expand All @@ -591,14 +590,14 @@ module HighLevel = struct
loop acc l
| "--run" :: cl :: args ->
let acc = cl :: "-x" :: acc in
let ctx = create_context (!each_params @ (List.rev acc)) in
let ctx = create_context (List.rev acc) in
ctx.com.sys_args <- args;
[],Some ctx
| ("-L" | "--library" | "-lib") :: name :: args ->
let libs,args = find_subsequent_libs [name] args in
let libs = List.filter (fun l -> not (Hashtbl.mem added_libs l)) libs in
List.iter (fun l -> Hashtbl.add added_libs l ()) libs;
let lines = add_libs libs pl server_api.cache has_display in
let lines = add_libs libs args server_api.cache has_display in
loop acc (lines @ args)
| ("--jvm" | "--java" | "-java" as arg) :: dir :: args ->
loop_lib arg dir "hxjava" acc args
Expand All @@ -614,7 +613,7 @@ module HighLevel = struct
and loop_lib arg dir lib acc args =
loop (dir :: arg :: acc) ("-lib" :: lib :: args)
in
let args,ctx = loop [] pl in
let args,ctx = loop [] args in
args,!server_mode,ctx

let execute_ctx server_api ctx server_mode =
Expand Down Expand Up @@ -642,6 +641,7 @@ module HighLevel = struct
let entry server_api comm args =
let create = create_context comm server_api.cache in
let each_args = ref [] in
let curdir = Unix.getcwd () in
let has_display = ref false in
(* put --display in front if it was last parameter *)
let args = match List.rev args with
Expand All @@ -661,14 +661,18 @@ module HighLevel = struct
in
let code = match ctx with
| Some ctx ->
(* Need chdir here because --cwd is eagerly applied in process_params *)
Unix.chdir curdir;
execute_ctx server_api ctx server_mode
| None ->
(* caused by --connect *)
0
in
if code = 0 && args <> [] && not !has_display then
if code = 0 && args <> [] && not !has_display then begin
(* We have to chdir here again because any --cwd also takes effect in execute_ctx *)
Unix.chdir curdir;
loop args
else
end else
code
in
let code = loop args in
Expand Down
14 changes: 14 additions & 0 deletions tests/misc/projects/Issue11354/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function main() {
var a = null;
if (a == null) a = 10;
$type(a); // Null<Null<Int>>

var b = null;
$type(b); // Null<Unknown<0>>
b = null;
$type(b); // Null<Null<Unknown<0>>>
b = null;
$type(b); // Null<Null<Null<Unknown<0>>>>
b = 10;
$type(b); // Null<Null<Null<Int>>>
}
15 changes: 15 additions & 0 deletions tests/misc/projects/Issue11354/compile.hxml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
--cwd proj
--main Main

--each

--cwd a
--interp

--next

--cwd b
--interp

--next
--interp
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue11354/compile.hxml.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Main.hx:2: Hello from folder a
Main.hx:2: Hello from folder b
Main.hx:2: Hello from folder proj
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue11354/proj/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function main() {
trace("Hello from folder proj");
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue11354/proj/a/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function main() {
trace("Hello from folder a");
}
3 changes: 3 additions & 0 deletions tests/misc/projects/Issue11354/proj/b/Main.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function main() {
trace("Hello from folder b");
}

0 comments on commit 7511a01

Please sign in to comment.