Skip to content

Commit

Permalink
fix: import from overlay fall back to main pack
Browse files Browse the repository at this point in the history
  • Loading branch information
vberlier committed Oct 15, 2023
1 parent 1dfd30c commit ebe2434
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
24 changes: 20 additions & 4 deletions bolt/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ def current_path(self) -> str:
"Currently executing module has no associated resource location."
)

def lookup(self, path: str) -> TextFileBase[Any]:
"""Lookup the associated source file in the compilation database.
If we're currently in an overlay, fall back to the source file in the main
data pack.
"""
if current := self.database.index.get(path):
return current

current_pack = self.database[self.database.current].pack
if current_pack is None or current_pack.overlay_parent is None:
raise KeyError(path)

return self.database.indices[current_pack.overlay_parent][path]

def match_ast(
self,
node: AstRoot,
Expand All @@ -183,7 +198,7 @@ def match_ast(
if not current:
current = self.database.current
elif isinstance(current, str):
current = self.database.index[current]
current = self.lookup(current)

compilation_unit = self.database[current]
compilation_unit.ast = node
Expand All @@ -192,7 +207,7 @@ def match_ast(

def __getitem__(self, current: Union[TextFileBase[Any], str]) -> CompiledModule:
if isinstance(current, str):
current = self.database.index[current]
current = self.lookup(current)

compilation_unit = self.database[current]
name = compilation_unit.resource_location or "<unknown>"
Expand Down Expand Up @@ -267,9 +282,10 @@ def __len__(self) -> int:
def get(self, current: Union[TextFileBase[Any], str]) -> Optional[CompiledModule]:
"""Return executable module if exists."""
if isinstance(current, str):
if current not in self.database.index:
try:
current = self.lookup(current)
except KeyError:
return None
current = self.database.index[current]

if current not in self.database:
return None
Expand Down
3 changes: 2 additions & 1 deletion examples/bolt_overlay/src/data/demo/functions/foo.mcfunction
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
say ("my" + "stuff")
from ./vars import stuff
say f"my {stuff}"
1 change: 1 addition & 0 deletions examples/bolt_overlay/src/data/demo/modules/vars.bolt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
stuff = 123
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
from ./vars import stuff
say (1 + 2)
say stuff
3 changes: 2 additions & 1 deletion tests/snapshots/examples__build_bolt_overlay__0.pack.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
`@function demo:foo`

```mcfunction
say mystuff
say my 123
```

## Overlay `dummy_overlay`
Expand All @@ -31,6 +31,7 @@ say mystuff

```mcfunction
say 3
say 123
```

`@endoverlay`

0 comments on commit ebe2434

Please sign in to comment.