Skip to content

Commit

Permalink
fix: ct collide from del ct
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Jan 5, 2024
1 parent ca6d8b8 commit 1c18e15
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
20 changes: 12 additions & 8 deletions src/ape/managers/compilers.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,18 @@ def compile(
cached_manifest = self.project_manager.local_project.cached_manifest

# Load past compiled contracts for verifying type-collision and other things.
already_compiled_contracts = (
(cached_manifest.contract_types or {}) if cached_manifest else {}
)
already_compiled_paths = [
contracts_folder / x.source_id
for x in already_compiled_contracts.values()
if x.source_id
]
already_compiled_contracts: Dict[str, ContractType] = {}
already_compiled_paths: List[Path] = []
for name, ct in ((cached_manifest.contract_types or {}) if cached_manifest else {}).items():
if not ct.source_id:
continue

_file = contracts_folder / ct.source_id
if not _file.is_file():
continue

already_compiled_contracts[name] = ct
already_compiled_paths.append(_file)

exclusions = self.config_manager.get_config("compile").exclude
for extension in extensions:
Expand Down
16 changes: 13 additions & 3 deletions tests/functional/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,9 +676,19 @@ def mock_compile(paths, base_path=None):
if path.suffix == mock.ext:
name = path.stem
code = HexBytes(123).hex()
contract_type = ContractType(
contractName=name, abi=[], deploymentBytecode=code, sourceId=path.name
)
data = {
"contractName": name,
"abi": [],
"deploymentBytecode": code,
"sourceId": path.name,
}

# Check for mocked overrides
overrides = mock.overrides
if isinstance(overrides, dict):
data = {**data, **overrides}

contract_type = ContractType.model_validate(data)
result.append(contract_type)

return result
Expand Down
35 changes: 35 additions & 0 deletions tests/functional/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,41 @@ def test_load_contracts(project_with_contract):
assert contracts == project_with_contract.contracts


def test_load_contracts_after_deleting_same_named_contract(config, compilers, mock_compiler):
"""
Tests against a scenario where you:
1. Add and compile a contract
2. Delete that contract
3. Add a new contract with same name somewhere else
Test such that we are able to compile successfully and not get a misleading
collision error from deleted files.
"""

with tempfile.TemporaryDirectory() as temp_dir:
path = Path(temp_dir)
contracts = path / "contracts"
contracts.mkdir()
init_contract = contracts / "foo.__mock__"
init_contract.write_text("LALA")
with config.using_project(path) as proj:
compilers.registered_compilers[".__mock__"] = mock_compiler
result = proj.load_contracts()
assert "foo" in result

# Delete file
init_contract.unlink()

# Create new contract that yields same name as deleted one.
new_contract = contracts / "bar.__mock__"
new_contract.write_text("BAZ")
mock_compiler.overrides = {"contractName": "foo"}

result = proj.load_contracts()
assert "foo" in result


def test_add_compiler_data(project_with_dependency_config):
# NOTE: Using different project than default to lessen
# chance of race-conditions from multi-process test runners.
Expand Down

0 comments on commit 1c18e15

Please sign in to comment.