Skip to content

No error checking in haml:erb2haml task #193

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

Open
dirschn opened this issue Sep 23, 2024 · 18 comments · May be fixed by #195
Open

No error checking in haml:erb2haml task #193

dirschn opened this issue Sep 23, 2024 · 18 comments · May be fixed by #195

Comments

@dirschn
Copy link

dirschn commented Sep 23, 2024

I ran the haml:erb2haml task after running gem install html2haml, and apparently html2haml didn't install properly. The task reported that it couldn't find it for each file, yet at the end it acted as though the task completed successfully and asked whether I wanted to delete the original files. I said yes, because this was an unimportant brand new project, and after the task was done all of my views were erased because none of the ones it said were generated actually were generated.

It seems like there could be some checking in the script to at least verify the expected files exist before reporting a success? I'd be happy to take a crack at it, but I'm a little surprised no one else has reported this yet.

rails haml:erb2haml 
--------------------------------------------------------------------------------
Generating HAML for app/views/default/pages/homepage.html.erb...
/home/nick/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.5.11/lib/bundler/rubygems_integration.rb:265:in `block in replace_bin_path': can't find executable html2haml for gem html2haml. html2haml is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception)
        from /home/nick/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.5.11/lib/bundler/rubygems_integration.rb:293:in `block in replace_bin_path'
        from /home/nick/.rbenv/versions/3.2.2/bin/html2haml:25:in `<main>'
Generating HAML for app/views/default/pages/show.html.erb...
/home/nick/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.5.11/lib/bundler/rubygems_integration.rb:265:in `block in replace_bin_path': can't find executable html2haml for gem html2haml. html2haml is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception)
        from /home/nick/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.5.11/lib/bundler/rubygems_integration.rb:293:in `block in replace_bin_path'
        from /home/nick/.rbenv/versions/3.2.2/bin/html2haml:25:in `<main>'
Generating HAML for app/views/default/shared/_navigation.html.erb...
/home/nick/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.5.11/lib/bundler/rubygems_integration.rb:265:in `block in replace_bin_path': can't find executable html2haml for gem html2haml. html2haml is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception)
        from /home/nick/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.5.11/lib/bundler/rubygems_integration.rb:293:in `block in replace_bin_path'
        from /home/nick/.rbenv/versions/3.2.2/bin/html2haml:25:in `<main>'
Generating HAML for app/views/layouts/application.html.erb...
/home/nick/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.5.11/lib/bundler/rubygems_integration.rb:265:in `block in replace_bin_path': can't find executable html2haml for gem html2haml. html2haml is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception)
        from /home/nick/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.5.11/lib/bundler/rubygems_integration.rb:293:in `block in replace_bin_path'
        from /home/nick/.rbenv/versions/3.2.2/bin/html2haml:25:in `<main>'
Generating HAML for app/views/layouts/default/application.html.erb...
/home/nick/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.5.11/lib/bundler/rubygems_integration.rb:265:in `block in replace_bin_path': can't find executable html2haml for gem html2haml. html2haml is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception)
        from /home/nick/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.5.11/lib/bundler/rubygems_integration.rb:293:in `block in replace_bin_path'
        from /home/nick/.rbenv/versions/3.2.2/bin/html2haml:25:in `<main>'
Generating HAML for app/views/layouts/mailer.html.erb...
/home/nick/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.5.11/lib/bundler/rubygems_integration.rb:265:in `block in replace_bin_path': can't find executable html2haml for gem html2haml. html2haml is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception)
        from /home/nick/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.5.11/lib/bundler/rubygems_integration.rb:293:in `block in replace_bin_path'
        from /home/nick/.rbenv/versions/3.2.2/bin/html2haml:25:in `<main>'
Generating HAML for app/views/layouts/mailer.text.erb...
/home/nick/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.5.11/lib/bundler/rubygems_integration.rb:265:in `block in replace_bin_path': can't find executable html2haml for gem html2haml. html2haml is not currently included in the bundle, perhaps you meant to add it to your Gemfile? (Gem::Exception)
        from /home/nick/.rbenv/versions/3.2.2/lib/ruby/gems/3.2.0/gems/bundler-2.5.11/lib/bundler/rubygems_integration.rb:293:in `block in replace_bin_path'
        from /home/nick/.rbenv/versions/3.2.2/bin/html2haml:25:in `<main>'
--------------------------------------------------------------------------------
HAML generated for the following files:
        app/views/default/pages/homepage.html.erb
        app/views/default/pages/show.html.erb
        app/views/default/shared/_navigation.html.erb
        app/views/layouts/application.html.erb
        app/views/layouts/default/application.html.erb
        app/views/layouts/mailer.html.erb
        app/views/layouts/mailer.text.erb
--------------------------------------------------------------------------------
Would you like to delete the original .erb files? (This is not recommended unless you are under version control.) (y/n)
y
Deleting original .erb files.
--------------------------------------------------------------------------------
Task complete!
@chmich
Copy link

chmich commented Apr 29, 2025

Same here: no .haml file was created and after deleting the original files all views are gone (i done this on a new test project, a real project would be destroyed by that function!)

@dirschn
Copy link
Author

dirschn commented Apr 29, 2025

Honestly I forgot about this. I'll look into it and make a PR sometime this week

@dirschn dirschn linked a pull request Apr 29, 2025 that will close this issue
@dirschn
Copy link
Author

dirschn commented Apr 29, 2025

@chmich I had a stroke of inspiration, check out #195 and let me know if it works for your case.

@chmich
Copy link

chmich commented Apr 30, 2025

@dirschn Thanks, sounds good!

How about the idea moving all original files into a backup-folder (e.g. «view_backups_20250430»)? Then you may not need to ask if theese files should be destroyed.

I will write a version for that for my gem (svelte-on-rails). for that i copied out your rake task (erb2haml) and will add this improvements. I will leave you a copy of that when it ist tested, i think this week.

@dirschn
Copy link
Author

dirschn commented Apr 30, 2025

@chmich Quick disclaimer, I'm not a maintainer here or anything, just a random schmuck who noticed an issue and wanted to fix it. That said, I thought about your suggestion and I keep coming back to thinking it's unnecessary. That kind of thing can be solved by either using version control or choosing not to delete and backing them up yourself.

Again, I'm not a maintainer and if one of those fine folks likes the idea I'd be happy to implement it here!

@chmich
Copy link

chmich commented Apr 30, 2025

@dirschn I agree with you that a backup folder is unnecessary because of VCS. But for thinking of a 100% safe process I prefer a way that is independent and safe. And on the other hand, if all views are backed up in one folder, it is one click to delete that folder. So I decided to go this way.

So, my task is to find on svelte_on_rails:install_haml_vite, and there the part Install::Haml

@dirschn
Copy link
Author

dirschn commented Apr 30, 2025

@chmich Yeah I can definitely appreciate that line of thinking, for sure. I'll hold off until the maintainers render a decision, but I'd be happy to port that over here as well if they think it's worth the effort!

@chmich
Copy link

chmich commented Apr 30, 2025

And, as i saw, you wrote for a better error control of the converting process. On my script i wrapped the execution of html2haml within Open3.capture. this gives much better control. And Open3 is included on Rails (not in plain ruby!)

@dirschn
Copy link
Author

dirschn commented Apr 30, 2025

Oh, very cool! I haven't heard of that. My intention here was to make as small of a change as possible to what's existing, but maybe I'll make a follow-up PR with a clean up of the whole task!

@chmich
Copy link

chmich commented Apr 30, 2025

Thank you, @dirschn! But the better place for this task might be the haml2rails gem?

And for the safety of the whole process: Like you, I did my test on an empty rails project. But on an existing project with many views, restoring them all from VCS can be a lot of work! You could leave a hint like Do not forget to commit before running this task ... but you know ... we are all human ... mistakes happen :)

@dirschn
Copy link
Author

dirschn commented Apr 30, 2025

@chmich That's a good point, personally I hate writing HTML and HAML is the first thing I do in a Rails project, but I forget that some folks could switch an established project. I think I'll restructure my PR a little now, and incorporate some of what you've suggested here. Thanks for the suggestions!

But the better place for this task might be the haml2rails gem?

Do you mean html2haml? If that's the case, it could definitely make sense to do that there, but I think doing it here makes just as much sense

@chmich
Copy link

chmich commented Apr 30, 2025

@dirschn Same here. The reason I wrote this is to test cases for my new gem, and to be able to quickly test new rails apps when new versions come out. And like you, I do everything in haml. Converting existing apps is a case I do not have or rarely have.

@chmich
Copy link

chmich commented Apr 30, 2025

I think i know what the error was on my case:

On the original script there is a line where html2haml ... is just wrapped within backticks! But without the gem html2haml installed (i am not sure but i think this was the case for me yesterday) ... this line does nothing, and no file is converted and no error is raised!. But, then, if you accept delete original files you end up by destroying all views. Not 100% sure but i assume this is solved by my approach above (Open3 and then raise if stderr.present?)

@dirschn
Copy link
Author

dirschn commented Apr 30, 2025

I think i know what the error was on my case:

On the original script there is a line where html2haml ... is just wrapped within backticks! But without the gem html2haml installed (i am not sure but i think this was the case for me yesterday) ... this line does nothing, and no file is converted and no error is raised!. But, then, if you accept delete original files you end up by destroying all views. Not 100% sure but i assume this is solved by my approach above (Open3 and then raise if stderr.present?)

Yup, this is the same issue I had. My solution here solves it as well, but I'm certainly interested in making it more robust. I'll take a second crack at it in the next day or two

@chmich
Copy link

chmich commented Apr 30, 2025

I noticed this on html2haml

@chmich
Copy link

chmich commented May 2, 2025

I have now removed my code block on my answer above and replaced it with the link to the repository.

I think this is now the correct installer to copy and paste into this gem or for a PR.

I highly recommend this to the maintainers of this project, as the current task is super dangerous!

That one gem adds and removes another gem is a unusual aproach! But this is the purpose: The htm2ham gem is only needed once and then never again on a certain app. Theoretically, it would be enough to install with gem install html2haml to make the conversion work. But to make sure it runs stable in a rails app, the surest way is to add it to the gemfile and remove it after the process has run through! This is what my task does.

In my case, this is a really heavy installer, where haml is just one part of more. But I wanted to have a good developer acceptance of these many necessary installation steps, so I invested a lot of time in making a safe running process. But the Haml part (link above) should serve exactly the same purpose as it is needed here.

So, my need now is solved, but, as a Haml fan, like @dirschn, i would be happy to see these features here on haml-rails.

You can easily test my task: Just follow the Steps Install from cero. By that you get much more than just haml, but haml too and see how it works.

@dirschn
Copy link
Author

dirschn commented May 2, 2025

@chmich Thanks for all that! I'll take a look at it, if I have any notes or questions do you have any preference how I get them to you? I can open an issue in your repo to keep this one a little cleaner

@chmich
Copy link

chmich commented May 2, 2025

Yes, please, @dirschn if you have questions or issues or ideas please leave it on my repo and we can develop there. The haml task (intalling both gems and execute the conversion) is within a separated module.

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

Successfully merging a pull request may close this issue.

2 participants