Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

bgilbert
Copy link
Contributor

@bgilbert bgilbert commented Sep 5, 2025

Fixes #14997.

@bgilbert bgilbert requested a review from jpakkane as a code owner September 5, 2025 04:53
@bonzini bonzini added this to the 1.9.1 milestone Sep 5, 2025
@bonzini
Copy link
Collaborator

bonzini commented Sep 5, 2025

Independent from this, you need to keep the first Rust file the same or, even better, move it first.

@bgilbert
Copy link
Contributor Author

bgilbert commented Sep 5, 2025

Hmm? This PR doesn't touch anything Rust-related.

@bonzini
Copy link
Collaborator

bonzini commented Sep 5, 2025

Hmm? This PR doesn't touch anything Rust-related.

Yes, it is an independent bug. The problem is that the first file is special for Rust, and typically in the top directory, so if you put subdirectories first you change the semantics (unlike C).

@bgilbert
Copy link
Contributor Author

bgilbert commented Sep 5, 2025

If the project needs a specific file order, it can still do something like

sources = files('main.rs')
sources += files(
  'subdir/a.rs',
  'subdir/b.rs',
)

or

executable(
  'main',
  'main.rs',
  files(
    'subdir/a.rs',
    'subdir/b.rs',
  )
)

and the formatter won't interfere with that. There are any number of programs that might receive lists of files and have opinions about the file order, and I don't think the formatter should start carrying special cases for those.

@bonzini
Copy link
Collaborator

bonzini commented Sep 6, 2025

There are any number of programs that might receive lists of files and have opinions about the file order, and I don't think the formatter should start carrying special cases for those.

If the formatter can change the semantics of the meson.build, I think that should be an opt-in.

@bgilbert
Copy link
Contributor Author

bgilbert commented Sep 6, 2025

It already could, though. There's no guarantee that the desired "first Rust file" sorts first under the current sort order, and sort_files has defaulted to true since the first commit in 2b37101. If you care about the order of your files(), it's not surprising that the formatter will break things if you let it reorder them.

In other words, this PR doesn't change anything fundamental about how the formatter works. It just fixes the sort order to bring the formatter in line with the Meson style guide and the existing behavior of the rewriter (added in #5010 for 0.50).

@eli-schwartz
Copy link
Member

Independent from this, you need to keep the first Rust file the same or, even better, move it first.

I assume you mean the hardcoded values "main.rs" / "lib.rs" and no other? Or... ?

@bonzini
Copy link
Collaborator

bonzini commented Sep 9, 2025

main.rs and lib.rs are just Cargo defaults (and they don't apply to tests/ and examples/, which treat all files in the directory as main files).

ninjabackend.py passes the first .rs file to rustc.

@eli-schwartz
Copy link
Member

So then if the first file isn't first, then I suppose it is doable to simply exclude the first element from sorting, i.e.

Independent from this, you need to keep the first Rust file the same

but ... how does meson format know what to

or, even better, move it first.

?

@bonzini
Copy link
Collaborator

bonzini commented Sep 9, 2025

It's not a regression so this PR is fine. But it could be a future improvement. By "move it first" I mean move the first Rust source even before e.g. .c files if the same target includes both languages.

@bgilbert
Copy link
Contributor Author

bgilbert commented Sep 9, 2025

The other thing is that the sources for a target may be built from any number of files() calls, or none at all. Should the formatter reorder the first Rust file of every files() call, just in case that call forms the first list for a target?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

format: files() sort order doesn't match style guide
3 participants