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

Pass in shakapacker config consistently #448

Merged
merged 8 commits into from
Mar 23, 2024

Conversation

tomdracz
Copy link
Collaborator

@tomdracz tomdracz commented Mar 11, 2024

Summary

Whilst investigating #408 I couldn't quite replicate the issues but did notice there a few places in the code where we were not respecting SHAKAPACKER_CONFIG env variable. This moves it up one level to main Shakapacker::Instance so it should be used consistently.

There are places like runners where we were using configuration object directly but they were already accounting for path set by env variable.

  • Add/update test to cover these changes
  • Update documentation
  • Update CHANGELOG file

@tomdracz tomdracz marked this pull request as ready for review March 11, 2024 10:36
@@ -106,7 +106,7 @@ def webpack_env

env.merge(
"SHAKAPACKER_ASSET_HOST" => instance.config.asset_host,
"SHAKAPACKER_CONFIG" => instance.config_path.to_s
"SHAKAPACKER_CONFIG" => instance.config.config_path.to_s
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Technically could be left as is since both places should have same source of truth but makes it so we're consistent with the line above

@config_path = Pathname.new(ENV["SHAKAPACKER_CONFIG"] || config_path)
@config_path = config_path
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't it better to use our configuration as the single source of truth? Only the Configuration class should care about how to set such variables and others should only and only refer to the Configuration instance to know the value.

I remember once we discussed implementing this gradually. At the moment, I don't remember why I checked SHAKAPACKER_CONFIG both in the Configuration and in the Instance classes. For this, I need to review the changes and possibly chats from that time.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see that the change in the Instance class is new. In the past, we only handled this issue in the Configuration class.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm happy with this being wherever, just needs to be consistent.

In runner

@shakapacker_config = ENV["SHAKAPACKER_CONFIG"] || File.join(@app_path, "config/shakapacker.yml")
we were passing passing Shakapacker config either from env or the file - means that we were effectively checking env twice before those changes
In Env we were checking it only on the instance
delegate :config_path, :logger, to: :@instance
which meant the env variable one was disregarded

If we want to keep on the instance, then need to address those two. Happy to do that if that's what we want to do instead of the instance

Copy link
Contributor

Choose a reason for hiding this comment

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

You are right.

I think the right thing is that anything related to configuration to be handled by and fetched from the Configuration class.

Copy link
Collaborator Author

@tomdracz tomdracz Mar 11, 2024

Choose a reason for hiding this comment

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

Amended @ahangarha Need to dig through spec failures though

Copy link
Collaborator Author

@tomdracz tomdracz Mar 11, 2024

Choose a reason for hiding this comment

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

Actually there's fun circular dependency now:

  • instance tries to get env
  • env is needed to create config
  • kaboom!

IMO this

if config_path.exist?
should take into account the custom config but is run as dependency of config. I knew there was a reason I didn't try this is config class by itself.

It's either a deeper refactor needed or back to what it was and doing the change on Instance level since that's the common ancestor. Thoughts? Reverted the changes back to moving config set on Instance for now

@@ -7,7 +7,7 @@ class Shakapacker::Instance

def initialize(root_path: Rails.root, config_path: Rails.root.join("config/shakapacker.yml"))
@root_path = root_path
@config_path = config_path
@config_path = Pathname.new(ENV["SHAKAPACKER_CONFIG"] || config_path)
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As per https://github.com/shakacode/shakapacker/pull/448/files#r1519695275

We check for available environments in lib/shakapacker/env.rb - that was disregarding custom files, so for example if my config passed through ENV defined a new environment, it would be not picked up and fallback on production env

Since Configuration needs env instance passed by itself, it cannot just look at the instance.config as it will create circular dependency

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmmm...

Interesting. Then let's see if we need to do some refactoring (perhaps must be done before v8 release)

@justin808 justin808 merged commit a92e867 into next Mar 23, 2024
52 checks passed
@justin808 justin808 deleted the pass-in-shakapacker-config-consistently branch March 23, 2024 00:28
ahangarha pushed a commit that referenced this pull request Mar 28, 2024
* Reference config from config rather than instance in compiler
justin808 added a commit that referenced this pull request May 8, 2024
* Remove deprecated webpacker spelling (#429)
* feat!: drop support for Ruby 2.6 (#415)
* feat!: remove `https` option (#414)
* feat!: remove `relative_url_root` option (#413)
* feat!: drop support for Node v12 (#431)
* feat!: remove `yarn_install` task (#412)
* ci: cancel in-progress jobs when new changes are pushed (#432)
* ci: merge ruby and node based jobs into single workflows (#434)
* ci: merge node related checks into a single workflow
* ci: merge ruby related checks into a single workflow
* ci: rename jobs
* ci: test against Ruby 3.1 and 3.2, and Node 20 (#433)
* ci: test against Ruby 3.1 and 3.2
* ci: test against Node 20
* ci: only run Rubocop with latest Ruby (#438)
* chore: update pull request template (#437)
* ci: test against Rails 7.1 (#436)
* feat!: remove `globalMutableWebpackConfig` (#439)
* ci: don't run on pushes to `next` (#444)
* feat!: remove `verify_file_existance` method (#446)
* feat!: remove `check_yarn` task (#443)
* ci: use latest bundler in generator jobs (#445)
* feat!: enable `ensure_consistent_versioning` by default (#447)
* Remove dependency on glob (#435)
* Remove dependency on glob
* Pass in shakapacker config consistently (#448)
* Reference config from config rather than instance in compiler
* Strip additional_paths from the asset paths generated in the file.js rule (#403)
* Add config.includePaths and strip includePaths from file.js output paths
* Refactor code to fix lint issue
* Make sure to replace only once
* Change variable name
* Revert includePath change and make stripping additional_paths opt-in
* refactor: use snake_case for method name (#450)
* fix: only strip paths that start with source path (#451)
* feat!: always use `package_json` for managing node packages (#430)
* feat!: minor cleanup of js utilities (#454)
* test: require `ostruct` explicitly (#460)
* ci: only run ESLint on latest Node (#462)
* docs: create upgrade guide for v8 (#458)
* V8 upgrade guide
* docs: write v8 upgrade doc
* docs: format with Prettier
* feat!: move tests and helpers into  (#459)
* test: move into `test` directory
* test: update import paths
* test: update jest config
* refactor: move test helpers
* test: move jest config into dedicated file
* docs: add changelog entry
* chore: remove unneeded eslint ignore
* refactor: update js linting (#461)
* refactor: setup and apply Prettier (#463)
* refactor: use `eslint-plugin-jest` (#464)
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.

3 participants