Skip to content
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

Parse Starlark files as raw bytes for Bzlmod #24217

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public static CompiledModuleFile parseAndCompile(
ExtendedEventHandler eventHandler)
throws ExternalDepsException {
StarlarkFile starlarkFile =
StarlarkFile.parse(ParserInput.fromUTF8(moduleFile.getContent(), moduleFile.getLocation()));
StarlarkFile.parse(
ParserInput.fromLatin1(moduleFile.getContent(), moduleFile.getLocation()));
if (!starlarkFile.ok()) {
Event.replayEventsOn(eventHandler, starlarkFile.errors());
throw ExternalDepsException.withMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private static StarlarkFile readAndParseVendorFile(Path path, Environment env)
new IOException("error reading VENDOR.bazel file", e), Transience.TRANSIENT);
}
StarlarkFile starlarkFile =
StarlarkFile.parse(ParserInput.fromUTF8(contents, path.getPathString()));
StarlarkFile.parse(ParserInput.fromLatin1(contents, path.getPathString()));
if (!starlarkFile.ok()) {
Event.replayEventsOn(env.getListener(), starlarkFile.errors());
throw new VendorFileFunctionException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private static StarlarkFile readAndParseRepoFile(Path path, Environment env)
new IOException("error reading REPO.bazel file at " + path, e), Transience.TRANSIENT);
}
StarlarkFile starlarkFile =
StarlarkFile.parse(ParserInput.fromUTF8(contents, path.getPathString()));
StarlarkFile.parse(ParserInput.fromLatin1(contents, path.getPathString()));
if (!starlarkFile.ok()) {
Event.replayEventsOn(env.getListener(), starlarkFile.errors());
throw new RepoFileFunctionException(
Expand Down
23 changes: 23 additions & 0 deletions src/test/py/bazel/bzlmod/bazel_module_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import pathlib
import shutil
import subprocess
import sys
import tempfile
from absl.testing import absltest
from src.test.py.bazel import test_base
Expand Down Expand Up @@ -1109,6 +1110,28 @@ def testRegression22754(self):
self.ScratchFile('testdata/WORKSPACE')
self.RunBazel(['build', ':all'])

def testUnicodePaths(self):
if sys.getfilesystemencoding() != 'utf-8':
self.skipTest('Test requires UTF-8 by default (Python 3.7+)')

unicode_dir = 'äöüÄÖÜß'
self.ScratchFile(unicode_dir + '/MODULE.bazel', ['module(name = "module")'])
self.ScratchFile(unicode_dir + '/BUILD', [
'filegroup(name = "choose_me")',
])
self.writeMainProjectFiles()
self.ScratchFile(
'MODULE.bazel',
[
'bazel_dep(name = "module")',
'local_path_override(',
' module_name = "module",',
' path = "%s",' % unicode_dir,
')',
],
)
self.RunBazel(['build', '@module//:choose_me'])


if __name__ == '__main__':
absltest.main()
4 changes: 2 additions & 2 deletions src/test/py/bazel/bzlmod/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

def download(url):
"""Download a file and return its content in bytes."""
response = urllib.request.urlopen(url)
return response.read()
with urllib.request.urlopen(url) as response:
return response.read()


def read(path):
Expand Down
19 changes: 7 additions & 12 deletions src/test/py/bazel/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

"""Bazel Python integration test framework."""

import locale
import os
import shutil
import socket
Expand Down Expand Up @@ -313,7 +312,7 @@ def ScratchFile(self, path, lines=None, executable=False):
if os.path.exists(abspath) and not os.path.isfile(abspath):
raise IOError('"%s" (%s) exists and is not a file' % (path, abspath))
self.ScratchDir(os.path.dirname(path))
with open(abspath, 'w') as f:
with open(abspath, 'w', encoding='utf-8') as f:
if lines:
for l in lines:
f.write(l)
Expand Down Expand Up @@ -445,7 +444,7 @@ def StopRemoteWorker(self):

self._worker_stdout.seek(0)
stdout_lines = [
l.decode(locale.getpreferredencoding()).strip()
l.decode('utf-8').strip()
for l in self._worker_stdout.readlines()
]
if stdout_lines:
Expand All @@ -455,7 +454,7 @@ def StopRemoteWorker(self):

self._worker_stderr.seek(0)
stderr_lines = [
l.decode(locale.getpreferredencoding()).strip()
l.decode('utf-8').strip()
for l in self._worker_stderr.readlines()
]
if stderr_lines:
Expand Down Expand Up @@ -509,18 +508,14 @@ def RunProgram(

stdout.seek(0)
stdout_lines = [
l.decode(locale.getpreferredencoding()).rstrip()
if rstrip
else l.decode(locale.getpreferredencoding()).strip()
for l in stdout.readlines()
l.decode('utf-8').rstrip() if rstrip else l.decode('utf-8').strip()
for l in stdout.readlines()
]

stderr.seek(0)
stderr_lines = [
l.decode(locale.getpreferredencoding()).rstrip()
if rstrip
else l.decode(locale.getpreferredencoding()).strip()
for l in stderr.readlines()
l.decode('utf-8').rstrip() if rstrip else l.decode('utf-8').strip()
for l in stderr.readlines()
]

if not allow_failure:
Expand Down