Skip to content

Sort before flatten #6539

@sakulstra

Description

@sakulstra

Component

Forge

Describe the feature you would like

forge flatten should sort interfaces/contracts/lib by name before flattening.

I've had a brief look at how this could be done, but it's non obvious to me:

/// Flattens all file imports into a single string
    pub fn flatten(&self, target: &Path) -> Result<String> {
        tracing::trace!("flattening file");
        let mut input_files = self.input_files();

        // we need to ensure that the target is part of the input set, otherwise it's not
        // part of the graph if it's not imported by any input file
        let flatten_target = target.to_path_buf();
        if !input_files.contains(&flatten_target) {
            input_files.push(flatten_target);
        }

        let sources = Source::read_all_files(input_files)?;
        let graph = Graph::resolve_sources(self, sources)?;
        self.flatten_node(target, &graph, &mut Default::default(), false, false, false).map(|x| {
            format!("{}\n", utils::RE_THREE_OR_MORE_NEWLINES.replace_all(&x, "\n\n").trim())
        })
    }

I don't know rust - potentially it could be as easy as sorting nodes before transforming to a string

Additional context

I'm a heavy user of forge flatten as it's quite nice to compare an upstream deployment of a contract with a local version.
Diffing folders usually is a lot harder then diffing single files.

That said, diffing flattened files can become problematic, when - for whatever reason - the import order on one of the two sides changes. The diff will grow huge and unreadable. If the contracts and interfaces would be sorted alphabetically on the flattened files it would be far easier to understand differences.

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