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

callers std.thisFile #436

Closed
ghostsquad opened this issue Jul 16, 2020 · 7 comments
Closed

callers std.thisFile #436

ghostsquad opened this issue Jul 16, 2020 · 7 comments

Comments

@ghostsquad
Copy link
Contributor

I'm working on a library that would greatly benefit from getting the filename of the caller of the function in which std.theFile is evaluated. Example:

test.jsonnet

local l = import "./lib.libsonnet";

file = l.file()

lib.libsonnet

{
   file()::
      // we want the _callers_ file here, not my own
      std.thisFile
}

I know this would likely be hard because of the lazy evaluation of jsonnet, but maybe still possible?? no idea.

@sbarzowski
Copy link
Collaborator

This is not a problem with lazy evaluation, it's a problem with referential transparency, purity and avoiding any implicit magic. The std.thisFile is basically a "variable" defined in each file separately. The way it works is similar to having local std = rest_of_std + { thisFile: "/path/to/the/file" } at the beginning on each file.

Do you plan to use it for providing some debugging info? Can you provide more details about the use case?

@ghostsquad
Copy link
Contributor Author

this comes from here: jsonnet-libs/docsonnet#5

and problems that docsonnet is trying to work around:

https://github.com/sh0rez/docsonnet?ts=4#whats-wrong-with-comments-why-not-parse-regular-comments


Being able to create a link back to the source code in the generated documentation is the goal, while also making it easy on the user to write documentation

@sbarzowski
Copy link
Collaborator

sbarzowski commented Jul 16, 2020

I see. There is no way to implicitly depend on the caller's environment and this is very much by design. Eliminating this sort of magic from the configs is pretty much the point. Some limited, carefully designe mechanism for docs and debugging is not out of question, though.

Fortunately, I think we can make it much better with existing features. We can simply pass the filename once per file, instead of passing it as an argument to every single function.

For example:

local d = (import 'docsonnet.libsonnet')(std.thisFile);

{
  '#foo': d.fn(...),
  'foo': ...
}

or

local d = (import 'docsonnet.libsonnet').forFile(std.thisFile);
{
  '#foo': d.fn(...),
  'foo': ...
}

This will require wrapping docsonnet stuff in a function which takes the filename, but then the user needs to pass it only once per file.

Alternatively OOP-style can be used to the same effect:

local d = (import 'docsonnet.libsonnet') + { filename: std.thisFile };

{
  '#foo': d.fn(...),
  'foo': ...
}

What do you think?

@sh0rez
Copy link
Contributor

sh0rez commented Jul 16, 2020

Loving this! Especially the last example makes it super easy to do this backwards compatible, no need to change existing imports.

Also given we might introduce a desugared syntax for docsonnet soon, this would be hidden entirely from the user.

@ghostsquad
Copy link
Contributor Author

Ya I was thinking about once per file for sure. Not a bad burden really, since you already need to copy/paste the import boilerplate anyway

@ghostsquad
Copy link
Contributor Author

I'm satisfied that this issue should be closed

@sbarzowski
Copy link
Collaborator

Good, thanks!

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

No branches or pull requests

3 participants