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

mformat: try to detect meson.format in parent directories #14178

Merged
merged 1 commit into from
Jan 31, 2025

Conversation

wrvsrx
Copy link
Contributor

@wrvsrx wrvsrx commented Jan 23, 2025

Fix #14167. This PR makes mformat search meson.format in parent directories.

@wrvsrx wrvsrx requested a review from jpakkane as a code owner January 23, 2025 04:52
@wrvsrx wrvsrx marked this pull request as draft January 23, 2025 04:57
@wrvsrx wrvsrx marked this pull request as ready for review January 23, 2025 05:28
test cases/format/4 config/meson.build Outdated Show resolved Hide resolved
default_config_path = sources[0].parent / 'meson.format'
if default_config_path.exists():
options.configuration = default_config_path
current_path = Path.cwd()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Searching from cwd is wrong, as you could invoke the command from an unrelated directory.

An option could be to search in parents of the given source file. My concern however is that you have no way to know when you reached the sources root.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, when searching upwards and encountering meson.format, the search should stop. If it is not found, I can think of two methods to determine the source root.

  1. Use some heuristic methods, such as stopping the search when encountering files like .git. This method is the simplest and less accurate, but it is at least better than the current approach of only looking for meson.format in sources[0].parent.
  2. First, search for meson.build files in each parent directory. If found, parse the file to check if it is the top-level meson.build (determined by whether it starts with project()). In my understanding, this method is accurate but less efficient.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterating the parents of the current source is probably the way to go, even if we could potentially go outside the source root.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could lead to a problem: consider a repository A that does not contain a meson.format file. If it exists as a submodule within a repository B that does contain a meson.format file, running meson format on A would use the meson.format from repository B. This might not align with the user's intentions.

But at least it's better than only use meson.format in sources[0].parent. I'll update this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First, search for meson.build files in each parent directory. If found, parse the file to check if it is the top-level meson.build (determined by whether it starts with project()). In my understanding, this method is accurate but less efficient.

That is what we do in mesonbuild/interpreterbase/interpreterbase.py, see sanity_check_ast. But it does require instantiating mparser.Parser() once for each file...

@wrvsrx wrvsrx force-pushed the fix-mformat branch 2 times, most recently from 6928bd1 to 7338dae Compare January 30, 2025 02:29
@wrvsrx wrvsrx requested a review from bruchar1 January 30, 2025 02:31
@wrvsrx wrvsrx force-pushed the fix-mformat branch 3 times, most recently from f8b1557 to 45d54ca Compare January 30, 2025 03:04
Copy link
Member

@bruchar1 bruchar1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not rebuild a Formatter object for every source file. This will be slow, especially when running recursively on big projects.

Also, parsing a different config file for each source file is a breaking change. If this is really what we want, this should be documented.

If we really want to do this, we should implement something similar to what is done for .editorconfig. A caching mechanism would also be something to consider in that case.

@wrvsrx
Copy link
Contributor Author

wrvsrx commented Jan 30, 2025

Maybe I should only find the meson.format only using the path of sources[0]?

@bruchar1

@bruchar1
Copy link
Member

Maybe I should only find the meson.format only using the path of sources[0]?

Yes, that's what I think. Or iterate on parents of all of the given sources until you find one, but outside of the loop that actually do the formatting, because in that loop, sources is a queue where all child items are added when doing the recursion.

mesonbuild/mformat.py Outdated Show resolved Hide resolved
mesonbuild/mformat.py Outdated Show resolved Hide resolved
@dcbaker
Copy link
Member

dcbaker commented Jan 31, 2025

@bruchar1 When you think that this is ready ping me and I'll merge

Copy link
Member

@bruchar1 bruchar1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ping @dcbaker

@dcbaker dcbaker merged commit c0da4a5 into mesonbuild:master Jan 31, 2025
30 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

meson format meson.build in subdir should follow meson.format in toplevel directory.
4 participants