JuliaTeX / TikzPictures.jl

Creating PGF/TikZ pictures and saving them in various formats
Other
89 stars 28 forks source link

Changing global working directory #44

Closed zsunberg closed 5 years ago

zsunberg commented 5 years ago

Sometimes this package has the side effect of changing the current working directory for all of Julia and leaving it changed! :astonished: This can result in very hard-to-diagnose unexpected behavior like files the user wanted to save in her directory ending up in this package's temporary working dir.

Of course it's fine to change directories temporarily to run commands, but it is hard to guarantee that it is changed back correctly in all cases. The two-argument version of cd() should be used to automatically enforce this. :smiley:

mykelk commented 5 years ago

Oh, this is definitely the way to go. @Shushman do you want to take a crack at this?

Shushman commented 5 years ago

Yup, on it

Shushman commented 5 years ago

@zsunberg I'm running into an issue with the f::Function in the two-argument version of cd. If you look at the code between cd(working_dir) here (https://github.com/JuliaTeX/TikzPictures.jl/blob/master/src/TikzPictures.jl#L220) and cd(original_dir) here (https://github.com/JuliaTeX/TikzPictures.jl/blob/master/src/TikzPictures.jl#L220) that is the behavior I need to replicate by the ::Function. But the function I'm writing to achieve the behavior in that block needs the arguments for tp and f etc (say that function is savePDFTPHelper(::PDF,::TikzPicture) and implements the interim code).

How do I call cd then? I can't do cd(savePDFTPHelper,working_dir) as it does not send through the arguments. But I can't do cd(savePDFTPHelper(f,tp),working_dir) either as that is not a ::Function, it evaluates to Nothing as it executes the function which has no return type.

I've done argument binding before in Julia but they were typically when the bound method still had some argument to operate on, and I was binding only a subset of arguments. Not sure what to do here :(

rejuvyesh commented 5 years ago

You can create an anonymous function which just needs the directory argument (IIUC). Also you should be using pwd() for the original directory.

See https://github.com/JuliaLang/julia/blob/master/base/file.jl#L81

Shushman commented 5 years ago

Can you try out the branch in #45 @zsunberg ?