Skip to content

Commit

Permalink
fix: Fix encoding of runfiles manifest and repository mapping files. (#…
Browse files Browse the repository at this point in the history
…2568)

See
bazelbuild/bazel#374 (comment):

> 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.
  • Loading branch information
phst authored Jan 22, 2025
1 parent 188598a commit 626b03a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions examples/bzlmod/runfiles/runfiles_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!")


Expand Down
12 changes: 6 additions & 6 deletions examples/bzlmod_build_file_generation/runfiles/runfiles_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!")


Expand Down
4 changes: 2 additions & 2 deletions python/runfiles/runfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(" "):
Expand Down Expand Up @@ -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 {}
Expand Down
2 changes: 1 addition & 1 deletion tests/runfiles/runfiles_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 626b03a

Please sign in to comment.