From 88d15808d3ef1f1201be33eb222e0a71ca70f8a4 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Fri, 21 Jun 2024 10:30:18 +0200 Subject: [PATCH 1/4] [macro] don't add modules as dependencies when using Compiler.include --- src/core/tPrinting.ml | 1 + src/core/tType.ml | 1 + src/macro/macroApi.ml | 5 +++++ src/typing/macroContext.ml | 9 +++++++++ src/typing/typeloadModule.ml | 2 +- std/haxe/macro/Compiler.hx | 2 +- 6 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/core/tPrinting.ml b/src/core/tPrinting.ml index 476509f9280..af95e80ef0a 100644 --- a/src/core/tPrinting.ml +++ b/src/core/tPrinting.ml @@ -617,6 +617,7 @@ module Printer = struct | MDepFromImport -> "MDepFromImport" | MDepFromTyping -> "MDepFromTyping" | MDepFromMacro -> "MDepFromMacro" + | MDepIgnore -> die "" __LOC__ let s_module_tainting_reason = function | CheckDisplayFile -> "check_display_file" diff --git a/src/core/tType.ml b/src/core/tType.ml index ab710bfa3d6..7995420b442 100644 --- a/src/core/tType.ml +++ b/src/core/tType.ml @@ -406,6 +406,7 @@ and module_dep_origin = | MDepFromTyping | MDepFromImport | MDepFromMacro + | MDepIgnore and module_dep = { md_sign : Digest.t; diff --git a/src/macro/macroApi.ml b/src/macro/macroApi.ml index f2ee01fcf68..a9200e1f9c1 100644 --- a/src/macro/macroApi.ml +++ b/src/macro/macroApi.ml @@ -26,6 +26,7 @@ type 'value compiler_api = { init_macros_done : unit -> bool; get_type : string -> Type.t option; get_module : string -> Type.t list; + include_module : string -> unit; after_init_macros : (unit -> unit) -> unit; after_typing : (module_type list -> unit) -> unit; on_generate : (Type.t list -> unit) -> bool -> unit; @@ -1878,6 +1879,10 @@ let macro_api ccom get_api = "get_module", vfun1 (fun s -> encode_array (List.map encode_type ((get_api()).get_module (decode_string s))) ); + "include_module", vfun1 (fun s -> + (get_api()).include_module (decode_string s); + vnull + ); "on_after_init_macros", vfun1 (fun f -> let f = prepare_callback f 1 in (get_api()).after_init_macros (fun tctx -> ignore(f [])); diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 290e9e1bf15..99135aee63e 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -127,6 +127,9 @@ let make_macro_com_api com mcom p = get_module = (fun s -> Interp.exc_string "unsupported" ); + include_module = (fun s -> + Interp.exc_string "unsupported" + ); after_init_macros = (fun f -> com.callbacks#add_after_init_macros (fun () -> let t = macro_timer com ["afterInitMacros"] in @@ -404,6 +407,12 @@ let make_macro_api ctx mctx p = m ) ); + MacroApi.include_module = (fun s -> + typing_timer ctx false (fun ctx -> + let path = parse_path s in + ignore(TypeloadModule.load_module ~origin:MDepIgnore ctx path p) + ) + ); MacroApi.type_expr = (fun e -> typing_timer ctx true (fun ctx -> type_expr ctx e WithType.value) ); diff --git a/src/typing/typeloadModule.ml b/src/typing/typeloadModule.ml index 13a5a991d73..58a5de9a270 100644 --- a/src/typing/typeloadModule.ml +++ b/src/typing/typeloadModule.ml @@ -845,7 +845,7 @@ and load_module' com g m p = let load_module ?(origin:module_dep_origin = MDepFromTyping) ctx m p = let m2 = load_module' ctx.com ctx.g m p in - add_dependency ~skip_postprocess:true ctx.m.curmod m2 origin; + if origin <> MDepIgnore then add_dependency ~skip_postprocess:true ctx.m.curmod m2 origin; if ctx.pass = PTypeField then flush_pass ctx.g PConnectField ("load_module",fst m @ [snd m]); m2 diff --git a/std/haxe/macro/Compiler.hx b/std/haxe/macro/Compiler.hx index cc3e5d14aec..e9b77e00ed1 100644 --- a/std/haxe/macro/Compiler.hx +++ b/std/haxe/macro/Compiler.hx @@ -224,7 +224,7 @@ class Compiler { var cl = prefix + file.substr(0, file.length - 3); if (skip(cl)) continue; - Context.getModule(cl); + load("include_module", 1)(cl); } else if (rec && sys.FileSystem.isDirectory(path + "/" + file) && !skip(prefix + file)) include(prefix + file, true, ignore, classPaths); } From cbee1a4b8a74e8c6343a63f69551db4e775cc1b8 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Fri, 21 Jun 2024 16:33:21 +0200 Subject: [PATCH 2/4] [tests] add test for 11701 --- tests/server/src/cases/issues/Issue11701.hx | 25 +++++++++++++++++++ .../test/templates/issues/Issue11701/Bar.hx | 5 ++++ .../test/templates/issues/Issue11701/Baz.hx | 5 ++++ .../test/templates/issues/Issue11701/Foo.hx | 8 ++++++ .../test/templates/issues/Issue11701/Main.hx | 10 ++++++++ .../templates/issues/Issue11701/compile.hxml | 3 +++ 6 files changed, 56 insertions(+) create mode 100644 tests/server/src/cases/issues/Issue11701.hx create mode 100644 tests/server/test/templates/issues/Issue11701/Bar.hx create mode 100644 tests/server/test/templates/issues/Issue11701/Baz.hx create mode 100644 tests/server/test/templates/issues/Issue11701/Foo.hx create mode 100644 tests/server/test/templates/issues/Issue11701/Main.hx create mode 100644 tests/server/test/templates/issues/Issue11701/compile.hxml diff --git a/tests/server/src/cases/issues/Issue11701.hx b/tests/server/src/cases/issues/Issue11701.hx new file mode 100644 index 00000000000..242e43f6dbf --- /dev/null +++ b/tests/server/src/cases/issues/Issue11701.hx @@ -0,0 +1,25 @@ +package cases.issues; + +import haxe.display.Diagnostic; + +class Issue11701 extends TestCase { + function test(_) { + vfs.putContent("compile.hxml", getTemplate("issues/Issue11701/compile.hxml")); + vfs.putContent("Main.hx", getTemplate("issues/Issue11701/Main.hx")); + vfs.putContent("bar/Bar.hx", getTemplate("issues/Issue11701/Bar.hx")); + vfs.putContent("baz/Baz.hx", getTemplate("issues/Issue11701/Baz.hx")); + + var content = getTemplate("issues/Issue11701/Foo.hx"); + var transform = Markers.parse(content); + vfs.putContent("foo/Foo.hx", transform.source); + + var args = ["compile.hxml"]; + + runHaxe(args); + assertSuccess(); + + runHaxeJson(args, ServerMethods.Invalidate, {file: new FsPath("foo/Foo.hx")}); + runHaxeJsonCb(args, DisplayMethods.Hover, {file: new FsPath("foo/Foo.hx"), offset: transform.offset(1)}, _ -> {}); + assertSuccess(); + } +} diff --git a/tests/server/test/templates/issues/Issue11701/Bar.hx b/tests/server/test/templates/issues/Issue11701/Bar.hx new file mode 100644 index 00000000000..34a920bd171 --- /dev/null +++ b/tests/server/test/templates/issues/Issue11701/Bar.hx @@ -0,0 +1,5 @@ +package bar; + +class Bar { + var baz:baz.Baz; +} diff --git a/tests/server/test/templates/issues/Issue11701/Baz.hx b/tests/server/test/templates/issues/Issue11701/Baz.hx new file mode 100644 index 00000000000..b0a2750231c --- /dev/null +++ b/tests/server/test/templates/issues/Issue11701/Baz.hx @@ -0,0 +1,5 @@ +package baz; + +class Baz { + var main:Main; +} diff --git a/tests/server/test/templates/issues/Issue11701/Foo.hx b/tests/server/test/templates/issues/Issue11701/Foo.hx new file mode 100644 index 00000000000..ea94d8defec --- /dev/null +++ b/tests/server/test/templates/issues/Issue11701/Foo.hx @@ -0,0 +1,8 @@ +package foo; + +class Foo { + function foo() { + baz.Baz; + tr{-1-}ace(""); + } +} diff --git a/tests/server/test/templates/issues/Issue11701/Main.hx b/tests/server/test/templates/issues/Issue11701/Main.hx new file mode 100644 index 00000000000..8d7bc01e59d --- /dev/null +++ b/tests/server/test/templates/issues/Issue11701/Main.hx @@ -0,0 +1,10 @@ +class Main { + var foo:foo.Foo; + + public static function main() include(); + + static macro function include() { + haxe.macro.Compiler.include("bar"); + return macro null; + } +} diff --git a/tests/server/test/templates/issues/Issue11701/compile.hxml b/tests/server/test/templates/issues/Issue11701/compile.hxml new file mode 100644 index 00000000000..33cd2de8dad --- /dev/null +++ b/tests/server/test/templates/issues/Issue11701/compile.hxml @@ -0,0 +1,3 @@ +-js out.js +--no-output +-main Main From d4644319b61acfbdedeceae557e946bb523c4bde Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Fri, 21 Jun 2024 16:37:41 +0200 Subject: [PATCH 3/4] [macro] use MDepFromMacroInclude as origin for Compiler.include() --- src/core/tFunctions.ml | 16 ++++++++++------ src/core/tPrinting.ml | 2 +- src/core/tType.ml | 2 +- src/typing/macroContext.ml | 2 +- src/typing/typeloadModule.ml | 2 +- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/src/core/tFunctions.ml b/src/core/tFunctions.ml index 1c9e4fc98d5..a1834532288 100644 --- a/src/core/tFunctions.ml +++ b/src/core/tFunctions.ml @@ -303,12 +303,16 @@ let null_abstract = { a_enum = false; } -let add_dependency ?(skip_postprocess=false) m mdep origin = - if m != null_module && mdep != null_module && (m.m_path != mdep.m_path || m.m_extra.m_sign != mdep.m_extra.m_sign) then begin - m.m_extra.m_deps <- PMap.add mdep.m_id ({md_sign = mdep.m_extra.m_sign; md_path = mdep.m_path; md_kind = mdep.m_extra.m_kind; md_origin = origin}) m.m_extra.m_deps; - (* In case the module is cached, we'll have to run post-processing on it again (issue #10635) *) - if not skip_postprocess then m.m_extra.m_processed <- 0 - end +let add_dependency ?(skip_postprocess=false) m mdep = function + (* These module dependency origins should not add as a dependency *) + | MDepFromMacroInclude -> () + + | origin -> + if m != null_module && mdep != null_module && (m.m_path != mdep.m_path || m.m_extra.m_sign != mdep.m_extra.m_sign) then begin + m.m_extra.m_deps <- PMap.add mdep.m_id ({md_sign = mdep.m_extra.m_sign; md_path = mdep.m_path; md_kind = mdep.m_extra.m_kind; md_origin = origin}) m.m_extra.m_deps; + (* In case the module is cached, we'll have to run post-processing on it again (issue #10635) *) + if not skip_postprocess then m.m_extra.m_processed <- 0 + end let arg_name (a,_) = a.v_name diff --git a/src/core/tPrinting.ml b/src/core/tPrinting.ml index af95e80ef0a..267ae5fae20 100644 --- a/src/core/tPrinting.ml +++ b/src/core/tPrinting.ml @@ -617,7 +617,7 @@ module Printer = struct | MDepFromImport -> "MDepFromImport" | MDepFromTyping -> "MDepFromTyping" | MDepFromMacro -> "MDepFromMacro" - | MDepIgnore -> die "" __LOC__ + | MDepFromMacroInclude -> "MDepFromMacroInclude" let s_module_tainting_reason = function | CheckDisplayFile -> "check_display_file" diff --git a/src/core/tType.ml b/src/core/tType.ml index 7995420b442..162685b9e60 100644 --- a/src/core/tType.ml +++ b/src/core/tType.ml @@ -406,7 +406,7 @@ and module_dep_origin = | MDepFromTyping | MDepFromImport | MDepFromMacro - | MDepIgnore + | MDepFromMacroInclude and module_dep = { md_sign : Digest.t; diff --git a/src/typing/macroContext.ml b/src/typing/macroContext.ml index 99135aee63e..aee915b886a 100644 --- a/src/typing/macroContext.ml +++ b/src/typing/macroContext.ml @@ -410,7 +410,7 @@ let make_macro_api ctx mctx p = MacroApi.include_module = (fun s -> typing_timer ctx false (fun ctx -> let path = parse_path s in - ignore(TypeloadModule.load_module ~origin:MDepIgnore ctx path p) + ignore(TypeloadModule.load_module ~origin:MDepFromMacroInclude ctx path p) ) ); MacroApi.type_expr = (fun e -> diff --git a/src/typing/typeloadModule.ml b/src/typing/typeloadModule.ml index 58a5de9a270..13a5a991d73 100644 --- a/src/typing/typeloadModule.ml +++ b/src/typing/typeloadModule.ml @@ -845,7 +845,7 @@ and load_module' com g m p = let load_module ?(origin:module_dep_origin = MDepFromTyping) ctx m p = let m2 = load_module' ctx.com ctx.g m p in - if origin <> MDepIgnore then add_dependency ~skip_postprocess:true ctx.m.curmod m2 origin; + add_dependency ~skip_postprocess:true ctx.m.curmod m2 origin; if ctx.pass = PTypeField then flush_pass ctx.g PConnectField ("load_module",fst m @ [snd m]); m2 From 2bb7f6441adabff8518df25bf99299a8d4b21c43 Mon Sep 17 00:00:00 2001 From: Rudy Ges Date: Fri, 21 Jun 2024 18:18:52 +0200 Subject: [PATCH 4/4] [tests] hxml file not needed anymore --- tests/server/src/cases/issues/Issue11701.hx | 4 +--- tests/server/test/templates/issues/Issue11701/compile.hxml | 3 --- 2 files changed, 1 insertion(+), 6 deletions(-) delete mode 100644 tests/server/test/templates/issues/Issue11701/compile.hxml diff --git a/tests/server/src/cases/issues/Issue11701.hx b/tests/server/src/cases/issues/Issue11701.hx index 242e43f6dbf..5dd60f6e827 100644 --- a/tests/server/src/cases/issues/Issue11701.hx +++ b/tests/server/src/cases/issues/Issue11701.hx @@ -4,7 +4,6 @@ import haxe.display.Diagnostic; class Issue11701 extends TestCase { function test(_) { - vfs.putContent("compile.hxml", getTemplate("issues/Issue11701/compile.hxml")); vfs.putContent("Main.hx", getTemplate("issues/Issue11701/Main.hx")); vfs.putContent("bar/Bar.hx", getTemplate("issues/Issue11701/Bar.hx")); vfs.putContent("baz/Baz.hx", getTemplate("issues/Issue11701/Baz.hx")); @@ -13,8 +12,7 @@ class Issue11701 extends TestCase { var transform = Markers.parse(content); vfs.putContent("foo/Foo.hx", transform.source); - var args = ["compile.hxml"]; - + var args = ["-main", "Main"]; runHaxe(args); assertSuccess(); diff --git a/tests/server/test/templates/issues/Issue11701/compile.hxml b/tests/server/test/templates/issues/Issue11701/compile.hxml deleted file mode 100644 index 33cd2de8dad..00000000000 --- a/tests/server/test/templates/issues/Issue11701/compile.hxml +++ /dev/null @@ -1,3 +0,0 @@ --js out.js ---no-output --main Main