Skip to content

Add !resolve YAML tag for sharing files across multiple projects #3331

Closed as not planned

Description

@squidfunk

Currently, you can't use a relative path in a base mkdocs.yml that you reference with INHERIT in another mkdocs.yml if it's not on the same layer of the file system. By adding a !resolve YAML tag, the path could be resolved to an absolute path before inheriting. This would also make sharing of assets or other files across projects simpler. The implementation is trivial:

import os

from yaml import Loader
from yaml.nodes import ScalarNode

def resolve_constructor(loader: Loader, node: ScalarNode):
  return os.path.abspath(loader.construct_scalar(node))

Loader.add_constructor("!resolve", resolve_constructor)

You could then do:

hooks:
  - !resolve hooks/some-hook.py

I'm open for submitting a PR, but wanted to discuss first.

Activity

  1. changed the title [-]Add `!resolve` YAML tag for sharing files across multiple projects.[/-] [+]Add `!resolve` YAML tag for sharing files across multiple projects[/+] on Aug 5, 2023
  2. oprypin commented on Aug 5, 2023

    @oprypin
  3. squidfunk commented on Aug 5, 2023

    @squidfunk
    ContributorAuthor

    I'm fine with that as well, but the problem is that you'd need to go through all keys and check if it's a path to an actual file, which is cumbersome given that paths can basically be anywhere, in hooks, plugins, extensions etc. Making it explicit is likely the least error prone approach, IMHO.

  4. pawamoy commented on Aug 5, 2023

    @pawamoy
    Contributor

    MkDocs could only post-process values which are actual mkdocs.config.config_options.Files or ListOfPaths or similar objects. That would force plugin authors to properly declare their config options 😄

  5. squidfunk commented on Aug 5, 2023

    @squidfunk
    ContributorAuthor

    We could definitely try that, jup. But I think it wouldn't hurt to add !resolve as well.

  6. squidfunk commented on Aug 7, 2023

    @squidfunk
    ContributorAuthor

    MkDocs could only post-process values which are actual mkdocs.config.config_options.Files or ListOfPaths or similar objects. That would force plugin authors to properly declare their config options 😄

    One case that came to my mind where this does not work: plugins that create files or directories. For example, all of Material for MkDocs's built-in plugins that implement caching define a cache_dir setting and create the directory if it does not exist. In any case, !resolve is trivial but can be pretty useful before we figure out if and how file system config options should be automatically resolved, which might have further side effects on plugins due to the change in behavior, as happened with MkDocs 1.5 and the change in config.extra_javascript.

    Should I submit a PR?

  7. lovelydinosaur commented on Apr 10, 2024

    @lovelydinosaur
    Collaborator

    I really like mkdocs.yml having some constraints here.

    I'd personally be -1 on a !resolve YAML tag, since I think it opens up a whole class of issues and unexpected behaviors.

    My suggestion to users considering this would typically be...

    • Probably just don't do that.
    • If you really do want to, then use some other preprocess tooling to generate the mkdocs.yml.
  8. squidfunk commented on Apr 11, 2024

    @squidfunk
    ContributorAuthor

    It would be very useful to allow authors to make specific paths absolute. There's also already !relative which was added by @oprypin in 1.5 (#3258), and !resolve would be its shadow companion. We can call it !absolute to better reflect that.

    Could you please expand on what you think might be the class of issues and unexpected behaviors you're concerned about? Given that the use of those tags is explicit, and thus opt-in, and they are evaluated prior to any plugins or extensions running any of their events (the main point of contact with the ecosystem), I currently don't see any problems here.

    On another note, it would be very useful to have an extension mechanism to add custom YAML tags. This would be another degree of freedom that would allow plugin and extension authors to add functionality, and MkDocs to do less.

  9. lovelydinosaur commented on Apr 12, 2024

    @lovelydinosaur
    Collaborator

    Mostly it's about design constraints / sensibility.

    (No, I'm not wild about either !resolve or INHERIT.)

    Could you please expand on what you think might be the class of issues and unexpected behaviors

    Unexpected behaviors I might expect probably include things like: "lookout theres a recursive resolution here", "oops I couldn't access this because of file permission issues", "wait this is actually a directory not a file", and "holy heck you've included something too large". There might also be some unexpected behaviors I haven't expected here.

  10. squidfunk commented on Apr 12, 2024

    @squidfunk
    ContributorAuthor

    I understand. Do you have another idea how to at least fix INHERIT then? It is currently broken if both files are not in the same directory and some extension or plugin with relative paths is used. The documentation even uses an example that suffers from the problem reported here. One option could be to at least fail the build if this is the case, but since INHERIT has been out for over a year now, this would, IMHO, mandate a major release + deprecation path.

    Additionally, if we could somehow allow themes/plugin to register custom YAML tags, that'd be a great extension capability.

  11. lovelydinosaur commented on Apr 12, 2024

    @lovelydinosaur
    Collaborator

    Do you have another idea how to at least fix INHERIT then?

    🤷🏼‍♂️... is the Plugin on_config sufficient to provide that behavior??

    mandate a [...] deprecation path.

    Indeed.

    Not saying "we should absolutely do this", but I think it'd present more cleanly if we did, and yes it'd need a nice careful deprecation.

  12. squidfunk commented on Apr 12, 2024

    @squidfunk
    ContributorAuthor

    Nope, it's too late, as on_config is invoked as part of build after configuration resolution:

    mkdocs/mkdocs/__main__.py

    Lines 281 to 286 in 6f5e748

    cfg = config.load_config(**kwargs)
    cfg.plugins.on_startup(command='build', dirty=not clean)
    try:
    build.build(cfg, dirty=not clean)
    finally:
    cfg.plugins.on_shutdown()

  13. pawamoy commented on Apr 12, 2024

    @pawamoy
    Contributor
    • lookout theres a recursive resolution here: I'm not sure to understand this one. Even if we tried to resolve a path multiple times, after the first time it would be a no-op.
    • oops I couldn't access this because of file permission issues: I guess users relying on the current behavior could start seeing "file not found" errors indeed.
    • wait this is actually a directory not a file: Directories should be supported too. !resolve only makes a path absolute, it doesn't even need to exist.
    • holy heck you've included something too large: Too large as in file contents or path length?

    Just to make sure we're on the same page: the !resolve tag only transforms scalars (strings) into their equivalent absolute path. One thing that doesn't seem addressed here however is from what base directory are they made absolute? Does the INHERIT mechanism change the current working directory before loading a file?

    I think we need a better example:

    ~/root/
        base/
            mkdocs.yml
            hooks.py
        project/
            mkdocs.yml
    
    # ~/root/base/mkdocs.yml
    hooks:
      - !resolve hooks.py
    # ~/root/project/mkdocs.yml
    INHERIT: ../../base/mkdocs.yml

    Here, when building project docs, the first item of hooks should point at ~/root/base/hooks.py, not ~/root/project/hooks.py, regardless of what the current working directory is, and regardless of whether the config file path was given with -f:

    • cd ~ && mkdocs build -f root/project/mkdocs.yml
    • cd ~/root && mkdocs build -f project/mkdocs.yml
    • cd ~/root/project && mkdocs build
  14. squidfunk commented on Apr 12, 2024

    @squidfunk
    ContributorAuthor

    @pawamoy thanks for breaking it down. Currently, it's resolved from the current working directory, so yes, it's actually an oversight in my implementation that this is not configurable. The intention was that it is resolved from the folder containing mkdocs.yml, so we would need to find a way to pass this location into the YAML loader. I'm sorry I haven't seen this before – interestingly it worked for all my use cases. I'd still find this functionality useful, if we could make it work somehow ☺️

  15. pawamoy commented on Apr 12, 2024

    @pawamoy
    Contributor

    Thanks for confirming! I don't use INHERIT myself but this seems like a nice fix for the cases mentioned here indeed 🙂

  16. squidfunk commented on Apr 12, 2024

    @squidfunk
    ContributorAuthor

    Great! Can we keep this issue open for consideration? As I suggested, allowing to use custom YAML tags would be another powerful degree of freedom, and if we could somehow provide the root path to a function that would allow to register those YAML tags, we could easily configure the base path in !resolve (and friends).

  17. lovelydinosaur commented on Apr 17, 2024

    @lovelydinosaur
    Collaborator

    To clarify... is this strictly only an issue when used with INHERIT?

    Currently, you can't use a relative path in a base mkdocs.yml that you reference with INHERIT in another mkdocs.yml

  18. squidfunk commented on Apr 17, 2024

    @squidfunk
    ContributorAuthor

    I'm not entirely sure, it might also be an issue if you invoke MkDocs from another folder than the mkdocs.yml resides in, i.e. with a different CWD, but that needs further testing. Essentially, when you do this:

    mkdocs serve --config-file path/to/mkdocs.yml
    

    INHERIT however definitely suffers from this problem when projects are not on the same level.

  19. pawamoy commented on Apr 17, 2024

    @pawamoy
    Contributor

    !relative was added to fix the issue of paths being relative to the CWD, for Markdown extensions settings. Plugins can know where the config file is, so can compute a path relative to it without the need for a custom tag. Markdown extensions don't know anything about MkDocs, so needed something like the !relative tag.

    The situation for a single config file, whatever CWD is, is solved.

    The situation where the config file inherits from other config files is not.
    When inheriting from other config files, these config file paths are lost, and cannot be used to correctly compute paths relative to them.

  20. lovelydinosaur commented on Apr 17, 2024

    @lovelydinosaur
    Collaborator

    The situation where the config file INHERITs from other config files is not.

    So, we've introduced a custom syntax on top of yaml, and there's unexpected issues, and we'd like to introduce another custom syntax on top of that to fix it?

    image
  21. pawamoy commented on Apr 17, 2024

    @pawamoy
    Contributor

    I'd say that the unexpected issues are relative to inheritance, not the !relative tag 😄

    If each setting could have an additional attribute telling the path of the config file it was defined in, plugins could support inherited relative paths (that would still require new API). For Markdown extensions, the !relative tag could be updated to use this new information too.

    If adding such an attribute and providing the corresponding API to retrieve it elegantly proves impossible or hard to implement in an elegant and backward-compatible way, the !resolve tag could come to the rescue. But I wonder if !resolve !relative ... or !relative !resolve ... would work 🤔

  22. lovelydinosaur commented on Apr 17, 2024

    @lovelydinosaur
    Collaborator

    Eyeing this up...

    image
  23. pawamoy commented on Apr 17, 2024

    @pawamoy
    Contributor

    If we could unify the API between plugins and extensions, for example by providing API to define MkDocs interfaces to extensions, then we could reuse the already existing config API (mkdocs.config.config_options.File) to resolve everything correctly. But this is a very big effort 🤷

  24. pawamoy commented on Apr 17, 2024

    @pawamoy
    Contributor

    The other route is to move away from INHERIT and implement something more akin to composition, see #2624.

  25. squidfunk commented on Apr 18, 2024

    @squidfunk
    ContributorAuthor

    I'd also see this issue in the context of a larger effort to unify plugins and extensions, and essentially allow for preprocessing the configuration file before it is loaded. I can say with confidence that this would resonate very well with the target group of MkDocs and Material for MkDocs, as it would be a new degree of freedom.

    I'm happy to discuss this on a call or another thread. If.!resolve is not a good fit, feel free to close this, but I don't think we should be stopping the conversation here. !INHERIT has too many problems, but already has a significant fraction of users.

    Back to the drawing board, I guess!

  26. lovelydinosaur commented on Apr 22, 2024

    @lovelydinosaur
    Collaborator

    The other route is to move away from INHERIT

    Yes. As with most open source projects there's actually been quite a lot of scope creep in mkdocs.

    (Dealing with that from where we are now is a bit awkward, tho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions