Skip to content

Commit 550cf5e

Browse files
bruchar1jpakkane
authored andcommitted
format: add --source-file-path argument for stdin
Fixes #14539. Otherwise, .editorconfig is read from current working directory, and there is no way to know what file name to filter to choose the right section of editor config.
1 parent b2a266b commit 550cf5e

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

docs/markdown/Commands.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ or `--check-only` option).
493493
input instead of reading it from a file. This cannot be used with `--recursive`
494494
or `--inline` arguments.
495495

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

497500
#### Differences with `muon fmt`
498501

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## meson format now has a --source-file-path argument when reading from stdin
2+
3+
This argument is mandatory to mix stdin reading with the use of editor config.
4+
It allows to know where to look for the .editorconfig, and to use the right
5+
section of .editorconfig based on the parsed file name.

mesonbuild/mformat.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,15 @@ def load_editor_config(self, source_file: Path) -> EditorConfig:
837837
# See https://editorconfig.org/
838838
config = EditorConfig()
839839

840-
for p in source_file.resolve().parents:
840+
if source_file == Path('STDIN'):
841+
raise MesonException('Using editorconfig with stdin requires --source-file-path argument')
842+
843+
try:
844+
source_file_path = source_file.resolve()
845+
except FileNotFoundError:
846+
raise MesonException(f'Unable to resolve path for "{source_file}"')
847+
848+
for p in source_file_path.parents:
841849
editorconfig_file = p / '.editorconfig'
842850
if not editorconfig_file.exists():
843851
continue
@@ -955,6 +963,11 @@ def add_arguments(parser: argparse.ArgumentParser) -> None:
955963
type=Path,
956964
help='output file (implies having exactly one input)'
957965
)
966+
parser.add_argument(
967+
'--source-file-path',
968+
type=Path,
969+
help='path to use, when reading from stdin'
970+
)
958971
parser.add_argument(
959972
'sources',
960973
nargs='*',
@@ -981,6 +994,10 @@ def run(options: argparse.Namespace) -> int:
981994
raise MesonException('--recursive argument is not compatible with stdin input')
982995
if options.inplace and from_stdin:
983996
raise MesonException('--inplace argument is not compatible with stdin input')
997+
if options.source_file_path and not from_stdin:
998+
raise MesonException('--source-file-path argument is only compatible with stdin input')
999+
if from_stdin and options.editor_config and not options.source_file_path:
1000+
raise MesonException('using --editor-config with stdin input requires --source-file-path argument')
9841001

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

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

9971014
try:
9981015
if from_stdin:
999-
src_file = Path('STDIN') # used for error messages and introspection
1016+
src_file = options.source_file_path or Path('STDIN') # used for error messages and introspection
10001017
code = sys.stdin.read()
10011018
else:
10021019
code = src_file.read_text(encoding='utf-8')

0 commit comments

Comments
 (0)