Skip to content

Fixes for meson format from stdin using editor config #14540

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/markdown/Commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,9 @@ or `--check-only` option).
input instead of reading it from a file. This cannot be used with `--recursive`
or `--inline` arguments.

*Since 1.9.0* Using `-` as source file with `--editor-config` now requires
`--source-file-path` argument to ensure consistent results.


#### Differences with `muon fmt`

Expand Down
5 changes: 5 additions & 0 deletions docs/markdown/snippets/meson-format-stdin-editorconfig.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## meson format now has a --source-file-path argument when reading from stdin

This argument is mandatory to mix stdin reading with the use of editor config.
It allows to know where to look for the .editorconfig, and to use the right
section of .editorconfig based on the parsed file name.
21 changes: 19 additions & 2 deletions mesonbuild/mformat.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,15 @@ def load_editor_config(self, source_file: Path) -> EditorConfig:
# See https://editorconfig.org/
config = EditorConfig()

for p in source_file.parents:
if source_file == Path('STDIN'):
raise MesonException('Using editorconfig with stdin requires --source-file-path argument')

try:
source_file_path = source_file.resolve()
except FileNotFoundError:
raise MesonException(f'Unable to resolve path for "{source_file}"')

for p in source_file_path.parents:
editorconfig_file = p / '.editorconfig'
if not editorconfig_file.exists():
continue
Expand Down Expand Up @@ -955,6 +963,11 @@ def add_arguments(parser: argparse.ArgumentParser) -> None:
type=Path,
help='output file (implies having exactly one input)'
)
parser.add_argument(
'--source-file-path',
type=Path,
help='path to use, when reading from stdin'
)
parser.add_argument(
'sources',
nargs='*',
Expand All @@ -981,6 +994,10 @@ def run(options: argparse.Namespace) -> int:
raise MesonException('--recursive argument is not compatible with stdin input')
if options.inplace and from_stdin:
raise MesonException('--inplace argument is not compatible with stdin input')
if options.source_file_path and not from_stdin:
raise MesonException('--source-file-path argument is only compatible with stdin input')
if from_stdin and options.editor_config and not options.source_file_path:
raise MesonException('using --editor-config with stdin input requires --source-file-path argument')

sources: T.List[Path] = options.sources.copy() or [Path(build_filename)]

Expand All @@ -996,7 +1013,7 @@ def run(options: argparse.Namespace) -> int:

try:
if from_stdin:
src_file = Path('STDIN') # used for error messages and introspection
src_file = options.source_file_path or Path('STDIN') # used for error messages and introspection
code = sys.stdin.read()
else:
code = src_file.read_text(encoding='utf-8')
Expand Down
Loading