From c9435f3a54aa4da0bd729b8a90ecf643794dabfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Facundo=20Dom=C3=ADnguez?= Date: Mon, 1 Nov 2021 15:28:21 -0300 Subject: [PATCH] Fix haskell_module tests --- haskell/private/actions/compile.bzl | 9 ------- haskell/private/haskell_impl.bzl | 25 +++++++++++++------ haskell/private/path_utils.bzl | 6 ++--- tests/haskell_module/binary/BUILD.bazel | 4 ++- .../binary/{TestBin.hs => Main.hs} | 0 tests/haskell_module/hs-boot/BUILD.bazel | 2 ++ tests/haskell_module/library/BUILD.bazel | 2 ++ tests/haskell_module/plugin/BUILD.bazel | 2 ++ 8 files changed, 29 insertions(+), 21 deletions(-) rename tests/haskell_module/binary/{TestBin.hs => Main.hs} (100%) diff --git a/haskell/private/actions/compile.bzl b/haskell/private/actions/compile.bzl index 5d0a28cc1e..ca0f1d343f 100644 --- a/haskell/private/actions/compile.bzl +++ b/haskell/private/actions/compile.bzl @@ -459,15 +459,6 @@ def compile_binary( env = c.env, arguments = c.args, ) - else: - # The list of sources might be empty if the binary is only - # built from modules produced with haskell_module - hs.actions.run_shell( - outputs = c.outputs, - command = """ - mkdir -p {objects_dir} - """.format(objects_dir = c.objects_dir.path), - ) return struct( object_files = c.object_files, diff --git a/haskell/private/haskell_impl.bzl b/haskell/private/haskell_impl.bzl index 89f5879a4b..177f4cf7c8 100644 --- a/haskell/private/haskell_impl.bzl +++ b/haskell/private/haskell_impl.bzl @@ -36,6 +36,7 @@ load( "get_dynamic_hs_lib_name", "get_lib_extension", "get_static_hs_lib_name", + "infer_main_module", "ln", "match_label", "parse_pattern", @@ -144,6 +145,20 @@ def _expand_make_variables(name, ctx, strings): ] return expand_make_variables(name, ctx, strings, extra_label_attrs) +def haskell_module_from_info(info): + """ Produces the module name from a HaskellModuleInfo """ + return paths.relativize( + paths.replace_extension(info.interface_file.path, ""), + info.import_dir, + ).replace("/", ".") + +def is_main_as_haskell_module(modules, main_function): + main_module = infer_main_module(main_function).replace(".", "/") + for m in modules: + if haskell_module_from_info(m[HaskellModuleInfo]) == main_module: + return True + return False + def _haskell_binary_common_impl(ctx, is_test): hs = haskell_context(ctx) dep_info = gather_dep_info(ctx, ctx.attr.deps) @@ -174,7 +189,8 @@ def _haskell_binary_common_impl(ctx, is_test): with_profiling = is_profiling_enabled(hs) srcs_files, import_dir_map = _prepare_srcs(ctx.attr.srcs) - module_map = determine_module_names(srcs_files, True, ctx.attr.main_function, ctx.file.main_file) + main_as_haskell_module = is_main_as_haskell_module(modules, ctx.attr.main_function) + module_map = determine_module_names(srcs_files, not main_as_haskell_module, ctx.attr.main_function, ctx.file.main_file) inspect_coverage = _should_inspect_coverage(ctx, hs, is_test) dynamic = not ctx.attr.linkstatic @@ -343,13 +359,6 @@ def _haskell_binary_common_impl(ctx, is_test): )), ] -def haskell_module_from_info(info): - """ Produces the module name from a HaskellModuleInfo """ - return paths.relativize( - paths.replace_extension(info.interface_file.path, ""), - info.import_dir, - ).replace("\\", ".") - def haskell_library_impl(ctx): hs = haskell_context(ctx) deps = ctx.attr.deps + ctx.attr.exports diff --git a/haskell/private/path_utils.bzl b/haskell/private/path_utils.bzl index 617a385269..9d4baba94e 100644 --- a/haskell/private/path_utils.bzl +++ b/haskell/private/path_utils.bzl @@ -74,7 +74,7 @@ def _module_map_insert(module_map, module_name, module_file, is_boot = False): src = module_file, ) -def determine_module_names(src_files, is_binary = False, main_function = "", main_file = None): +def determine_module_names(src_files, search_binary = False, main_function = "", main_file = None): """Determine a mapping from module names to source files. The module name is inferred from the source file name. See @@ -94,7 +94,7 @@ def determine_module_names(src_files, is_binary = False, main_function = "", mai Args: src_files: sequence of File, source files. - is_binary: bool, whether target requires a main module. + search_binary: bool, whether we need to ensure there's a main module. main_function: string, optional, the `main_function` attribute to a Haskell binary rule. main_file: File, optional, the `main_file` attribute to a Haskell binary rule. @@ -117,7 +117,7 @@ def determine_module_names(src_files, is_binary = False, main_function = "", mai else: _module_map_insert(module_map, module_name, src, is_boot = src.short_path.endswith("-boot")) - if is_binary: + if search_binary: main_module = infer_main_module(main_function) if main_file: _module_map_insert(module_map, main_module, main_file) diff --git a/tests/haskell_module/binary/BUILD.bazel b/tests/haskell_module/binary/BUILD.bazel index ba5adb49f0..2af21b3d73 100644 --- a/tests/haskell_module/binary/BUILD.bazel +++ b/tests/haskell_module/binary/BUILD.bazel @@ -31,7 +31,9 @@ haskell_test( haskell_module( name = "TestBinModule", - src = "TestBin.hs", + # TODO: Test naming the Main module as something different + # after adding some attribute to make the module name explicit + src = "Main.hs", deps = ["//tests/hackage:base"], ) diff --git a/tests/haskell_module/binary/TestBin.hs b/tests/haskell_module/binary/Main.hs similarity index 100% rename from tests/haskell_module/binary/TestBin.hs rename to tests/haskell_module/binary/Main.hs diff --git a/tests/haskell_module/hs-boot/BUILD.bazel b/tests/haskell_module/hs-boot/BUILD.bazel index 83324f5be6..578a0a8880 100644 --- a/tests/haskell_module/hs-boot/BUILD.bazel +++ b/tests/haskell_module/hs-boot/BUILD.bazel @@ -38,6 +38,8 @@ haskell_module( haskell_library( name = "hs-boot-lib", + # TODO: remove when haskell_module supports dynamic builds + linkstatic = True, modules = [ ":lib-B", ":lib-A", diff --git a/tests/haskell_module/library/BUILD.bazel b/tests/haskell_module/library/BUILD.bazel index ae7fa06556..143bc1f731 100644 --- a/tests/haskell_module/library/BUILD.bazel +++ b/tests/haskell_module/library/BUILD.bazel @@ -9,6 +9,8 @@ package(default_testonly = 1) haskell_library( name = "TestLib", + # TODO: Remove when haskell_module supports dynamic builds + linkstatic = True, modules = [ ":TestLibModule", ], diff --git a/tests/haskell_module/plugin/BUILD.bazel b/tests/haskell_module/plugin/BUILD.bazel index e507c3bebe..37d7073821 100644 --- a/tests/haskell_module/plugin/BUILD.bazel +++ b/tests/haskell_module/plugin/BUILD.bazel @@ -57,6 +57,8 @@ haskell_module( haskell_library( name = "lib", + # TODO: remove when haskell_module supports dynamic builds + linkstatic = True, modules = [ ":module-with-plugin", ":module-without-plugin",