How to import a local library into another local library in sway?

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

Hi there!!

It seems that you should switch the order of imports:

  • first mod the structs in main.sw
  • then mod the events

Let me know if that works

Hi, thats updated code

contract;
mod structs;
mod events;
use structs::*;
use events::*;

use std::logging::log;
use std::auth::msg_sender;

Forc build result

 forc build                                                                                       (library-import-issue+9) 23:08:21 
 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:3:5
  |
1 | 
2 | mod structs;
3 | mod events;
  |     ^^^^^^ Module "events::structs" could not be found.
4 | use structs::*;
5 | 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

repo GitHub - PaulZhemanov/fuel-counter-contract

Also, I tryed to mod structs in events like here

library;
mod structs;
use structs::Counter;

pub struct IncrementEvent{
    counter: Counter
}

Error:

error
 --> /Users/alexey/Desktop/fuel-counter-contract/counter-contract/src/events.sw:1:5
  |
0 | library;
1 | mod structs;
  |     ^^^^^^^ The file /Users/alexey/Desktop/fuel-counter-contract/counter-contract/src/events/structs.sw could not be read: No such file or directory (os error 2)
2 | use structs::Counter;
  |
____

error
 --> /Users/alexey/Desktop/fuel-counter-contract/counter-contract/src/main.sw:3:5
  |
1 | 
2 | mod structs;
3 | mod events;
  |     ^^^^^^ Module "events::structs" could not be found.
4 | use structs::*;
5 | 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 4 errors.
Error: Failed to compile counter-contract

:thinking: :thinking:

I’ve escalated the issue but this seems like you reached one boundary of sway. You can get more details here.

I suggest just to duplicate the properties of counter inside the log . It’s not that elegant but seems to work.

events.sw

library;

pub struct IncrementEvent{
    value: u64,
    caller: Identity,
    timestamp: u64,
}
1 Like

The solution is to change use structs::Counter; to use ::structs::Counter; in events.sw.

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.