OpenZeppelin / nile

CLI tool to develop StarkNet projects written in Cairo
MIT License
321 stars 76 forks source link

First install calls for uninstalled module #10

Closed perama-v closed 2 years ago

perama-v commented 2 years ago

Environment: New python 3.8.6 environment

Issue: Running nile install 0.4.1 fails with error No module named: starkware

Description: Upon first installation, the package invokes the test module, which calls the starkware package, which is not yet installed.

Temporary resolution: In src/nile/main.py, removing the line from nile.commands.test import test_command allows the installation to proceed.

However, this also kills nile test. Replacing the line after installation allows for the nile test command to work.

I wasn't sure exactly how to approach this for a permanent fix.

martriay commented 2 years ago

what about moving the imports to the functions where they're used? wouldn't that lazy-load them? haven't tried it, but if I'm not mistaken that would only try to load the modules if the command is called

something like

async def _run_test_contract(path):
    from starkware.starknet.compiler.compile import compile_starknet_files
    from starkware.starknet.testing.state import StarknetState
    from starkware.starkware_utils.error_handling import StarkException

    print(f"Running tests for {path}")
    definition = compile_starknet_files([path], debug_info=True)
    starknet = await StarknetState.empty()
    contract_addr = await starknet.deploy(definition)

    for abi_entry in definition.abi:
        if abi_entry["type"] != "function":
            continue
        func_name = abi_entry["name"]
        if not func_name.startswith("test"):
            continue
        local_starknet = starknet.copy()
        try:
            await local_starknet.invoke_raw(
                contract_address=contract_addr,
                selector=func_name,
                calldata=[0 for _ in abi_entry["inputs"]],
            )
            print(f"[PASS] {func_name}")
        except StarkException as ex:
            print(f"[FAIL] {func_name}")
            print(ex.message)
martriay commented 2 years ago

Fixed in https://github.com/martriay/nile/releases/tag/v0.0.7