Skip to content

Commit a5d5cea

Browse files
committed
Revert "Build: pass PATH environment variable to Docker container (#10133)"
This reverts commit bc4bced.
1 parent 8fb5409 commit a5d5cea

File tree

1 file changed

+28
-44
lines changed

1 file changed

+28
-44
lines changed

readthedocs/doc_builder/environments.py

+28-44
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from docker.errors import APIError as DockerAPIError
1515
from docker.errors import DockerException
1616
from docker.errors import NotFound as DockerNotFoundError
17-
from requests.exceptions import ConnectionError, ReadTimeout # noqa
17+
from requests.exceptions import ConnectionError, ReadTimeout
1818
from requests_toolbelt.multipart.encoder import MultipartEncoder
1919

2020
from readthedocs.api.v2.client import api as api_v2
@@ -73,7 +73,7 @@ def __init__(
7373
bin_path=None,
7474
record_as_success=False,
7575
demux=False,
76-
**kwargs, # pylint: disable=unused-argument
76+
**kwargs,
7777
):
7878
self.command = command
7979
self.shell = shell
@@ -252,8 +252,8 @@ def save(self):
252252
{key: str(value) for key, value in data.items()}
253253
)
254254
resource = api_v2.command
255-
resp = resource._store["session"].post( # pylint: disable=protected-access
256-
resource._store["base_url"] + "/", # pylint: disable=protected-access
255+
resp = resource._store['session'].post(
256+
resource._store['base_url'] + '/',
257257
data=encoder,
258258
headers={
259259
'Content-Type': encoder.content_type,
@@ -301,40 +301,11 @@ def run(self):
301301

302302
self.start_time = datetime.utcnow()
303303
client = self.build_env.get_client()
304-
305-
# Create a copy of the environment to update PATH variable
306-
environment = self._environment.copy()
307-
# Default PATH variable
308-
# This default comes from our Docker image:
309-
#
310-
# $ docker run --user docs -it --rm readthedocs/build:ubuntu-22.04 /bin/bash
311-
# docs@bfe702e31cdd:~$ echo $PATH
312-
# /home/docs/.asdf/shims:/home/docs/.asdf/bin
313-
# :/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
314-
# docs@bfe702e31cdd:~$
315-
asdf_paths = (
316-
f"/home/{settings.RTD_DOCKER_USER}/.asdf/shims"
317-
f":/home/{settings.RTD_DOCKER_USER}/.asdf/bin"
318-
)
319-
if settings.RTD_DOCKER_COMPOSE:
320-
asdf_paths += ":/root/.asdf/shims:/root/.asdf/bin"
321-
322-
default_paths = "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
323-
environment["PATH"] = f"{asdf_paths}:{default_paths}"
324-
325-
# Prepend the BIN_PATH if it's defined
326-
if self.bin_path:
327-
original_path = environment.get("PATH")
328-
escaped_bin_path = self._escape_command(self.bin_path)
329-
environment["PATH"] = escaped_bin_path
330-
if original_path:
331-
environment["PATH"] = f"{escaped_bin_path}:{original_path}"
332-
333304
try:
334305
exec_cmd = client.exec_create(
335306
container=self.build_env.container_id,
336307
cmd=self.get_wrapped_command(),
337-
environment=environment,
308+
environment=self._environment,
338309
user=self.user,
339310
workdir=self.cwd,
340311
stdout=True,
@@ -386,18 +357,31 @@ def get_wrapped_command(self):
386357
"""
387358
Wrap command in a shell and optionally escape special bash characters.
388359
360+
In order to set the current working path inside a docker container, we
361+
need to wrap the command in a shell call manually.
362+
389363
Some characters will be interpreted as shell characters without
390364
escaping, such as: ``pip install requests<0.8``. When passing
391365
``escape_command=True`` in the init method this escapes a good majority
392366
of those characters.
393367
"""
368+
prefix = ''
369+
if self.bin_path:
370+
bin_path = self._escape_command(self.bin_path)
371+
prefix += f'PATH={bin_path}:$PATH '
372+
394373
command = (
395374
' '.join(
396375
self._escape_command(part) if self.escape_command else part
397376
for part in self.command
398377
)
399378
)
400-
return f"/bin/bash -c '{command}'"
379+
return (
380+
"/bin/sh -c '{prefix}{cmd}'".format(
381+
prefix=prefix,
382+
cmd=command,
383+
)
384+
)
401385

402386
def _escape_command(self, cmd):
403387
r"""Escape the command by prefixing suspicious chars with `\`."""
@@ -540,14 +524,14 @@ class BuildEnvironment(BaseEnvironment):
540524
"""
541525

542526
def __init__(
543-
self,
544-
project=None,
545-
version=None,
546-
build=None,
547-
config=None,
548-
environment=None,
549-
record=True,
550-
**kwargs, # pylint: disable=unused-argument
527+
self,
528+
project=None,
529+
version=None,
530+
build=None,
531+
config=None,
532+
environment=None,
533+
record=True,
534+
**kwargs,
551535
):
552536
super().__init__(project, environment)
553537
self.version = version
@@ -573,7 +557,7 @@ def run(self, *cmd, **kwargs):
573557
})
574558
return super().run(*cmd, **kwargs)
575559

576-
def run_command_class(self, *cmd, **kwargs): # pylint: disable=signature-differs
560+
def run_command_class(self, *cmd, **kwargs): # pylint: disable=arguments-differ
577561
kwargs.update({
578562
'build_env': self,
579563
})

0 commit comments

Comments
 (0)