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

Support custom directories to watch when running mkdocs serve #2642

Merged
merged 18 commits into from Nov 7, 2021

Conversation

steven-terrana
Copy link
Contributor

Implements #2632

  • adds a watch property to the mkdocs.yaml schema. Accepts a list of directories to watch.
  • adds a -w/--watch command line option to mkdocs serve that can be passed multiple times
  • options from mkdocs.yaml and CLI flags are combined
  • docs updated

Copy link
Contributor

@oprypin oprypin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Seems good overall.

mkdocs/commands/serve.py Outdated Show resolved Hide resolved
mkdocs/commands/serve.py Outdated Show resolved Hide resolved
mkdocs/commands/serve.py Outdated Show resolved Hide resolved
@oprypin oprypin added this to the 1.3.0 milestone Oct 26, 2021
mkdocs/__main__.py Outdated Show resolved Hide resolved
@@ -120,4 +120,7 @@ def get_schema():
# A key value pair should be the string name (as the key) and a dict of config
# options (as the value).
('plugins', config_options.Plugins(default=['search'])),

# a list of extra directories to watch while running `mkdocs serve`
('watch', config_options.Type(list, default=[]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the path will have to be relativized to the location of mkdocs.yml, as here:

if self.config_dir and not os.path.isabs(value):
value = os.path.join(self.config_dir, value)

Well, I think that's what would be the most logical as per what I've seen from MkDocs so far.
Feel free to argue otherwise.


This would probably require a new type in config_optons.py. ListOfPaths or something.
During validation it could reuse FilesystemObject.

I would recommend turning it non-abstract:

 class FilesystemObject(Type):
+    existence_test = staticmethod(os.path.exists)
+    name = 'path'

Relevant implementation example:

class Nav(OptionallyRequired):


Note that we probably should not check for existence of the path (exists=False should be kept). Not sure though.

Copy link
Contributor

@oprypin oprypin Oct 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: we should, in fact, check for existence (just set exists=True), otherwise it throws an OSError anyway after startup

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify why this is important:

watch:
    - foo

mkdocs serve --config-file=subdir/mkdocs.yml -w bar

I want this to watch the foo that is adjacent to mkdocs.yml.
The difference in the output being:

-Watching paths for changes: 'subdir/docs', 'subdir/mkdocs.yml', 'foo', 'bar'
+Watching paths for changes: 'subdir/docs', 'subdir/mkdocs.yml', 'subdir/foo', 'bar'

(-w flag not relativized intentionally)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaving this to you btw.

And some tests for the new config option would be nice :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

root@08e90cd25d84:/# mkdocs serve --config-file=mkdocs/mkdocs.yml -w home
INFO     -  Building documentation...
INFO     -  Cleaning site directory
INFO     -  Documentation built in 1.09 seconds
INFO     -  [01:17:16] Watching paths for changes: 'mkdocs/docs', 'mkdocs/mkdocs.yml', 'mkdocs/foo', 'home'

seems to be working :).

agree this needs some tests. i'll have that soon. might take a day or two to carve out some more time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unit tests for ListOfPaths added

Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll argue in favor of this 🙂
As a data point, see this mkdocstrings issue.
That ListOfPaths type could be very useful to resolve it!

mkdocs/__main__.py Outdated Show resolved Hide resolved
oprypin and others added 5 commits October 28, 2021 00:21
Report watched directories in one line, move code to livereload
Co-authored-by: Oleh Prypin <oleh@pryp.in>
Co-authored-by: Oleh Prypin <oleh@pryp.in>
mkdocs/commands/serve.py Outdated Show resolved Hide resolved
@pawamoy
Copy link
Sponsor Contributor

pawamoy commented Oct 29, 2021

I'm not sure if/where it is stated in the docs that paths in the config file are relative to the config file itself. Maybe this could be added again in the docs for the watch option, and/or in the changelog, to make it clear?

Copy link
Contributor

@oprypin oprypin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

mkdocs/config/config_options.py Outdated Show resolved Hide resolved
mkdocs/tests/config/config_options_tests.py Outdated Show resolved Hide resolved
mkdocs/tests/config/config_options_tests.py Outdated Show resolved Hide resolved
steven-terrana and others added 6 commits October 29, 2021 08:42
Co-authored-by: Oleh Prypin <oleh@pryp.in>
Co-authored-by: Oleh Prypin <oleh@pryp.in>
Co-authored-by: Oleh Prypin <oleh@pryp.in>
Co-authored-by: Oleh Prypin <oleh@pryp.in>
@steven-terrana
Copy link
Contributor Author

I'm not sure if/where it is stated in the docs that paths in the config file are relative to the config file itself. Maybe this could be added again in the docs for the watch option, and/or in the changelog, to make it clear?

nice catch @pawamoy. I've updated the docs with a note admonition calling this out.

@oprypin oprypin merged commit 82fcf31 into mkdocs:master Nov 7, 2021
@oprypin
Copy link
Contributor

oprypin commented Nov 7, 2021

Thank you!

@oprypin oprypin linked an issue Nov 16, 2021 that may be closed by this pull request

The paths provided via the configuration file are relative to the configuration file.

The paths provided via the `-w`/`--watch` CLI parameters are not.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bit late to the party, but what exactly would it then be? What would it's "position" be based of? Where the command is executed? That should've been mentioned here IMO, so maybe in a future PR/Commit could this be changed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support custom directories to watch when running mkdocs serve
4 participants