From 626b03a9fadf076abe50c32b07242ce3bf29bdf3 Mon Sep 17 00:00:00 2001 From: Philipp Stephani Date: Wed, 22 Jan 2025 17:58:32 +0100 Subject: [PATCH] fix: Fix encoding of runfiles manifest and repository mapping files. (#2568) See https://github.com/bazelbuild/bazel/issues/374#issuecomment-2594713891: > all output files produced by Bazel should use UTF-8 and \n line endings on > all platforms, including Windows. Previously this would use the legacy ANSI codepage on Windows. --- CHANGELOG.md | 2 ++ examples/bzlmod/runfiles/runfiles_test.py | 12 ++++++------ .../runfiles/runfiles_test.py | 12 ++++++------ python/runfiles/runfiles.py | 4 ++-- tests/runfiles/runfiles_test.py | 2 +- 5 files changed, 17 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00624db01f..9fdce66550 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -63,6 +63,8 @@ Unreleased changes template. * (gazelle) Providing multiple input requirements files to `gazelle_python_manifest` now works correctly. * (pypi) Handle trailing slashes in pip index URLs in environment variables, fixes [#2554](https://github.com/bazelbuild/rules_python/issues/2554). +* (runfiles) Runfile manifest and repository mapping files are now interpreted + as UTF-8 on all platforms. {#v0-0-0-added} ### Added diff --git a/examples/bzlmod/runfiles/runfiles_test.py b/examples/bzlmod/runfiles/runfiles_test.py index e1ba14e569..7b7e87726a 100644 --- a/examples/bzlmod/runfiles/runfiles_test.py +++ b/examples/bzlmod/runfiles/runfiles_test.py @@ -27,36 +27,36 @@ def testCurrentRepository(self): def testRunfilesWithRepoMapping(self): data_path = runfiles.Create().Rlocation("example_bzlmod/runfiles/data/data.txt") - with open(data_path) as f: + with open(data_path, "rt", encoding="utf-8", newline="\n") as f: self.assertEqual(f.read().strip(), "Hello, example_bzlmod!") def testRunfileWithRlocationpath(self): data_rlocationpath = os.getenv("DATA_RLOCATIONPATH") data_path = runfiles.Create().Rlocation(data_rlocationpath) - with open(data_path) as f: + with open(data_path, "rt", encoding="utf-8", newline="\n") as f: self.assertEqual(f.read().strip(), "Hello, example_bzlmod!") def testRunfileInOtherModuleWithOurRepoMapping(self): data_path = runfiles.Create().Rlocation( "our_other_module/other_module/pkg/data/data.txt" ) - with open(data_path) as f: + with open(data_path, "rt", encoding="utf-8", newline="\n") as f: self.assertEqual(f.read().strip(), "Hello, other_module!") def testRunfileInOtherModuleWithItsRepoMapping(self): data_path = lib.GetRunfilePathWithRepoMapping() - with open(data_path) as f: + with open(data_path, "rt", encoding="utf-8", newline="\n") as f: self.assertEqual(f.read().strip(), "Hello, other_module!") def testRunfileInOtherModuleWithCurrentRepository(self): data_path = lib.GetRunfilePathWithCurrentRepository() - with open(data_path) as f: + with open(data_path, "rt", encoding="utf-8", newline="\n") as f: self.assertEqual(f.read().strip(), "Hello, other_module!") def testRunfileInOtherModuleWithRlocationpath(self): data_rlocationpath = os.getenv("OTHER_MODULE_DATA_RLOCATIONPATH") data_path = runfiles.Create().Rlocation(data_rlocationpath) - with open(data_path) as f: + with open(data_path, "rt", encoding="utf-8", newline="\n") as f: self.assertEqual(f.read().strip(), "Hello, other_module!") diff --git a/examples/bzlmod_build_file_generation/runfiles/runfiles_test.py b/examples/bzlmod_build_file_generation/runfiles/runfiles_test.py index 5bfa5302ef..6ce4c2db37 100644 --- a/examples/bzlmod_build_file_generation/runfiles/runfiles_test.py +++ b/examples/bzlmod_build_file_generation/runfiles/runfiles_test.py @@ -29,36 +29,36 @@ def testRunfilesWithRepoMapping(self): data_path = runfiles.Create().Rlocation( "example_bzlmod_build_file_generation/runfiles/data/data.txt" ) - with open(data_path) as f: + with open(data_path, "rt", encoding="utf-8", newline="\n") as f: self.assertEqual(f.read().strip(), "Hello, example_bzlmod!") def testRunfileWithRlocationpath(self): data_rlocationpath = os.getenv("DATA_RLOCATIONPATH") data_path = runfiles.Create().Rlocation(data_rlocationpath) - with open(data_path) as f: + with open(data_path, "rt", encoding="utf-8", newline="\n") as f: self.assertEqual(f.read().strip(), "Hello, example_bzlmod!") def testRunfileInOtherModuleWithOurRepoMapping(self): data_path = runfiles.Create().Rlocation( "our_other_module/other_module/pkg/data/data.txt" ) - with open(data_path) as f: + with open(data_path, "rt", encoding="utf-8", newline="\n") as f: self.assertEqual(f.read().strip(), "Hello, other_module!") def testRunfileInOtherModuleWithItsRepoMapping(self): data_path = lib.GetRunfilePathWithRepoMapping() - with open(data_path) as f: + with open(data_path, "rt", encoding="utf-8", newline="\n") as f: self.assertEqual(f.read().strip(), "Hello, other_module!") def testRunfileInOtherModuleWithCurrentRepository(self): data_path = lib.GetRunfilePathWithCurrentRepository() - with open(data_path) as f: + with open(data_path, "rt", encoding="utf-8", newline="\n") as f: self.assertEqual(f.read().strip(), "Hello, other_module!") def testRunfileInOtherModuleWithRlocationpath(self): data_rlocationpath = os.getenv("OTHER_MODULE_DATA_RLOCATIONPATH") data_path = runfiles.Create().Rlocation(data_rlocationpath) - with open(data_path) as f: + with open(data_path, "rt", encoding="utf-8", newline="\n") as f: self.assertEqual(f.read().strip(), "Hello, other_module!") diff --git a/python/runfiles/runfiles.py b/python/runfiles/runfiles.py index ea816c64fd..3943be5646 100644 --- a/python/runfiles/runfiles.py +++ b/python/runfiles/runfiles.py @@ -56,7 +56,7 @@ def RlocationChecked(self, path: str) -> Optional[str]: def _LoadRunfiles(path: str) -> Dict[str, str]: """Loads the runfiles manifest.""" result = {} - with open(path, "r") as f: + with open(path, "r", encoding="utf-8", newline="\n") as f: for line in f: line = line.rstrip("\n") if line.startswith(" "): @@ -367,7 +367,7 @@ def _ParseRepoMapping(repo_mapping_path: Optional[str]) -> Dict[Tuple[str, str], if not repo_mapping_path: return {} try: - with open(repo_mapping_path, "r") as f: + with open(repo_mapping_path, "r", encoding="utf-8", newline="\n") as f: content = f.read() except FileNotFoundError: return {} diff --git a/tests/runfiles/runfiles_test.py b/tests/runfiles/runfiles_test.py index cf6a70a020..a3837ac842 100644 --- a/tests/runfiles/runfiles_test.py +++ b/tests/runfiles/runfiles_test.py @@ -552,7 +552,7 @@ def __init__( def __enter__(self) -> Any: tmpdir = os.environ.get("TEST_TMPDIR") self._path = os.path.join(tempfile.mkdtemp(dir=tmpdir), self._name) - with open(self._path, "wt") as f: + with open(self._path, "wt", encoding="utf-8", newline="\n") as f: f.writelines(l + "\n" for l in self._contents) return self