You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add !resolve YAML tag for sharing files across multiple projects #3331
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:
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
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.
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 😄
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.
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.
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.
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.
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?
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:
@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 ☺️
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).
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.
!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.
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?
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 🤔
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 🤷
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.
Activity
[-]Add `!resolve` YAML tag for sharing files across multiple projects.[/-][+]Add `!resolve` YAML tag for sharing files across multiple projects[/+]oprypin commentedon Aug 5, 2023
squidfunk commentedon Aug 5, 2023
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.
pawamoy commentedon Aug 5, 2023
MkDocs could only post-process values which are actual
mkdocs.config.config_options.Files orListOfPathsor similar objects. That would force plugin authors to properly declare their config options 😄squidfunk commentedon Aug 5, 2023
We could definitely try that, jup. But I think it wouldn't hurt to add
!resolveas well.squidfunk commentedon Aug 7, 2023
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_dirsetting and create the directory if it does not exist. In any case,!resolveis 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 inconfig.extra_javascript.Should I submit a PR?
lovelydinosaur commentedon Apr 10, 2024
I really like
mkdocs.ymlhaving some constraints here.I'd personally be -1 on a
!resolveYAML tag, since I think it opens up a whole class of issues and unexpected behaviors.My suggestion to users considering this would typically be...
mkdocs.yml.squidfunk commentedon Apr 11, 2024
It would be very useful to allow authors to make specific paths absolute. There's also already
!relativewhich was added by @oprypin in 1.5 (#3258), and!resolvewould be its shadow companion. We can call it!absoluteto 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.
lovelydinosaur commentedon Apr 12, 2024
Mostly it's about design constraints / sensibility.
(No, I'm not wild about either
!resolveorINHERIT.)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.
squidfunk commentedon Apr 12, 2024
I understand. Do you have another idea how to at least fix
INHERITthen? 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 sinceINHERIThas 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.
lovelydinosaur commentedon Apr 12, 2024
🤷🏼♂️... is the Plugin
on_configsufficient to provide that behavior??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.
squidfunk commentedon Apr 12, 2024
Nope, it's too late, as
on_configis invoked as part ofbuildafter configuration resolution:mkdocs/mkdocs/__main__.py
Lines 281 to 286 in 6f5e748
pawamoy commentedon Apr 12, 2024
!resolveonly makes a path absolute, it doesn't even need to exist.Just to make sure we're on the same page: the
!resolvetag 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:
Here, when building
projectdocs, the first item ofhooksshould 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.ymlcd ~/root && mkdocs build -f project/mkdocs.ymlcd ~/root/project && mkdocs buildsquidfunk commentedon Apr 12, 2024
@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 somehowpawamoy commentedon Apr 12, 2024
Thanks for confirming! I don't use INHERIT myself but this seems like a nice fix for the cases mentioned here indeed 🙂
squidfunk commentedon Apr 12, 2024
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).lovelydinosaur commentedon Apr 17, 2024
To clarify... is this strictly only an issue when used with
INHERIT?squidfunk commentedon Apr 17, 2024
I'm not entirely sure, it might also be an issue if you invoke MkDocs from another folder than the
mkdocs.ymlresides in, i.e. with a different CWD, but that needs further testing. Essentially, when you do this:INHERIThowever definitely suffers from this problem when projects are not on the same level.pawamoy commentedon Apr 17, 2024
!relativewas 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!relativetag.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.
lovelydinosaur commentedon Apr 17, 2024
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?
pawamoy commentedon Apr 17, 2024
I'd say that the unexpected issues are relative to inheritance, not the
!relativetag 😄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
!relativetag 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
!resolvetag could come to the rescue. But I wonder if!resolve !relative ...or!relative !resolve ...would work 🤔lovelydinosaur commentedon Apr 17, 2024
Eyeing this up...
pawamoy commentedon Apr 17, 2024
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 🤷pawamoy commentedon Apr 17, 2024
The other route is to move away from
INHERITand implement something more akin to composition, see #2624.squidfunk commentedon Apr 18, 2024
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.
!resolveis not a good fit, feel free to close this, but I don't think we should be stopping the conversation here.!INHERIThas too many problems, but already has a significant fraction of users.Back to the drawing board, I guess!
lovelydinosaur commentedon Apr 22, 2024
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.