Methods to traverse the "virtual filesystem"

I realized that the file functions in GoHugo traverse only the local filesystem, not the mounted folders/files. For example, os.ReadDir shows only the files in the root repo, not the mounted module directories.

I tried to use readdir on “known” paths that are mounted from modules and got error messages about not found paths, so that doesn’t work either.

Is there any way to “access” the virtual filesystem within the layout files WITHOUT knowing of their existence?

By the way, I tried to save the output of hugo config mounts in a try to attempt listing from there as data files, but first of all, it’s not proper JSON (there is a missing comma between each imported module, the rest looks like valid JSON). But even those paths, if they are a directory, can’t be traversed via os.ReadDir.

Long story short: Is there any way to know about the files and directories that are available from within the layout files?

One hacky way would be via templates.Exists, but for that I would need to know what files to expect and check for.

I ran across this a couple of days ago:

The comment author states that the os.Something functions are inconsistent with respect to file system, some using the OS file system, while others use the union file system. It seems like your issue would be resolved if they all used the union file system.

It looks like the union file system, in this case, only includes the content directory.

1 Like

Looking a previous topics, issues, and pull requests related to the the os.Something functions, it looks a bit like a third rail that we wouldn’t want to touch. I wonder if a new function namespace for the
union file system would make sense, limited to:

  • ufs.ReadDir
  • ufs.FileExists

Why limit to those?

  • I’ve never found Stat to be useful
  • I can’t think of a reason to ReadFile within the union file system; we already have the .Content method on Page and Resource objects. If you need to read the content of a file in static, mount static to assets.


1 Like

I would love those two methods. For one thing I feel like module developers need to somehow have a way to see if their layout files are being loaded (between two additional modules for instance) and those functions would help immensely. ReadFile wouldn’t bring anything to the table, that is not already there from the page objects (and we know what formats, variables to expect there), but having an overview of the virtual filesystem with ReadDir and FileExist makes sense.

Thank you for looking into this!


1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.