The flatten task emits a file combining all the Solidity files in the project (or a subset, when a list of files is passed). Ideally, this output should be compilable as-is. There are at least two scenarios where this doesn't happen: when the flattened file has more than one SPDX license and when it has repeated and/or incompatible pragma experimental or pragma abicoder statements.

SPDX Licenses

The easiest approach to fix this problem, and something users frequently do, is to just remove all the SPDX Licenses, maybe leaving the first one. We could do this automatically, but it's probably not a decision that we should take on our own, and will probably violate all the licenses.

Instead, we should add a single license at the top, merging all the licenses in the sources. We should also comment out all the existing licenses to make solc ignore them, but leaving them in place, to communicate the actual license of each part of the flattened output.

Commenting out SPDX licenses is not trivial because they are already inside a comment. It seems like the compiler will pick any comment with the SPDX pattern and interpret it as a license. So we need to modify the string in some way. We can replace the - for _, or just remove the colon:

/* Original license: SPDX_License_Identifier: MIT */
/* Original license: SPDX-License-Identifier MIT */

The only exception to this commenting-out process is when all the licenses are the same. If that happens we should just remove them.

Finally, we should add a license at the top of the output with all the different licenses (without repetition), joined by an AND. For example SPDX-License-Identifier MIT AND MPL-2.0

Repeated or incompatible ABI pragmas

You can only define one abi encoder to be used in a file. This can lead to multiple problems when flattening:

  1. One of the files has the abi v2 pragma (i.e. pragma experimental ABIEncoderV2 or pragma abicoder v2) and another file has no abi related pragma.
  2. One of the files has the abi v2 pragma and another one has the abi v1 pragma (i.e. pragma abicoder v1)
  3. Multiple files have the same ABI pragma

Like with the licenses, we don't want to remove the ABI pragmas, but just comment them out instead, and add one to the top of the output.

In the case (1) and (2) above, there's no correct abi pragma that could be added, but we default to adding an abi v2 pragma. We should also print a warning to stderr after the flattened output is printed to stdout.

In the case (3) we would just add the repeated abi pragma at the top.

Test cases

This is a list of test cases for the new flatten task. In all scenarios, the first two files are flattened and the result is the third file. The actual result includes comments indicating that the file was flattened with hardhat, and where each file begins, but they are omitted here to be more concise.

Some cases also indicate that a warning should be printed.

Simple case