HiPhish / jinja.vim

Filetype detection an syntax support for Jinja templates.
Other
22 stars 5 forks source link

bug: Does not work with filenames containing % #3

Open chancez opened 1 month ago

chancez commented 1 month ago

I know this project is really old, but I figured I'd file this bug report, just in case anyone else encounters the issue I did.

I'm using https://copier.readthedocs.io/en/stable/ which uses Jinja to template out scaffolding for files in a repository. One feature is that it uses Jinja in the filenames of files to do logic like conditionally create certain files. However this means there's % in the filenames. What I found is that when I'm using this jinja.vim plugin I cannot save files with % in them.

For example with the filename

template/.github/ISSUE_TEMPLATE/{% if create_bug_report_issue_template %}bug_report.yaml{% endif %}.jinja

I get an error like:

<mplate %}bug_report.yaml{% endif %}.jinja} if create_bug_report_issue_template template/.github/ISSUE_TEMPLATE/{template/.github/ISSUE_TEMPLATE/{% if create_bug_report_issue_template %}bug_report.yaml{% endif %}.jinja if create_bug_report_issue_template template/.github/ISSUE_TEMPLATE/{% if create_bug_report_issue_template %}bug_report.yaml{% endif %}.jinja}bug_report.yaml{template/.github/ISSUE_TEMPLATE/{% if "             
E212: Can't open file for writing: name too long                                                                                                                                                                                                                                                                                                                                                                                             
Press ENTER or type command to continue

I think this is because of the usage of the expand() function here: https://github.com/HiPhish/jinja.vim/blob/master/ftdetect/jinja.vim#L25 and the subsequent call to fnamemodify here https://github.com/HiPhish/jinja.vim/blob/master/ftdetect/jinja.vim#L32C27-L32C38. I'm not sure if the expand call is required, or what the correct fix is, but maybe when I have some time I'll try forking and fixing it myself to see if it works for my use-case, but if you have suggestions I'd love to hear it.

Anyways, thanks for your plugin!

HiPhish commented 3 weeks ago

Thanks for reporting the bug. I fixed it, just had to fnameescape the file name first. Try the latest master please.

Here is what the code is trying to achieve:

  1. Strip away the trailing .jinja (or whatever other extension) from the file name (using fnamemodify)
  2. Let Vim detect the file type as it would normally
  3. Append .jinja to the detected file type
  4. Set the file name back to the original value

I don't have any better idea how to tell Vim "try detecting the file type, but pretend that last file extension wasn't there". Using the :file command to change the buffer name causes other problem as well.

chancez commented 3 weeks ago

@HiPhish Nice! Thanks for the update.

The latest master no longer fails in the same way, and the filename reported in the buffer seems to be correct. However, it does seem to still be somewhat buggy. When I try to save, I get the following error instead:

E13: File exists (add ! to override)

That said, this error is relatively easy to work-around at least.

HiPhish commented 3 weeks ago

Yes, that problem is related to my use of :filename, it seems to dissociate the buffer from its file. I hope I can find a workaround.

HiPhish commented 3 weeks ago

@chancez I think I have managed to fix the problem. Can you please report back if anything weird starts happening, like changes being lost or files that should not exist getting created?

chancez commented 3 weeks ago

Well in my small test case, it works as expected. I'll report back if I find any issues. Thanks again for the help. Nothing like finding a bug after 6 years and getting it fixed in no time, your a champ!