diff --git a/mkdocs/config/config_options.py b/mkdocs/config/config_options.py index 434dfd6ae3..115be24b01 100644 --- a/mkdocs/config/config_options.py +++ b/mkdocs/config/config_options.py @@ -734,14 +734,6 @@ def post_validation(self, config: Config, key_name: str): if not config.config_file_path: return - # Validate that the dir is not the parent dir of the config file. - if os.path.dirname(config.config_file_path) == config[key_name]: - raise ValidationError( - f"The '{key_name}' should not be the parent directory of the" - f" config file. Use a child directory instead so that the" - f" '{key_name}' is a sibling of the config file." - ) - class File(FilesystemObject): """ @@ -800,13 +792,6 @@ def post_validation(self, config: Config, key_name: str): f"it will be deleted if --clean is passed to mkdocs build. " f"(site_dir: '{site_dir}', docs_dir: '{docs_dir}')" ) - elif (site_dir + os.sep).startswith(docs_dir.rstrip(os.sep) + os.sep): - raise ValidationError( - f"The 'site_dir' should not be within the 'docs_dir' as this " - f"leads to the build directory being copied into itself and " - f"duplicate nested files in the 'site_dir'. " - f"(site_dir: '{site_dir}', docs_dir: '{docs_dir}')" - ) class Theme(BaseConfigOption[theme.Theme]): diff --git a/mkdocs/structure/files.py b/mkdocs/structure/files.py index b1c4df5aba..9990e7d048 100644 --- a/mkdocs/structure/files.py +++ b/mkdocs/structure/files.py @@ -343,6 +343,11 @@ def get_files(config: MkDocsConfig) -> Files: files: list[File] = [] conflicting_files: list[tuple[File, File]] = [] for source_dir, dirnames, filenames in os.walk(config['docs_dir'], followlinks=True): + # Skip processing any files in the site_dir + if os.path.abspath(source_dir) == os.path.abspath(config['site_dir']): + log.info(f"Excluding files in site_dir '{config['site_dir']}'.") + continue + relative_dir = os.path.relpath(source_dir, config['docs_dir']) dirnames.sort() filenames.sort(key=_file_sort_key) diff --git a/mkdocs/tests/config/config_options_legacy_tests.py b/mkdocs/tests/config/config_options_legacy_tests.py index 8d6da457bb..8bd2b189f5 100644 --- a/mkdocs/tests/config/config_options_legacy_tests.py +++ b/mkdocs/tests/config/config_options_legacy_tests.py @@ -768,20 +768,6 @@ class Schema: ) self.assertEqual(conf['dir'], os.path.join(base_path, 'foo')) - def test_site_dir_is_config_dir_fails(self): - class Schema: - dir = c.DocsDir() - - with self.expect_error( - dir="The 'dir' should not be the parent directory of the config file. " - "Use a child directory instead so that the 'dir' is a sibling of the config file." - ): - self.get_config( - Schema, - {'dir': '.'}, - config_file_path=os.path.join(os.path.abspath('.'), 'mkdocs.yml'), - ) - class ListOfPathsTest(TestCase): def test_valid_path(self): @@ -882,23 +868,6 @@ def test_doc_dir_in_site_dir(self): ): self.get_config(self.Schema, test_config) - def test_site_dir_in_docs_dir(self): - j = os.path.join - - test_configs = ( - {'docs_dir': 'docs', 'site_dir': j('docs', 'site')}, - {'docs_dir': '.', 'site_dir': 'site'}, - {'docs_dir': '', 'site_dir': 'site'}, - {'docs_dir': '/', 'site_dir': 'site'}, - ) - - for test_config in test_configs: - with self.subTest(test_config): - with self.expect_error( - site_dir=re.compile(r"The 'site_dir' should not be within the 'docs_dir'.*") - ): - self.get_config(self.Schema, test_config) - def test_common_prefix(self): """Legitimate settings with common prefixes should not fail validation.""" test_configs = ( diff --git a/mkdocs/tests/config/config_options_tests.py b/mkdocs/tests/config/config_options_tests.py index 2c8a33ae88..60e98ec698 100644 --- a/mkdocs/tests/config/config_options_tests.py +++ b/mkdocs/tests/config/config_options_tests.py @@ -972,20 +972,6 @@ class Schema(Config): ) self.assertEqual(conf.dir, os.path.join(base_path, 'foo')) - def test_site_dir_is_config_dir_fails(self) -> None: - class Schema(Config): - dir = c.DocsDir() - - with self.expect_error( - dir="The 'dir' should not be the parent directory of the config file. " - "Use a child directory instead so that the 'dir' is a sibling of the config file." - ): - self.get_config( - Schema, - {'dir': '.'}, - config_file_path=os.path.join(os.path.abspath('.'), 'mkdocs.yml'), - ) - class ListOfPathsTest(TestCase): def test_valid_path(self) -> None: @@ -1095,23 +1081,6 @@ def test_doc_dir_in_site_dir(self) -> None: ): self.get_config(self.Schema, test_config) - def test_site_dir_in_docs_dir(self) -> None: - j = os.path.join - - test_configs = ( - {'docs_dir': 'docs', 'site_dir': j('docs', 'site')}, - {'docs_dir': '.', 'site_dir': 'site'}, - {'docs_dir': '', 'site_dir': 'site'}, - {'docs_dir': '/', 'site_dir': 'site'}, - ) - - for test_config in test_configs: - with self.subTest(test_config): - with self.expect_error( - site_dir=re.compile(r"The 'site_dir' should not be within the 'docs_dir'.*") - ): - self.get_config(self.Schema, test_config) - def test_common_prefix(self) -> None: """Legitimate settings with common prefixes should not fail validation.""" test_configs = ( diff --git a/mkdocs/tests/integration/root_docs/README.md b/mkdocs/tests/integration/root_docs/README.md new file mode 100644 index 0000000000..0e680e6857 --- /dev/null +++ b/mkdocs/tests/integration/root_docs/README.md @@ -0,0 +1,17 @@ +# Welcome to MkDocs + +For full documentation visit [mkdocs.org](https://www.mkdocs.org). + +## Commands + +* `mkdocs new [dir-name]` - Create a new project. +* `mkdocs serve` - Start the live-reloading docs server. +* `mkdocs build` - Build the documentation site. +* `mkdocs help` - Print this help message. + +## Project layout + + mkdocs.yml # The configuration file. + docs/ + index.md # The documentation homepage. + ... # Other markdown pages, images and other files. diff --git a/mkdocs/tests/integration/root_docs/mkdocs.yml b/mkdocs/tests/integration/root_docs/mkdocs.yml new file mode 100644 index 0000000000..6d1db3080e --- /dev/null +++ b/mkdocs/tests/integration/root_docs/mkdocs.yml @@ -0,0 +1,8 @@ +site_name: MyTest +docs_dir: . + +nav: + - 'README.md' + - 'testing.md' + +site_author: "Allison Thackston" diff --git a/mkdocs/tests/integration/root_docs/testing.md b/mkdocs/tests/integration/root_docs/testing.md new file mode 100644 index 0000000000..26075f1ca2 --- /dev/null +++ b/mkdocs/tests/integration/root_docs/testing.md @@ -0,0 +1 @@ +Page content. diff --git a/mkdocs/tests/structure/file_tests.py b/mkdocs/tests/structure/file_tests.py index 1c301cb564..e1bd16ba6c 100644 --- a/mkdocs/tests/structure/file_tests.py +++ b/mkdocs/tests/structure/file_tests.py @@ -672,6 +672,18 @@ def test_get_files_exclude_readme_with_index(self, tdir): self.assertIsInstance(files, Files) self.assertEqual([f.src_uri for f in files], ['index.md', 'foo.md']) + @tempdir( + files=[ + 'index.md', + 'site/foo.md', + ] + ) + def test_get_files_exclude_site_dir(self, tdir): + config = load_config(docs_dir=tdir, site_dir=os.path.join(tdir, "site")) + files = get_files(config) + self.assertIsInstance(files, Files) + self.assertEqual([f.src_uri for f in files], ['index.md']) + @tempdir() @tempdir(files={'test.txt': 'source content'}) def test_copy_file(self, src_dir, dest_dir):