Local Library Imports in Sway: A Challenge
Hello there! We’re grappling with a rather intricate issue concerning local library imports within Sway. Let’s delve into this problem.
The Scenario
Imagine you have a contract consisting of three files: structs.sw
, events.sw
, and main.sw
. I need to import a struct defined in structs.sw
into events.sw
. It sounds simple, but there’s more to it than meets the eye.
The Contract’s Components
structs.sw
This library, structs.sw
, holds the struct Counter
and its associated default()
implementation. The struct Counter
is used in the storage of main.sw
.
events.sw
Now, events.sw
is where the action is. It houses the struct IncrementEvent
, which is vital for the indexer, and requires the struct Counter
.
main.sw
In our main file, main.sw
, we import both events.sw
and structs.sw
libraries. But our usage of Counter
in the storage is where things get interesting.
Additionally, within the fn increment()
, there’s an event named IncrementEvent
.
The Challenge
Here’s where it gets tricky. We need to import struct Counter
, which is defined in the structs.sw
library, into the events.sw
library. But when we run forc build
, it throws this error:
>>> forc build
Compiling library core (/Users/alexey/.forc/git/checkouts/std-9be0d6062747ea7/04a597093e7441898933dd412b8e4dc6ac860cd3/sway-lib-core)
Compiling library std (git+https://github.com/fuellabs/sway?tag=v0.44.1#04a597093e7441898933dd412b8e4dc6ac860cd3)
Compiling contract counter-contract (/Users/alexey/Desktop/fuel-counter-contract/counter-contract)
error
--> /Users/alexey/Desktop/fuel-counter-contract/counter-contract/src/main.sw:1:5
|
0 | contract;
1 | mod events;
| ^^^^^^ Module "events::structs" could not be found.
2 | mod structs;
3 | use events::*;
|
____
error
--> /Users/alexey/Desktop/fuel-counter-contract/counter-contract/src/events.sw:6:14
|
4 |
5 | pub struct IncrementEvent{
6 | counter: Counter
| ^^^^^^^ Could not find symbol "Counter" in this scope.
7 | }
|
____
error
--> /Users/alexey/Desktop/fuel-counter-contract/counter-contract/src/events.sw:6:14
|
4 |
5 | pub struct IncrementEvent{
6 | counter: Counter
| ^^^^^^^ Unknown type name "Counter".
7 | }
|
____
Aborting due to 3 errors.
Error: Failed to compile counter-contract
Toolchain
Default host: aarch64-apple-darwin
fuelup home: /Users/alexey/.fuelup
installed toolchains
--------------------
beta-3-aarch64-apple-darwin
beta-4-rc.2-aarch64-apple-darwin
latest-aarch64-apple-darwin (default)
hotfix
my-custom-toolchain
active toolchain
-----------------
latest-aarch64-apple-darwin (default)
forc : 0.44.1
- forc-client
- forc-deploy : 0.44.1
- forc-run : 0.44.1
- forc-doc : 0.44.1
- forc-explore : 0.28.1
- forc-fmt : 0.44.1
- forc-index : 0.20.7
- forc-lsp : 0.44.1
- forc-tx : 0.44.1
- forc-wallet : 0.3.0
fuel-core : 0.20.4
fuel-core-keygen : Error getting version string
fuel-indexer : 0.20.7
fuels versions
---------------
forc : 0.45
forc-wallet : 0.45
How to reproduce
fuelup self update
fuelup install latest
fuelup default latest
git clone https://github.com/PaulZhemanov/fuel-counter-contract.git
cd fuel-counter-contract.
git checkout library-import-issue
cd counter-contract
forc build