Skip to content

Commit 1825f28

Browse files
authored
Merge pull request #9018 from readthedocs/humitos/build-environment-bugfix
Build: use same build environment for setup and build
2 parents 2e3f208 + 0a213fb commit 1825f28

File tree

2 files changed

+67
-62
lines changed

2 files changed

+67
-62
lines changed

readthedocs/doc_builder/director.py

+54-55
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ def setup_vcs(self):
9797
if commit:
9898
self.data.build["commit"] = commit
9999

100+
def create_build_environment(self):
101+
self.build_environment = self.data.environment_class(
102+
project=self.data.project,
103+
version=self.data.version,
104+
config=self.data.config,
105+
build=self.data.build,
106+
environment=self.get_build_env_vars(),
107+
)
108+
100109
def setup_environment(self):
101110
"""
102111
Create the environment and install required dependencies.
@@ -105,59 +114,50 @@ def setup_environment(self):
105114
2. create language (e.g. Python) environment
106115
3. install dependencies into the environment
107116
"""
108-
self.build_environment = self.data.environment_class(
109-
project=self.data.project,
117+
# Environment used for building code, usually with Docker
118+
language_environment_cls = Virtualenv
119+
if any(
120+
[
121+
self.data.config.conda is not None,
122+
self.data.config.python_interpreter in ("conda", "mamba"),
123+
]
124+
):
125+
language_environment_cls = Conda
126+
127+
self.language_environment = language_environment_cls(
110128
version=self.data.version,
129+
build_env=self.build_environment,
111130
config=self.data.config,
112-
build=self.data.build,
113-
environment=self.get_build_env_vars(),
114131
)
115132

116-
# Environment used for building code, usually with Docker
117-
with self.build_environment:
118-
language_environment_cls = Virtualenv
119-
if any(
120-
[
121-
self.data.config.conda is not None,
122-
self.data.config.python_interpreter in ("conda", "mamba"),
123-
]
124-
):
125-
language_environment_cls = Conda
126-
127-
self.language_environment = language_environment_cls(
128-
version=self.data.version,
129-
build_env=self.build_environment,
130-
config=self.data.config,
131-
)
132-
133-
# TODO: check if `before_build` and `after_build` are still useful
134-
# (maybe in commercial?)
135-
#
136-
# I didn't find they are used anywhere, we should probably remove them
137-
before_build.send(
138-
sender=self.data.version,
139-
environment=self.build_environment,
140-
)
133+
# TODO: check if `before_build` and `after_build` are still useful
134+
# (maybe in commercial?)
135+
#
136+
# I didn't find they are used anywhere, we should probably remove them
137+
before_build.send(
138+
sender=self.data.version,
139+
environment=self.build_environment,
140+
)
141141

142-
self.pre_system_dependencies()
143-
self.system_dependencies()
144-
self.post_system_dependencies()
142+
self.pre_system_dependencies()
143+
self.system_dependencies()
144+
self.post_system_dependencies()
145145

146-
# Install all ``build.tools`` specified by the user
147-
if self.data.config.using_build_tools:
148-
self.language_environment.install_build_tools()
146+
# Install all ``build.tools`` specified by the user
147+
if self.data.config.using_build_tools:
148+
self.language_environment.install_build_tools()
149149

150-
self.pre_create_environment()
151-
self.create_environment()
152-
self.post_create_environment()
150+
self.pre_create_environment()
151+
self.create_environment()
152+
self.post_create_environment()
153153

154-
self.pre_install()
155-
self.install()
156-
self.post_install()
154+
self.pre_install()
155+
self.install()
156+
self.post_install()
157157

158-
# TODO: remove this and document how to do it on `build.jobs.post_install`
159-
if self.data.project.has_feature(Feature.LIST_PACKAGES_INSTALLED_ENV):
160-
self.language_environment.list_packages_installed()
158+
# TODO: remove this and document how to do it on `build.jobs.post_install`
159+
if self.data.project.has_feature(Feature.LIST_PACKAGES_INSTALLED_ENV):
160+
self.language_environment.list_packages_installed()
161161

162162
def build(self):
163163
"""
@@ -168,17 +168,16 @@ def build(self):
168168
3. build PDF
169169
4. build ePub
170170
"""
171-
with self.build_environment:
172-
self.data.outcomes = defaultdict(lambda: False)
173-
self.data.outcomes["html"] = self.build_html()
174-
self.data.outcomes["search"] = self.is_type_sphinx()
175-
self.data.outcomes["localmedia"] = self.build_htmlzip()
176-
self.data.outcomes["pdf"] = self.build_pdf()
177-
self.data.outcomes["epub"] = self.build_epub()
178-
179-
after_build.send(
180-
sender=self.data.version,
181-
)
171+
self.data.outcomes = defaultdict(lambda: False)
172+
self.data.outcomes["html"] = self.build_html()
173+
self.data.outcomes["search"] = self.is_type_sphinx()
174+
self.data.outcomes["localmedia"] = self.build_htmlzip()
175+
self.data.outcomes["pdf"] = self.build_pdf()
176+
self.data.outcomes["epub"] = self.build_epub()
177+
178+
after_build.send(
179+
sender=self.data.version,
180+
)
182181

183182
# VCS checkout
184183
def pre_checkout(self):

readthedocs/projects/tasks/builds.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -548,13 +548,19 @@ def execute(self):
548548
# objects in the database
549549
self.sync_versions(self.data.build_director.vcs_repository)
550550

551-
# Installing
552-
self.update_build(state=BUILD_STATE_INSTALLING)
553-
self.data.build_director.setup_environment()
554-
555-
# Building
556-
self.update_build(state=BUILD_STATE_BUILDING)
557-
self.data.build_director.build()
551+
# TODO: remove the ``create_build_environment`` hack. Ideally, this should be
552+
# handled inside the ``BuildDirector`` but we can't use ``with
553+
# self.build_environment`` twice because it kills the container on
554+
# ``__exit__``
555+
self.data.build_director.create_build_environment()
556+
with self.data.build_director.build_environment:
557+
# Installing
558+
self.update_build(state=BUILD_STATE_INSTALLING)
559+
self.data.build_director.setup_environment()
560+
561+
# Building
562+
self.update_build(state=BUILD_STATE_BUILDING)
563+
self.data.build_director.build()
558564

559565
@staticmethod
560566
def get_project(project_pk):

0 commit comments

Comments
 (0)