Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit 967fdde

Browse files
authored
sol_binary generates remappings.txt compatible with Forge (#3)
* `sol_binary` generates `remappings.txt` compatible with Forge * Test for `remappings.txt` generated by `sol_binary()` * Fixes for review from @alexeagle: 1. Add `e2e/fake_remote_repo` to CI workflow. 2. Allow duplicate remappings keys i.f.f. corresponding values match. 3. Allow optional specification of a remappings output file. * Add `e2e/fake_remote_repo/.bazelrc` * Add a no-op test to `e2e/fake_remote_repo`; see `BUILD` comment for rationale. * Update `docs/rules.md` * Remove `sol_binary.outputs` left over from earlier design exploration
1 parent df20faf commit 967fdde

File tree

13 files changed

+106
-4
lines changed

13 files changed

+106
-4
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ jobs:
2222
folder:
2323
- "."
2424
- "e2e/workspace"
25+
- "e2e/fake_remote_repo"
2526

2627
# Steps represent a sequence of tasks that will be executed as part of the job
2728
steps:

docs/rules.md

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/fake_remote_repo/.bazelrc

Whitespace-only changes.

e2e/fake_remote_repo/BUILD

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
load("@aspect_rules_sol//sol:defs.bzl", "sol_sources")
2+
load("@bazel_skylib//rules:write_file.bzl", "write_file")
3+
4+
REMAP_TARGET = "./external/fake_remote_repo/"
5+
6+
sol_sources(
7+
name = "echo",
8+
srcs = ["Echo.sol"],
9+
remappings = {
10+
"remapped-fake-remote-repo/": REMAP_TARGET,
11+
# Although this alternate isn't used for imports, it's included to
12+
# demonstrate complete generation of remappings.txt by the sol_binary()
13+
# that depends on this sol_sources().
14+
"alternate-remapped-fake-remote-repo/": REMAP_TARGET,
15+
},
16+
visibility = ["//visibility:public"],
17+
)
18+
19+
# The common CI workflow for all workspaces in this repo calls `bazel test`,
20+
# which fails if no tests are present. Instead of disabling said call and
21+
# risking future tests added here not being performed, we have a no-op that
22+
# should be removed if real tests are ever added.
23+
sh_test(
24+
name = "noop_test",
25+
srcs = ["noop.sh"],
26+
)
27+
28+
write_file(
29+
name = "empty",
30+
out = "noop.sh",
31+
content = [],
32+
)

e2e/fake_remote_repo/Echo.sol

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity ^0.8.17;
3+
4+
contract Echoer {
5+
function echo(string memory payload) external pure returns (string memory) {
6+
return payload;
7+
}
8+
}

e2e/fake_remote_repo/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Fake remote repository for testing
2+
3+
`../workspace/` depends on this to demonstrate remappings, including generation of a `remappings.txt` file for compatibility with Forge.

e2e/fake_remote_repo/WORKSPACE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Override http_archive for local testing
2+
local_repository(
3+
name = "aspect_rules_sol",
4+
path = "../..",
5+
)
6+
7+
######################
8+
# rules_sol setup #
9+
######################
10+
load("@aspect_rules_sol//sol:repositories.bzl", "LATEST_VERSION", "rules_sol_dependencies", "sol_register_toolchains")
11+
12+
# Fetches the rules_sol dependencies.
13+
# If you want to have a different version of some dependency,
14+
# you should fetch it *before* calling this.
15+
# Alternatively, you can skip calling this function, so long as you've
16+
# already fetched all the dependencies.
17+
rules_sol_dependencies()
18+
19+
sol_register_toolchains(
20+
name = "solc",
21+
sol_version = LATEST_VERSION,
22+
)

e2e/workspace/BUILD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ sol_binary(
66
name = "solc",
77
srcs = ["HelloWorld.sol"],
88
ast_compact_json = True,
9+
remappings = "remappings",
10+
deps = ["@fake_remote_repo//:echo"],
911
)
1012

1113
select_file(
@@ -18,5 +20,6 @@ write_source_files(
1820
name = "write_golden",
1921
files = {
2022
"HelloWorld.json": "pick_ast",
23+
"remappings.txt": ":remappings",
2124
},
2225
)

e2e/workspace/HelloWorld.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"absolutePath":"HelloWorld.sol","exportedSymbols":{"HelloWorld":[5]},"id":6,"license":"Apache-2.0","nodeType":"SourceUnit","nodes":[{"id":1,"literals":["solidity","^","0.8",".17"],"nodeType":"PragmaDirective","src":"39:24:0"},{"abstract":false,"baseContracts":[],"canonicalName":"HelloWorld","contractDependencies":[],"contractKind":"contract","fullyImplemented":true,"id":5,"linearizedBaseContracts":[5],"name":"HelloWorld","nameLocation":"74:10:0","nodeType":"ContractDefinition","nodes":[{"constant":false,"functionSelector":"cfae3217","id":4,"mutability":"mutable","name":"greet","nameLocation":"105:5:0","nodeType":"VariableDeclaration","scope":5,"src":"91:36:0","stateVariable":true,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_string_storage","typeString":"string"},"typeName":{"id":2,"name":"string","nodeType":"ElementaryTypeName","src":"91:6:0","typeDescriptions":{"typeIdentifier":"t_string_storage_ptr","typeString":"string"}},"value":{"hexValue":"48656c6c6f20576f726c6421","id":3,"isConstant":false,"isLValue":false,"isPure":true,"kind":"string","lValueRequested":false,"nodeType":"Literal","src":"113:14:0","typeDescriptions":{"typeIdentifier":"t_stringliteral_3ea2f1d0abf3fc66cf29eebb70cbd4e7fe762ef8a09bcc06c8edf641230afec0","typeString":"literal_string \"Hello World!\""},"value":"Hello World!"},"visibility":"public"}],"scope":6,"src":"65:65:0","usedErrors":[]}],"src":"39:91:0"}
1+
{"absolutePath":"HelloWorld.sol","exportedSymbols":{"Echoer":[41],"HelloWorld":[28]},"id":29,"license":"Apache-2.0","nodeType":"SourceUnit","nodes":[{"id":1,"literals":["solidity","^","0.8",".17"],"nodeType":"PragmaDirective","src":"39:24:1"},{"absolutePath":"./external/fake_remote_repo/Echo.sol","file":"remapped-fake-remote-repo/Echo.sol","id":2,"nameLocation":"-1:-1:-1","nodeType":"ImportDirective","scope":29,"sourceUnit":42,"src":"65:44:1","symbolAliases":[],"unitAlias":""},{"abstract":false,"baseContracts":[],"canonicalName":"HelloWorld","contractDependencies":[41],"contractKind":"contract","fullyImplemented":true,"id":28,"linearizedBaseContracts":[28],"name":"HelloWorld","nameLocation":"120:10:1","nodeType":"ContractDefinition","nodes":[{"constant":false,"id":5,"mutability":"immutable","name":"echoer","nameLocation":"162:6:1","nodeType":"VariableDeclaration","scope":28,"src":"137:31:1","stateVariable":true,"storageLocation":"default","typeDescriptions":{"typeIdentifier":"t_contract$_Echoer_$41","typeString":"contract Echoer"},"typeName":{"id":4,"nodeType":"UserDefinedTypeName","pathNode":{"id":3,"name":"Echoer","nameLocations":["137:6:1"],"nodeType":"IdentifierPath","referencedDeclaration":41,"src":"137:6:1"},"referencedDeclaration":41,"src":"137:6:1","typeDescriptions":{"typeIdentifier":"t_contract$_Echoer_$41","typeString":"contract Echoer"}},"visibility":"private"},{"body":{"id":15,"nodeType":"Block","src":"189:38:1","statements":[{"expression":{"id":13,"isConstant":false,"isLValue":false,"isPure":false,"lValueRequested":false,"leftHandSide":{"id":8,"name":"echoer","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":5,"src":"199:6:1","typeDescriptions":{"typeIdentifier":"t_contract$_Echoer_$41","typeString":"contract Echoer"}},"nodeType":"Assignment","operator":"=","rightHandSide":{"arguments":[],"expression":{"argumentTypes":[],"id":11,"isConstant":false,"isLValue":false,"isPure":false,"lValueRequested":false,"nodeType":"NewExpression","src":"208:10:1","typeDescriptions":{"typeIdentifier":"t_function_creation_nonpayable$__$returns$_t_contract$_Echoer_$41_$","typeString":"function () returns (contract Echoer)"},"typeName":{"id":10,"nodeType":"UserDefinedTypeName","pathNode":{"id":9,"name":"Echoer","nameLocations":["212:6:1"],"nodeType":"IdentifierPath","referencedDeclaration":41,"src":"212:6:1"},"referencedDeclaration":41,"src":"212:6:1","typeDescriptions":{"typeIdentifier":"t_contract$_Echoer_$41","typeString":"contract Echoer"}}},"id":12,"isConstant":false,"isLValue":false,"isPure":false,"kind":"functionCall","lValueRequested":false,"nameLocations":[],"names":[],"nodeType":"FunctionCall","src":"208:12:1","tryCall":false,"typeDescriptions":{"typeIdentifier":"t_contract$_Echoer_$41","typeString":"contract Echoer"}},"src":"199:21:1","typeDescriptions":{"typeIdentifier":"t_contract$_Echoer_$41","typeString":"contract Echoer"}},"id":14,"nodeType":"ExpressionStatement","src":"199:21:1"}]},"id":16,"implemented":true,"kind":"constructor","modifiers":[],"name":"","nameLocation":"-1:-1:-1","nodeType":"FunctionDefinition","parameters":{"id":6,"nodeType":"ParameterList","parameters":[],"src":"186:2:1"},"returnParameters":{"id":7,"nodeType":"ParameterList","parameters":[],"src":"189:0:1"},"scope":28,"src":"175:52:1","stateMutability":"nonpayable","virtual":false,"visibility":"public"},{"body":{"id":26,"nodeType":"Block","src":"288:50:1","statements":[{"expression":{"arguments":[{"hexValue":"68656c6c6f20776f726c64","id":23,"isConstant":false,"isLValue":false,"isPure":true,"kind":"string","lValueRequested":false,"nodeType":"Literal","src":"317:13:1","typeDescriptions":{"typeIdentifier":"t_stringliteral_47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad","typeString":"literal_string \"hello world\""},"value":"hello world"}],"expression":{"argumentTypes":[{"typeIdentifier":"t_stringliteral_47173285a8d7341e5e972fc677286384f802f8ef42a5ec5f03bbfa254cb01fad","typeString":"literal_string \"hello world\""}],"expression":{"id":21,"name":"echoer","nodeType":"Identifier","overloadedDeclarations":[],"referencedDeclaration":5,"src":"305:6:1","typeDescriptions":{"typeIdentifier":"t_contract$_Echoer_$41","typeString":"contract Echoer"}},"id":22,"isConstant":false,"isLValue":false,"isPure":false,"lValueRequested":false,"memberLocation":"312:4:1","memberName":"echo","nodeType":"MemberAccess","referencedDeclaration":40,"src":"305:11:1","typeDescriptions":{"typeIdentifier":"t_function_external_pure$_t_string_memory_ptr_$returns$_t_string_memory_ptr_$","typeString":"function (string memory) pure external returns (string memory)"}},"id":24,"isConstant":false,"isLValue":false,"isPure":false,"kind":"functionCall","lValueRequested":false,"nameLocations":[],"names":[],"nodeType":"FunctionCall","src":"305:26:1","tryCall":false,"typeDescriptions":{"typeIdentifier":"t_string_memory_ptr","typeString":"string memory"}},"functionReturnParameters":20,"id":25,"nodeType":"Return","src":"298:33:1"}]},"functionSelector":"19ff1d21","id":27,"implemented":true,"kind":"function","modifiers":[],"name":"hello","nameLocation":"242:5:1","nodeType":"FunctionDefinition","parameters":{"id":17,"nodeType":"ParameterList","parameters":[],"src":"247:2:1"},"returnParameters":{"id":20,"nodeType":"ParameterList","parameters":[{"constant":false,"id":19,"mutability":"mutable","name":"","nameLocation":"-1:-1:-1","nodeType":"VariableDeclaration","scope":27,"src":"273:13:1","stateVariable":false,"storageLocation":"memory","typeDescriptions":{"typeIdentifier":"t_string_memory_ptr","typeString":"string"},"typeName":{"id":18,"name":"string","nodeType":"ElementaryTypeName","src":"273:6:1","typeDescriptions":{"typeIdentifier":"t_string_storage_ptr","typeString":"string"}},"visibility":"internal"}],"src":"272:15:1"},"scope":28,"src":"233:105:1","stateMutability":"view","virtual":false,"visibility":"external"}],"scope":29,"src":"111:229:1","usedErrors":[]}],"src":"39:302:1"}

e2e/workspace/HelloWorld.sol

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
// SPDX-License-Identifier: Apache-2.0
22
pragma solidity ^0.8.17;
33

4+
import "remapped-fake-remote-repo/Echo.sol";
5+
46
contract HelloWorld {
5-
string public greet = "Hello World!";
6-
}
7+
Echoer private immutable echoer;
8+
9+
constructor() {
10+
echoer = new Echoer();
11+
}
12+
13+
function hello() external view returns (string memory) {
14+
return echoer.echo("hello world");
15+
}
16+
}

0 commit comments

Comments
 (0)