From 11bca3f57eddc82e773e8aef627f7a7442b8692d Mon Sep 17 00:00:00 2001 From: Joshua Cannon Date: Fri, 2 Dec 2022 20:48:25 -0600 Subject: [PATCH] Don't strip the input of a `DictOption` file config (#17705) YAML, in particular, is trailing-newline aware, as evidenced by the new test (fails before change). Let's just let the underlying libraries handle leading/trailing space :wink: --- src/python/pants/option/options_test.py | 14 ++++++++++++++ src/python/pants/option/parser.py | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/python/pants/option/options_test.py b/src/python/pants/option/options_test.py index 1b6824a7fb6..9168d6554c9 100644 --- a/src/python/pants/option/options_test.py +++ b/src/python/pants/option/options_test.py @@ -1503,6 +1503,20 @@ def test_fromfile_yaml(self) -> None: options = self._parse(flags=f"fromfile --{'dictvalue'}=@{fp.name}") assert val == options.for_scope("fromfile")["dictvalue"] + def test_fromfile_yaml_trailing_newlines_matter(self) -> None: + with temporary_file(suffix=".yaml", binary_mode=False) as fp: + fp.write( + dedent( + """\ + a: |+ + multiline + """ + ) + ) + fp.close() + options = self._parse(flags=f"fromfile --{'dictvalue'}=@{fp.name}") + assert {"a": "multiline\n"} == options.for_scope("fromfile")["dictvalue"] + def test_fromfile_error(self) -> None: options = self._parse(flags="fromfile --string=@/does/not/exist") with pytest.raises(FromfileError): diff --git a/src/python/pants/option/parser.py b/src/python/pants/option/parser.py index 150e52adc44..ea600626b2e 100644 --- a/src/python/pants/option/parser.py +++ b/src/python/pants/option/parser.py @@ -565,13 +565,13 @@ def expand(val_or_str): fromfile = val_or_str[1:] try: with open(fromfile) as fp: - s = fp.read().strip() + s = fp.read() if fromfile.endswith(".json"): return json.loads(s) elif fromfile.endswith(".yml") or fromfile.endswith(".yaml"): return yaml.safe_load(s) else: - return s + return s.strip() except (OSError, ValueError, yaml.YAMLError) as e: raise FromfileError( f"Failed to read {dest} in {self._scope_str()} from file {fromfile}: {e!r}"