Skip to content

Bugfix: Prevent generators from overwriting files #274

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

Merged
merged 8 commits into from
Dec 8, 2024

Conversation

maxemitchell
Copy link
Contributor

This adds a create method that raises an error if the file already exists.

This allows the write method to still be used by specs and when we want to override (e.g. for editing the routes file).

Addresses #181

@maxemitchell maxemitchell force-pushed the prevent_file_overwriting branch from 6c2d4ae to 3f32755 Compare November 14, 2024 23:11
@@ -88,7 +151,7 @@ class #{inflector.camelize(action)} < #{inflector.camelize(app)}::View
EXPECTED

expect(fs.read("app/templates/#{controller}/#{action}.html.erb")).to eq(template_file)
expect(output).to include("Created app/views/#{controller}/#{action}.rb")
expect(output).to include("Created app/templates/#{controller}/#{action}.html.erb")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was looking for the wrong file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice find

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find indeed! How did this even pass before? 😅

@@ -305,47 +375,45 @@ class #{inflector.camelize(action)} < #{inflector.camelize(app)}::View
EXPECTED

expect(fs.read("app/templates/#{controller}/#{action}.html.erb")).to eq(template_file)
expect(output).to include("Created app/views/#{controller}/#{action}.rb")
expect(output).to include("Created app/templates/#{controller}/#{action}.html.erb")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing here with wrong file

Comment on lines +382 to +383
context "with nested action name" do
let(:context) { Hanami::CLI::Generators::App::ActionContext.new(inflector, app, nil, %w[api users], "thing") }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a context for this

end
end

context "deeply nested action" do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this deeply nested action context was duplicate

@maxemitchell maxemitchell marked this pull request as ready for review November 14, 2024 23:18
Copy link
Member

@timriley timriley left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for taking care of this, @maxemitchell! Especially continuing the work on it even after RubyConf finished — truly heroic! 🤩

The changes here all look good to me 👍🏼

I'd be happy to merge this as is, because this PR alone is already a valuable improvement on what we're doing now.

Before I do that, I'd like to take this moment to get your thoughts on how this PR fits into the bigger picture.


With this PR in its current state, if a user ran a generator that encountered an existing file, the hanami CLI would crash with the new FileAlreadyExistsError and print out a stack trace. On the plus side, this prevents unwanted file overwriting, but the rest of the user experience here doesn't feel so good. The error and stacktrace will be overwhelming, and it may be hard for the user to identify the cause for the CLI command crashing.

I think a better experience here might be fore our generate command classes to catch this FileAlreadyExistsError and print a nicer error message before then cleanly exiting (with a non-zero exit code). This might be something we can put into a single base command class for all the generators, so that we don't have to implement it in every single command class.

What do you think about this? Any thoughts on how else this might be made nicer? And now the big question: is this something you'd also be interested in working on? 😇 I'd be very happy for it to be in a follow-up PR so that we can merge this one and stay focused on smaller, incremental changes. Let me know!

@@ -88,7 +151,7 @@ class #{inflector.camelize(action)} < #{inflector.camelize(app)}::View
EXPECTED

expect(fs.read("app/templates/#{controller}/#{action}.html.erb")).to eq(template_file)
expect(output).to include("Created app/views/#{controller}/#{action}.rb")
expect(output).to include("Created app/templates/#{controller}/#{action}.html.erb")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find indeed! How did this even pass before? 😅

@stephannv
Copy link

@timriley If you want, I can make a following PR adding a prompt confirmation:

Screen.Recording.2024-12-06.at.16.54.31.mov

@timriley
Copy link
Member

timriley commented Dec 6, 2024

@stephannv that would be amazing! How about I merge this PR today, and then you can build on top of main?

@stephannv
Copy link

@stephannv that would be amazing! How about I merge this PR today, and then you can build on top of main?

Sure @timriley !

@timriley
Copy link
Member

timriley commented Dec 8, 2024

I resolved a merge conflict to lib/hanami/cli/generators/app/component.rb after the changes in #277.

Everything looks good now. I'll merge.

Thanks again @maxemitchell for your contribution! ❤️ Please reach out if you're interested in doing something like this again 😄

@timriley timriley merged commit 04ec18a into hanami:main Dec 8, 2024
3 checks passed
@timriley
Copy link
Member

timriley commented Dec 8, 2024

@stephannv This is merged, feel free to base any future work off the main branch :)

@stephannv
Copy link

@timriley while I was coding the prompt confirmation I figured out that asking the users if they want to overwrite the file may not be the best experience, this can lead to many successive questions (eg. generating an action with action, test, view, template files), and in the future we may want to add hanami g xyz -y to say yes to all questions, or add a option to skip (don't create the file and don't halt the generator).

So now I'm not sure if this feature (overwrite prompt) is worth it since Hanami generators are generating simple "blank" files and not generating complex setup.

But if we still want to overwrite existing files, maybe a --force option can be simple and useful, eg.

$ hanami g action books.index
Cannot overwrite existing file: `app/actions/books/index.rb`. Pass `--force` argument to force file overwriting.

$ hanami g action books.index --force
...
Updated app/actions/books/index.rb
...

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 this pull request may close these issues.

4 participants