diff --git a/CHANGES b/CHANGES index 1f2dcb8684d..3072cb0090d 100644 --- a/CHANGES +++ b/CHANGES @@ -16,6 +16,8 @@ Features added * #9445: autodoc: Support class properties * #9445: py domain: ``:py:property:`` directive supports ``:classmethod:`` option to describe the class property +* #9378: setuptools: Add the build directory to sys.path automatically if + ``build_sphinx`` command is invoked after ``build`` command Bugs fixed ---------- diff --git a/sphinx/setup_command.py b/sphinx/setup_command.py index 1fc55bc157f..a1a464e56a8 100644 --- a/sphinx/setup_command.py +++ b/sphinx/setup_command.py @@ -160,6 +160,8 @@ def run(self) -> None: if self.nitpicky: confoverrides['nitpicky'] = self.nitpicky + self.setup_syspath() + for builder, builder_target_dir in self.builder_target_dirs: app = None @@ -187,3 +189,10 @@ def run(self) -> None: src = app.config.root_doc + app.builder.out_suffix # type: ignore dst = app.builder.get_outfilename('index') # type: ignore os.symlink(src, dst) + + def setup_syspath(self) -> None: + """Set up sys.path to import target modules from the build directory.""" + build = self.distribution.command_obj.get('build') # type: ignore + build_lib = getattr(build, 'build_lib', None) + if build_lib and os.path.exists(build_lib) and build_lib not in sys.path: + sys.path.append(build_lib)