Skip to content

Commit 09b87fd

Browse files
authored
Fix path mapping from #99 (#100)
1 parent ea45d4c commit 09b87fd

5 files changed

Lines changed: 22 additions & 14 deletions

File tree

‎mojo/mojo_import.bzl‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ def _mojo_import_impl(ctx):
99
return [
1010
DefaultInfo(files = depset(mojo_deps, transitive = [transitive_mojodeps])),
1111
MojoInfo(
12-
import_paths = depset([pkg.dirname for pkg in mojo_deps], transitive = [import_paths]),
13-
mojodeps = depset([pkg for pkg in mojo_deps], transitive = [transitive_mojodeps]),
12+
import_paths = depset(
13+
[struct(package = pkg, import_path = ".") for pkg in mojo_deps],
14+
transitive = [import_paths],
15+
),
16+
mojodeps = depset(mojo_deps, transitive = [transitive_mojodeps]),
1417
),
1518
]
1619

‎mojo/mojo_library.bzl‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ other Mojo targets."""
33

44
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
55
load("//mojo:providers.bzl", "MojoInfo")
6-
load("//mojo/private:utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "is_exec_config")
6+
load("//mojo/private:utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "format_import", "is_exec_config")
77

88
def _format_include(arg):
99
return ["-I", arg.dirname]
@@ -42,7 +42,7 @@ def _mojo_library_implementation(ctx):
4242
output_group_kwargs["mojo_fixits"] = depset([fixits_file])
4343
args.add("--experimental-export-fixit", fixits_file)
4444

45-
file_args.add_all(transitive_mojodeps, map_each = _format_include)
45+
file_args.add_all(import_paths, map_each = format_import)
4646
file_args.add(root_directory)
4747
ctx.actions.run(
4848
executable = mojo_toolchain.mojo,
@@ -68,15 +68,17 @@ def _mojo_library_implementation(ctx):
6868
for target in ctx.attr.data:
6969
transitive_runfiles.append(target[DefaultInfo].default_runfiles)
7070

71-
import_path = mojo_precmp_file.dirname + "/" + ctx.attr.import_path
72-
7371
return [
7472
DefaultInfo(
7573
files = depset([mojo_precmp_file]),
7674
runfiles = ctx.runfiles(ctx.files.data).merge_all(transitive_runfiles),
7775
),
7876
MojoInfo(
79-
import_paths = depset([import_path], transitive = [import_paths]),
77+
# Passed through as a File + string combo so that it can be path mapped later.
78+
import_paths = depset(
79+
[struct(package = mojo_precmp_file, import_path = ctx.attr.import_path)],
80+
transitive = [import_paths],
81+
),
8082
mojodeps = depset([mojo_precmp_file], transitive = [transitive_mojodeps]),
8183
),
8284
OutputGroupInfo(**output_group_kwargs),

‎mojo/private/mojo_binary_test.bzl‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ load("@rules_cc//cc/common:cc_info.bzl", "CcInfo")
99
load("@rules_python//python:py_info.bzl", "PyInfo")
1010
load("//mojo:providers.bzl", "MojoInfo")
1111
load(":transitions.bzl", "python_version_transition")
12-
load(":utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "is_exec_config")
12+
load(":utils.bzl", "MOJO_EXTENSIONS", "collect_mojoinfo", "format_import", "is_exec_config")
1313

1414
_PYTHON_TOOLCHAIN_TYPE = "@rules_python//python:toolchain_type"
1515
_ATTRS = {
@@ -93,9 +93,6 @@ def _find_main(name, srcs, main):
9393
def _format_include(arg):
9494
return ["-I", arg.dirname]
9595

96-
def _add_include(arg):
97-
return ["-I", arg]
98-
9996
def _mojo_binary_test_implementation(ctx, *, shared_library = False):
10097
cc_toolchain = find_cpp_toolchain(ctx)
10198
mojo_toolchain = ctx.exec_groups["mojo_compile"].toolchains["//:toolchain_type"].mojo_toolchain_info
@@ -121,7 +118,7 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False):
121118

122119
all_deps = ctx.attr.deps + mojo_toolchain.implicit_deps + ([ctx.attr._link_extra_lib] if ctx.attr._link_extra_lib else [])
123120
transitive_includes, transitive_mojodeps = collect_mojoinfo(all_deps)
124-
args.add_all(transitive_includes, map_each = _add_include)
121+
args.add_all(transitive_includes, map_each = format_import)
125122

126123
# NOTE: Argument order:
127124
# 1. Basic functional arguments

‎mojo/private/utils.bzl‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Helpers internal to rules_mojo."""
22

33
load("@bazel_features//:features.bzl", "bazel_features")
4+
load("@bazel_skylib//lib:paths.bzl", "paths")
45
load("//mojo:providers.bzl", "MojoInfo")
56

67
MOJO_EXTENSIONS = ("mojo",)
@@ -12,7 +13,9 @@ def collect_mojoinfo(deps):
1213
deps: A list of dependencies to collect MojoInfo from.
1314
1415
Returns:
15-
A single MojoInfo object with the combined data.
16+
A tuple (imports, mojodeps) of two depsets combining every MojoInfo in
17+
deps: `imports` holds struct(package, import_path) entries for building
18+
-I flags, `mojodeps` holds the precompiled mojo Files for action inputs.
1619
"""
1720
import_paths = []
1821
mojodeps = []
@@ -24,6 +27,9 @@ def collect_mojoinfo(deps):
2427

2528
return depset(transitive = import_paths), depset(transitive = mojodeps)
2629

30+
def format_import(dep):
31+
return ["-I", paths.normalize(paths.join(dep.package.dirname, dep.import_path))]
32+
2733
def is_exec_config(ctx):
2834
"""Determines whether the current configuration is an exec configuration.
2935

‎mojo/providers.bzl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
MojoInfo = provider(
44
doc = "Information about how to build a Mojo target.",
55
fields = {
6-
"import_paths": "Directories that should be passed with -I to mojo",
6+
"import_paths": "Depset of struct(package = File, import_path = str). Each element is a mojoc file paired with its relative import path.",
77
"mojodeps": "The precompiled mojo files required by the target",
88
},
99
)

0 commit comments

Comments
 (0)