Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forge flatten sort contracts alphabetically #9788

Closed
sakulstra opened this issue Jan 30, 2025 · 8 comments · Fixed by foundry-rs/compilers#247 or #9836
Closed

forge flatten sort contracts alphabetically #9788

sakulstra opened this issue Jan 30, 2025 · 8 comments · Fixed by foundry-rs/compilers#247 or #9836
Labels
C-forge Command: forge Cmd-forge-flatten Command: forge flatten T-feature Type: feature

Comments

@sakulstra
Copy link
Contributor

Component

Forge

Describe the feature you would like

We rely on flatten quite heavily to diff code.
One huge pain point with this is that flattening two versions of a contract can result in dramatically different code as import order might have changed.

Therefore it would be a huge help/improvement if flatten would sort contacts alphabetically, either as default or when passing a flag.

Additional context

No response

@sakulstra sakulstra added T-feature Type: feature T-needs-triage Type: this issue needs to be labelled labels Jan 30, 2025
@github-project-automation github-project-automation bot moved this to Todo in Foundry Jan 30, 2025
@sakulstra sakulstra changed the title forge flatten sort forge flatten sort contracts alphabetically Jan 30, 2025
@grandizzy grandizzy added C-forge Command: forge Cmd-forge-flatten Command: forge flatten and removed T-needs-triage Type: this issue needs to be labelled labels Jan 30, 2025
@grandizzy
Copy link
Collaborator

grandizzy commented Jan 30, 2025

per foundry compilers comment

/// We can't just sort files alphabetically as it might break compilation, because Solidity
/// does not allow base class definitions to appear after derived contract
/// definitions.
///
/// Instead, we sort files by the number of their dependencies (imports of any depth) in ascending
/// order. If files have the same number of dependencies, we sort them alphabetically.
/// Target file is always placed last.

simple test yields solc failure with Error (2449): Definition of base has to precede definition of derived contract

@klkvr could you please chime in?

@grandizzy grandizzy added the T-to-discuss Type: requires discussion label Jan 30, 2025
@sakulstra
Copy link
Contributor Author

sakulstra commented Feb 4, 2025

@grandizzy interesting, i was not aware.

For us this would still be insanely useful 😅 - we don't actually want to build/test or deploy the flattened code. We just want to compare it.
The realworld problem we have is for example here vs here.

In the first Address comes first, in the second Initializable comes first. Both i think don't have dependencies.


Perhaps this overall approach is a bit suboptimal and there might be better ways to do this?
Would be curious on your opinions, I guess contract diffing could be a quite useful feature on foundry itself as i guess everyone working with proxies would be interested in this.

@klkvr
Copy link
Member

klkvr commented Feb 4, 2025

@sakulstra this was initially addressed in scope of foundry-rs/compilers#52 to resolve #6539

Sorting contracs alphabetically is not feasible because of the base has to precede definition of derived contract error. When working on this I was mostly optimizing for diff readability which I think is good enough now as order of files is pretty much deterministic as long as imported fileds not change.

The realworld problem we have is for example here vs here.

In the first Address comes first, in the second Initializable comes first. Both i think don't have dependencies.

This is happening because in the second file, Initializable.sol is coming from lib/aave-v3-origin/.. which is lexicographically higher than Address.sol path

@sakulstra
Copy link
Contributor Author

sakulstra commented Feb 4, 2025

This is happening because in the second file, Initializable.sol is coming from lib/aave-v3-origin/.. which is lexicographically higher than Address.sol path

Sorry, i don't see it 😅

How is Initializable lexicographically higher than Address? Or is the path considered for the sorting... which would be weird, no?
As in this case changing from e.g. submodules to soldeer or npm would completely change the output i guess?

@klkvr
Copy link
Member

klkvr commented Feb 6, 2025

Or is the path considered for the sorting... which would be weird, no?

yeah path is considered right now

As in this case changing from e.g. submodules to soldeer or npm would completely change the output i guess?

yeah tbh I'd say this is an edge case I just didn't consider at the time :) I guess we can use path as secondary sorting key, should be fixed in foundry-rs/compilers#247

mattsse pushed a commit to foundry-rs/compilers that referenced this issue Feb 6, 2025
Closes foundry-rs/foundry#9788

ref
foundry-rs/foundry#9788 (comment)

Instead of sorting by path, firstly attempts sorting by filenames
@github-project-automation github-project-automation bot moved this from Todo to Done in Foundry Feb 6, 2025
@grandizzy
Copy link
Collaborator

going to reopen as a placeholder for bumping compiler version

@grandizzy grandizzy reopened this Feb 6, 2025
@grandizzy grandizzy added Cmd-forge-flatten Command: forge flatten and removed Cmd-forge-flatten Command: forge flatten T-to-discuss Type: requires discussion labels Feb 6, 2025
@mattsse
Copy link
Member

mattsse commented Feb 6, 2025

sending patch @grandizzy

@mattsse
Copy link
Member

mattsse commented Feb 6, 2025

patch is out

grandizzy added a commit to grandizzy/foundry that referenced this issue Feb 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-forge Command: forge Cmd-forge-flatten Command: forge flatten T-feature Type: feature
Projects
Status: Done
4 participants