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

Add more file functions to stdlib #422

Open
nikolay opened this issue Dec 14, 2017 · 8 comments
Open

Add more file functions to stdlib #422

nikolay opened this issue Dec 14, 2017 · 8 comments

Comments

@nikolay
Copy link

nikolay commented Dec 14, 2017

So far, the only function for file paths is std.thisFile, but its utility is questionable.

We need functions giving us the full pathname of the current file, just the filename, and just the directory. It would be even better if we can get a function that extracts the filename, and the directory of a given file path, too.

I've been trying to use importstr and include configuration files, but I cannot make it work when the containing file is symlinked and it uses relative to its configuration files. Of course, the same applies to import, too.

@nikolay nikolay changed the title Add more file functions Add more file functions to stdlib Dec 14, 2017
@sbarzowski
Copy link
Collaborator

I think we could add operations on paths. That sound pretty useful. It doesn't require anything special from the language, so it could be a separate library as well.

Functions like:

unixDirname(path)
unixBasename(path)
unixJoinPaths(path_array)
unixExtension(path)
.
.
.

I've been trying to use importstr and include configuration files, but I cannot make it work when the containing file is symlinked and it uses relative to its configuration files. Of course, the same applies to import, too.

I'm not sure I understand how it relates to the previous point. Could you describe what you want to do in more detail? Am example of how you think the new API may look like in the context of your problem would also be useful.

@nikolay
Copy link
Author

nikolay commented Dec 15, 2017

@sbarzowski There are uses cases where I need to imported file to be relative to the symlink's target and in others - to the symlink's source. On top, the documentation does not state clearly if the import is relative to the importing file's source or target if it's a symlink. If not a symlink, I'm still unsure if the imported file path is relative, to what it is relative to then.

@sbarzowski
Copy link
Collaborator

@nikolay

the documentation does not state clearly if the import is relative to the importing file's source or target if it's a symlink. If not a symlink, I'm still unsure if the imported file path is relative, to what it is relative to then.

Good point, thanks for catching this. We should definitely have it in the docs.

It's relative to the current file or to any of the library paths (specified with -J commandline option or JPATH env variable). Not sure about the symlinks, though. I'm gonna try that later.

@sparkprime
Copy link
Contributor

Agreed this should be documented better. At the moment it's defined by implementation - the filename is the one used to open the file, and the "current working directory", i.e. the string used to absolutize relative paths in imports in that file, is just the directory removed from the filename.

The stdlib is weak even regarding basic string tasks like finding a substring. Definitely there is work to do there.

@sparkprime
Copy link
Contributor

Other than the functions sbarzowski listed, and documentation of the meaning of the current directory when importing a symlink, is there any other action item here?

@seh
Copy link

seh commented Nov 15, 2018

It would be nice to have function that could resolve the "real path" of a file, or canonicalize it.

Today I noticed that std.thisFile can fail to include the containing directory name, when it has several ".." path components littered throughout. My locally crafted dirname function mistakenly "works" by yielding a directory path ending in "/..," instead of the intended containing directory name.

This strange import path is the result of arriving at this file by way of the interaction of the jsonnet_library and jsonnet_to_json Bazel rules.

@bcatubig
Copy link

It would be nice to have a glob function that could find files based on the glob match and return them in a list.

@tristan957
Copy link

It would be cool if there was a function to determine of a file existed in order to support optional importstr.

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

No branches or pull requests

6 participants