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

Add --gemfile flag to bundle init to configure gemfile name to generate #6046

Conversation

gustavothecoder
Copy link
Contributor

What was the end-user or developer problem that led to this PR?

When running bundle init with BUNDLE_GEMFILE, the gemfile name is still the original (Gemfile) instead of the custom defined in BUNDLE_GEMFILE.

For more details, see the issue #6016.

What is your fix for the problem, implemented in this PR?

The main problem here is that Bundler#preferred_gemfile_name doesn't consider the BUNDLE_GEMFILE config, so my first change was adjust this method to use the BUNDLE_GEMFILE when necessary. I changed the method to give priority to the init_gems_rb config, so if user uses both init_gems_rb and BUNDLE_GEMFILE, the first one will be used. I decide to make this way to preserve the original behavior, but I'm not sure if this approach is ideal and I would like to know what you guys think. I think that we'll need to update the docs with this behavior, by the way.

The second change I made was to adjusting the init command to use some template when BUNDLE_GEMFILE is used. This was necessary because the template is chosen based on the gemfile name and the current implementation doesn't consider the case where the gemfile has a custom name. I noticed that all available templates has the same content, is it feasible to merge them into a single template? Because that would simplify the code a bit.

Make sure the following tasks are checked

@deivid-rodriguez
Copy link
Member

Thank you @gustavothecoder!

Any opinions on what the behavior should be if the BUNDLE_GEMFILE set already exists? Should we keep the current behavior with a warning in that case? @dentarg what do you think?

@gustavothecoder
Copy link
Contributor Author

Thank you @gustavothecoder!

Any opinions on what the behavior should be if the BUNDLE_GEMFILE set already exists? Should we keep the current behavior with a warning in that case? @dentarg what do you think?

You mean when the user sets the BUNDLE_GEMFILE using the bundle config? I think we should use the BUNDLE_GEMFILE defined in runtime.

@dentarg
Copy link

dentarg commented Nov 7, 2022 via email

@deivid-rodriguez
Copy link
Member

Some environments have a fixed BUNDLE_GEMFILE, and on top of that, when bundle exec is run, BUNDLE_GEMFILE is fixed to the current Gemfile for the subprocess. Also, Bundler generated binstubs set BUNDLE_GEMFILE right at the top.

I'm afraid this may break bundle init processes inside user scripts due to the above.

That's why I was suggesting a warning if BUNDLE_GEMFILE has a non default, existing, value, and fallback to the current behavior of creating a file named Gemfile.

@dentarg
Copy link

dentarg commented Nov 7, 2022

So BUNDLE_GEMFILE=foo bundle exec <script that calls "bundle init"> should still create Gemfile (but also print a warning)? Well, I have no strong feelings about that but really, not something I'm gonna do I think, and I like the spirit of keeping backwards compatibility, but... what the hell :) If I encountered it, I would find the behaviour a bit funny I think, but I guess it makes sense considering past behaviour.

@deivid-rodriguez
Copy link
Member

Yes, in these simple examples feeding BUNDLE_GEMFILE directly, it feels a bit dumb, but sometimes things are messy/complicated and it's actually hard to tell how/where/what set/exported BUNDLE_GEMFILE in the first place.

@gustavothecoder
Copy link
Contributor Author

Just to confirm if I understand correctly. When BUNDLE_GEMFILE already exists and is different from the default gemfile name, we'll print a warning and create another gemfile with the default name (Gemfile), that's it?

@deivid-rodriguez
Copy link
Member

@gustavothecoder Yes, that's my suggestion.

@gustavothecoder
Copy link
Contributor Author

@deivid-rodriguez I think it's working fine now:

demo.mp4

@deivid-rodriguez
Copy link
Member

Looks good!

I was also thinking that warnings should be actionable, so we should clearly explain to users how to fix the warning.

The solution would be to pass BUNDLE_GEMFILE=Gemfile explicitly to the subcommand, but depending on the situation it might not be so easy, maybe we should add a explicit --gemfile flag to bundle init?

Or maybe this is all a bit overkill and I'm overthinking this. I'd like to hear opinions from other maintainers.

@dentarg
Copy link

dentarg commented Nov 10, 2022

maybe we should add a explicit --gemfile flag to bundle init?

That makes sense to me. I have actually looked for it, and I almost thought I found it 😆 but then I noticed that it said --gemspec 😓

@gustavothecoder
Copy link
Contributor Author

maybe we should add a explicit --gemfile flag to bundle init?

That makes sense for me too, but, are we going to support both BUNDLE_GEMFILE and --gemfile?

@deivid-rodriguez what do you think of this message?: "Bundler will name the gemfile "Gemfile" because foo already exists at /some/dir/foo. To silence this warning, run bundle init --gemfile Gemfile."

@deivid-rodriguez
Copy link
Member

@gustavothecoder Sorry for the lack of feedback.

Based on the discussion, I think the easiest approach here may be to add a --gemfile option and don't change anything regarding BUNDLE_GEMFILE here, i.e., ignore it like we've been doing till now?

@gustavothecoder
Copy link
Contributor Author

@gustavothecoder Sorry for the lack of feedback.

Based on the discussion, I think the easiest approach here may be to add a --gemfile option and don't change anything regarding BUNDLE_GEMFILE here, i.e., ignore it like we've been doing till now?

That make sense. I'll change the fix.

@gustavothecoder gustavothecoder force-pushed the fix-bundle-gemfile-config-key branch 2 times, most recently from 1470d3b to 4907bc2 Compare December 22, 2022 13:41
@gustavothecoder
Copy link
Contributor Author

@deivid-rodriguez do you think we can improve the implementation?

@deivid-rodriguez
Copy link
Member

Sorry for the lack of feedback here @gustavothecoder, I'll try to review this soon.

bundler/lib/bundler/cli/init.rb Outdated Show resolved Hide resolved
bundler/lib/bundler/cli.rb Outdated Show resolved Hide resolved
@gustavothecoder gustavothecoder force-pushed the fix-bundle-gemfile-config-key branch 2 times, most recently from 0948852 to 0785efe Compare January 26, 2023 23:42
Copy link
Member

@deivid-rodriguez deivid-rodriguez left a comment

Choose a reason for hiding this comment

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

Looks mostly ready, just one more comment.

Thanks for your infinite patience here, I think it was this long discussion because this approach finally seems the simplest way to go to me!

bundler/lib/bundler/cli/init.rb Show resolved Hide resolved
@deivid-rodriguez deivid-rodriguez changed the title Fix: bundle init should respect BUNDLE_GEMFILE Add --gemfile flag to bundle init to configure gemfile name to generate Jan 27, 2023
@@ -32,7 +32,7 @@ def run
file << spec.to_gemfile
end
else
File.open(File.expand_path("../templates/#{gemfile}", __dir__), "r") do |template|
File.open(File.expand_path(template_path, __dir__), "r") do |template|
Copy link
Member

Choose a reason for hiding this comment

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

We currently have two gemfile templates: gems.rb and Gemfile, but they have the same content. I think we can use ../templates/Gemfile unconditionally here and remove the gems.rb template and the template_path method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can. I'll do it.

@gustavothecoder gustavothecoder force-pushed the fix-bundle-gemfile-config-key branch 2 times, most recently from 6d0092e to c3d9073 Compare February 12, 2023 11:29
Copy link
Member

@deivid-rodriguez deivid-rodriguez left a comment

Choose a reason for hiding this comment

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

Looks great, just one more small comment but I'll fix it myself when rebasing this! Thanks so much @gustavothecoder!

bundler/lib/bundler/man/bundle-init.1.ronn Outdated Show resolved Hide resolved
@deivid-rodriguez deivid-rodriguez added this pull request to the merge queue Feb 14, 2023
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to no response for status checks Feb 14, 2023
@deivid-rodriguez deivid-rodriguez added this pull request to the merge queue Feb 14, 2023
Merged via the queue into rubygems:master with commit c91f6ef Feb 14, 2023
@gustavothecoder gustavothecoder deleted the fix-bundle-gemfile-config-key branch February 14, 2023 20:08
deivid-rodriguez added a commit that referenced this pull request Feb 15, 2023
…ig-key

Add `--gemfile` flag to `bundle init` to configure gemfile name to generate

(cherry picked from commit c91f6ef)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants