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

error_unless_package_manager_is_obvious! triggers when rails environment is required from outside the rails root #519

Closed
marvinthepa opened this issue Oct 8, 2024 · 0 comments · Fixed by #520
Labels

Comments

@marvinthepa
Copy link
Contributor

marvinthepa commented Oct 8, 2024

Expected behavior:

When running rails code from outside the rails root, it should still work.

Actual behavior:

It errors because of the package json check:

vendor/bundle/ruby/3.3/gems/package_json-0.1.0/lib/package_json.rb:25:in `read': $DIR does not contain a package.json (PackageJson::Error)
from vendor/bundle/ruby/3.3/gems/shakapacker-8.0.2/lib/shakapacker/utils/manager.rb:19:in `error_unless_package_manager_is_obvious!'
from vendor/bundle/ruby/3.3/gems/shakapacker-8.0.2/lib/shakapacker/railtie.rb:20:in `block in <class:Engine>'
from vendor/bundle/ruby/3.3/gems/railties-7.1.3.4/lib/rails/initializable.rb:32:in `instance_exec'
from vendor/bundle/ruby/3.3/gems/railties-7.1.3.4/lib/rails/initializable.rb:32:in `run'
from vendor/bundle/ruby/3.3/gems/railties-7.1.3.4/lib/rails/initializable.rb:61:in `block in run_initializers'
from /usr/local/lib/ruby/3.3/tsort.rb:231:in `block in tsort_each'
from /usr/local/lib/ruby/3.3/tsort.rb:353:in `block (2 levels) in each_strongly_connected_component'
from /usr/local/lib/ruby/3.3/tsort.rb:434:in `each_strongly_connected_component_from'
from /usr/local/lib/ruby/3.3/tsort.rb:352:in `block in each_strongly_connected_component'
from /usr/local/lib/ruby/3.3/tsort.rb:350:in `each'
from /usr/local/lib/ruby/3.3/tsort.rb:350:in `call'
from /usr/local/lib/ruby/3.3/tsort.rb:350:in `each_strongly_connected_component'
from /usr/local/lib/ruby/3.3/tsort.rb:229:in `tsort_each'
from /usr/local/lib/ruby/3.3/tsort.rb:208:in `tsort_each'
from vendor/bundle/ruby/3.3/gems/railties-7.1.3.4/lib/rails/initializable.rb:60:in `run_initializers'
from vendor/bundle/ruby/3.3/gems/railties-7.1.3.4/lib/rails/application.rb:426:in `initialize!'
from config/environment.rb:12:in `<top (required)>'
from <internal:/usr/local/lib/ruby/3.3/rubygems/core_ext/kernel_require.rb>:136:in `require'
from <internal:/usr/local/lib/ruby/3.3/rubygems/core_ext/kernel_require.rb>:136:in `require'
from test/test_helper.rb:25:in `<top (required)>'

test_helper.rb:25 contains

require File.expand_path('../config/environment', __dir__)

Small, reproducible repo:

Should be easy to set up, just init a rails repo, lets say in "reproducer", then add a ruby file one level up with the following content:

require File.expand_path('reproducer/config/environment', __dir__)

Setup environment:

  • Ruby version: 3.3.2
  • Rails version: 7.1.3.4
  • Shakapacker version: 8.0.2

I think the issue is in

      def self.error_unless_package_manager_is_obvious!
        return unless PackageJson.read.fetch("packageManager", nil).nil?

This should be something along the lines of

      def self.error_unless_package_manager_is_obvious!
        return unless PackageJson.read(Rails.root).fetch("packageManager", nil).nil?

(at least this fixes it for me - of course you might not what the dependency to rails here, but rather in the railtie..).

Btw. self.guess_binary seems to have the same issue, it looks for the lockfile in the current directory instead of the rails root.
On the other hand, Shakapacker::VersionChecker::NodePackageVersion.package_json_path finds the correct package.json, so maybe error_unless_package_manager_is_obvious should use that instead?

@marvinthepa marvinthepa added the bug label Oct 8, 2024
marvinthepa pushed a commit to marvinthepa/shakapacker that referenced this issue Oct 8, 2024
marvinthepa pushed a commit to marvinthepa/shakapacker that referenced this issue Oct 9, 2024
marvinthepa pushed a commit to marvinthepa/shakapacker that referenced this issue Oct 9, 2024
marvinthepa pushed a commit to marvinthepa/shakapacker that referenced this issue Oct 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant