algorand / pyteal

Algorand Smart Contracts in Python
https://pyteal.readthedocs.io
MIT License
285 stars 131 forks source link

Source Map Bugfix: Just getcwd() instead of relpath hackery #691

Closed tzaffi closed 1 year ago

tzaffi commented 1 year ago

Problem

@barnjamin pointed out a bug when running the source mapper in Beaker:

❯ pwd
/Users/zeph/github/algorand/beaker
❯ cd examples/simple
❯ python hello.py
Traceback (most recent call last):
  File "/Users/zeph/github/algorand/beaker/examples/simple/hello.py", line 48, in <module>
    demo()
  File "/Users/zeph/github/algorand/beaker/examples/simple/hello.py", line 23, in demo
    app_client = client.ApplicationClient(
  File "/Users/zeph/github/algorand/beaker/beaker/client/application_client.py", line 55, in __init__
    self.app = app.build(client)
  File "/Users/zeph/github/algorand/beaker/beaker/application.py", line 1067, in build
    compile_results = router.compile(
  File "/Users/zeph/github/algorand/beaker/.venv/lib/python3.10/site-packages/pyteal/ast/router.py", line 1213, in compile
    return self._build_impl(input).get_results()
  File "/Users/zeph/github/algorand/beaker/.venv/lib/python3.10/site-packages/pyteal/ast/router.py", line 1221, in _build_impl
    abundle = input.get_compilation(ap)._compile_impl(
  File "/Users/zeph/github/algorand/beaker/.venv/lib/python3.10/site-packages/pyteal/compiler/compiler.py", line 492, in _compile_impl
    source_mapper = _PyTealSourceMapper(
  File "/Users/zeph/github/algorand/beaker/.venv/lib/python3.10/site-packages/pyteal/compiler/sourcemap.py", line 717, in __init__
    self.build()
  File "/Users/zeph/github/algorand/beaker/.venv/lib/python3.10/site-packages/pyteal/compiler/sourcemap.py", line 837, in build
    self._build_r3sourcemap()
  File "/Users/zeph/github/algorand/beaker/.venv/lib/python3.10/site-packages/pyteal/compiler/sourcemap.py", line 884, in _build_r3sourcemap
    assert all(
AssertionError: inconsistent sourceRoot - aborting

This PR aims to fix the issue by defining the source map's source root to be the current working directory. This really should have been the logic to begin with, but instead, the root was calculated using a path comparison algorithm that broke on relative paths.

Testing

Unit tests were added and I confirmed locally that when depending on this branch everything was copacetic:

❯ python hello.py
Deployed app in txid MYBYTUF3TL64R5L7EVKUIY6LV6FH4HMAF2HIEU7IPLVV7XNJGUTQ
        App ID: 5 
        Address: 2SYXFSCZAQCZ7YIFUCUZYOVR7G6Y3UBGSJIWT4EZ4CO3T6WVYTMHVSANOY 

Hello, Beaker