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

A trace function #130

Closed
copumpkin opened this issue Mar 15, 2016 · 7 comments
Closed

A trace function #130

copumpkin opened this issue Mar 15, 2016 · 7 comments

Comments

@copumpkin
Copy link

There might well be one already, but I looked under the "Assertions and Debugging" section of the stdlib docs and only saw assertions.

Basically, I want a function that is equivalent to

local const(str, x) = x;

But that prints str to stderr as a side effect when forced. Very handy for debugging odd execution flow and the like.

Both Haskell and Nix call this function trace, but I'm open to other ideas.

@sparkprime
Copy link
Member

Yes, this is a great idea and something I've had in the back of my mind for a while. You can sort of debug with error right now -- error foo will print out foo, but will also block execution.

One concern is how many implementation hacks this would expose. For example, at the moment, object-level locals are re-evaluated whenever you read a field of the object (as they are desugared into every field). They are not cached like regular thunks. This will give surprising output! However it is probably still useful.

@copumpkin
Copy link
Author

Hah, didn't realize that. Yeah, I don't think anyone will be expecting particularly sensible properties around the output, other than it somehow representing what the VM is doing.

@sparkprime
Copy link
Member

It is trivial to add a new builtin that prints str and returns x. I'm not saying that would be the best way to provide this feature, but it would give you something to play with if you wanted to do that.

@copumpkin
Copy link
Author

I've been meaning to poke around the code for a while. I'll see if I can scrounge together some time one of these days and hack at it, if you don't get sick of waiting for me before that 😄

@sparkprime
Copy link
Member

To save some time -- you need to add another builtin to jsonnet_builtin_decl in desugarer.cpp then modify vm.cpp to add the corresponding case to the inner interpreter loop

                 case FRAME_BUILTIN_FORCE_THUNKS: {

and I apologize for the hairiness of that code :)

@sbarzowski
Copy link
Collaborator

sbarzowski commented Apr 10, 2018

https://stackoverflow.com/questions/49742536/how-to-printf-in-jsonnet/49760216#49760216
The need for this keeps coming back. it's pretty clear people want to debug jsonnet sometimes :-).

@ahakanbaba
Copy link
Contributor

I will push a PR soon.

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

4 participants