Skip to content

forge flatten does not support qualified imports #2545

@adhusson

Description

@adhusson

Component

Forge

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (9ed1c37 2022-08-01T00:09:10.803586Z)

What command(s) is the bug in?

forge flatten

Operating System

macOS (Apple Silicon)

Describe the bug

Issue

When a flattened file contains a qualified import (of the form import path as Q), then:

  1. Q is not stripped from accesses to definitions found in path. For instance, Q.structName does not become structName.
  2. Names that clash with other names defined in other imported files are not modified to be unique. For instance, if the struct DataType is imported (with qualifier) from two files, compilation will fail.

Suggestion

forge flatten should strip all qualifiers, and rename definitions as needed (recursively).

Example:

// contract.sol
pragma solidity ^0.8.13;
import "./aux1.sol" as A;
import "./aux2.sol" as B;

contract C {
  struct Data { A.DataType a; B.DataType b; }
}

// aux1.sol
struct DataType { string s; }

// aux2.sol
struct DataType { string s; }

forge flatten results in this (which fails to compile):

pragma solidity ^0.8.13;
struct DataType { string s; }

struct DataType { string s; }

contract C {
  struct Data { A.DataType a; B.DataType b; }
}

when it should result in something like

pragma solidity ^0.8.13;
struct DataType1 { string s; }

struct DataType2 { string s; }

contract C {
  struct Data { DataType1 a; DataType2 b; }
}

Note: this is kind of a followup to #1440.

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

Status
Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions