Skip to content

Commit ea45d4c

Browse files
authored
Add ability to import through directories (#99)
modular/modular@abc342f adds this functionality, plumbing it through here. Now we can set `import_path`, similar to rules_python's `imports`, but restricting it to only one possible way to import a package. (There's no hard reason for this restriction, but for now it generally makes sense that there should only be one.) Also bumps the default Mojo version (to pull this change) and fixes a warning.
1 parent a33aaa6 commit ea45d4c

8 files changed

Lines changed: 45 additions & 12 deletions

File tree

‎mojo/extensions.bzl‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ load("//mojo:mojo_host_platform.bzl", "mojo_host_platform")
44
load("//mojo/private:mojo_gpu_toolchains_repository.bzl", "mojo_gpu_toolchains_repository")
55

66
_PLATFORMS = ["linux_aarch64", "linux_x86_64", "macos_arm64"]
7-
_DEFAULT_VERSION = "1.0.0b3.dev2026061206"
7+
_DEFAULT_VERSION = "1.0.0b3.dev2026072006"
88
_KNOWN_SHAS = {
9-
"1.0.0b3.dev2026061206": {
10-
"linux_aarch64": "061042c815d945c8e403a13c5317d6dbd9ceef5a0ef84422ae6b6da0a2f0fe59",
11-
"linux_x86_64": "b7f569472b02a35f40c8ea96ea51dac73b6c6c085e23c08ecf3613276df4f7dd",
12-
"macos_arm64": "bc4106c6013994b4eb246702832c7bc6967c4f6eeaa911eccf75d2dff26d7a3a",
13-
"mojo_compiler_mojo_libs": "1c99770a8a14804f810e6a2a12f1fd5b8d5c783301b38b74c785bc421ac334b9",
9+
"1.0.0b3.dev2026072006": {
10+
"linux_aarch64": "63ad1c8fe113abc6a617ecadc76d42a5cbcabe19a6ef01c90b5cbc4281b118e2",
11+
"linux_x86_64": "72c810694ebcc514a363857e3d8f07828be994cf26a9317b50839a3f09c45fa3",
12+
"macos_arm64": "f58003493298e4427bf217c26274649f0973b3fafe379c9d319e59231066fff2",
13+
"mojo_compiler_mojo_libs": "27f7acea73231e93f4e9b4485d33059343d357c552b9828f2a4e2575018742a5",
1414
},
1515
}
1616
_PLATFORM_MAPPINGS = {

‎mojo/mojo_library.bzl‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,15 @@ 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+
7173
return [
7274
DefaultInfo(
7375
files = depset([mojo_precmp_file]),
7476
runfiles = ctx.runfiles(ctx.files.data).merge_all(transitive_runfiles),
7577
),
7678
MojoInfo(
77-
import_paths = depset([mojo_precmp_file.dirname], transitive = [import_paths]),
79+
import_paths = depset([import_path], transitive = [import_paths]),
7880
mojodeps = depset([mojo_precmp_file], transitive = [transitive_mojodeps]),
7981
),
8082
OutputGroupInfo(**output_group_kwargs),
@@ -111,6 +113,13 @@ precompile' since it does not accept many flags.
111113
"deps": attr.label_list(
112114
providers = [MojoInfo],
113115
),
116+
"import_path": attr.string(
117+
doc = """
118+
Path relative to this package where it should be imported from.
119+
Generally something like `.`, `..`, or `../..`.
120+
""",
121+
default = ".",
122+
),
114123
"data": attr.label_list(),
115124
"_mojo_precompile_copts": attr.label(
116125
default = Label("//:mojo_precompile_copt"),

‎mojo/private/mojo_binary_test.bzl‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ def _find_main(name, srcs, main):
9393
def _format_include(arg):
9494
return ["-I", arg.dirname]
9595

96-
def _format_path(arg):
97-
return [arg.path]
96+
def _add_include(arg):
97+
return ["-I", arg]
9898

9999
def _mojo_binary_test_implementation(ctx, *, shared_library = False):
100100
cc_toolchain = find_cpp_toolchain(ctx)
@@ -120,8 +120,8 @@ def _mojo_binary_test_implementation(ctx, *, shared_library = False):
120120
args.add_all([file], map_each = _format_include)
121121

122122
all_deps = ctx.attr.deps + mojo_toolchain.implicit_deps + ([ctx.attr._link_extra_lib] if ctx.attr._link_extra_lib else [])
123-
_, transitive_mojodeps = collect_mojoinfo(all_deps)
124-
args.add_all(transitive_mojodeps, map_each = _format_include)
123+
transitive_includes, transitive_mojodeps = collect_mojoinfo(all_deps)
124+
args.add_all(transitive_includes, map_each = _add_include)
125125

126126
# NOTE: Argument order:
127127
# 1. Basic functional arguments

‎tests/nested/BUILD.bazel‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("//mojo:mojo_test.bzl", "mojo_test")
2+
3+
mojo_test(
4+
name = "main",
5+
srcs = ["main.mojo"],
6+
visibility = ["//tests:__subpackages__"],
7+
deps = ["//tests/nested/outer:inner"],
8+
)

‎tests/nested/main.mojo‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from outer.inner import get_five
2+
3+
def main():
4+
print(get_five())

‎tests/nested/outer/BUILD.bazel‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
load("//mojo:mojo_library.bzl", "mojo_library")
2+
3+
mojo_library(
4+
name = "inner",
5+
srcs = [
6+
"__init__.mojo",
7+
],
8+
import_path = "..",
9+
visibility = ["//tests:__subpackages__"],
10+
)

‎tests/nested/outer/__init__.mojo‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def get_five() -> Int:
2+
return 5

‎tests/python/python_shared_library.mojo‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ from std.python._cpython import PyObjectPtr
66

77

88
@export
9-
def PyInit_python_shared_library() -> PythonObject:
9+
def PyInit_python_shared_library() abi("C") -> PythonObject:
1010
"""Create a Python module with a function binding for `mojo_count_args`."""
1111

1212
try:

0 commit comments

Comments
 (0)