-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
assets:compile doesn't respect NODE_ENV #1395
Comments
@y310 Thanks, ahh yes. The idea was to align with Sprockets Anyway would be good to know your use case? |
My case is a bit irregular... I run If |
I'm going to close this issue since this behavior seems like a spec rather than bug. |
I want to run |
@gauravtiwari - We have spent hours debugging the above bug, since it directly contradicts the Custom Rails Environments instructions for this gem, which state that NODE_ENV can force
If one has to use |
Here's our use case: We have a bug in one of our projects that is only appearing in production, but not in our dev environments. It likely has to do with the order of imports/compilation, and we wanted to debug that. When debugging that, we wanted to compare a dev compile to a prod compile. We wound up going in circles just trying to figure out that https://github.com/rails/webpacker#custom-rails-environments 🤔 Running $ NODE_ENV=development ./bin/webpack 1 ↵
module.js:491
throw err;
^
Error: Cannot find module 'eslint-friendly-formatter'
at Function.Module._resolveFilename (module.js:489:15)
at Function.Module._load (module.js:439:25)
at Module.require (module.js:517:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/Users/username/path/to/project/config/webpack/loaders/eslint.js:8:16)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3) So now we would have to debug or workaround that and the rabbit hole deepens... |
Let's address this one properly, the idea earlier was to align with sprockets behaviour but seems like it's much more complicated in Webpacker's case. @csalvato What happens if you run yarn install? I guess since yarn install is being run in production mode it's stripping off all the dev dependencies. Btw, what environment is this - CI? |
@csalvato Can you double check that
I don't think webpack will ever fit nicely in a sprokets-sized hole. There will always be friction from those who expected webpack flows and those who expected sprokets flows...it's all about listening to user expectations. In this case, I don't think users expect (or want) webpackER to ignore the system-set |
@gauravtiwari This is the output: $ yarn install
yarn install v1.10.0
[1/5] 🔍 Validating package.json...
[2/5] 🔍 Resolving packages...
[3/5] 🚚 Fetching packages...
[4/5] 🔗 Linking dependencies...
warning "@rails/webpacker > [email protected]" has unmet peer dependency "caniuse-lite@^1.0.30000697".
warning " > [email protected]" has incorrect peer dependency "jquery@^1.7.0".
warning " > [email protected]" has unmet peer dependency "webpack@^2.0.0 || ^3.0.0".
warning "pdfjs-dist > [email protected]" has unmet peer dependency "webpack@^2.0.0 || ^3.0.0 || ^4.0.0".
warning " > [email protected]" has unmet peer dependency "webpack@^2.0.0 || >= 3.0.0-rc.0 || ^3.0.0".
warning " > [email protected]" has unmet peer dependency "css-loader@*".
warning " > [email protected]" has unmet peer dependency "webpack@^1.0.0 || ^2.0.0 || ^3.0.0".
warning "karma-webpack > [email protected]" has unmet peer dependency "webpack@^1.0.0 || ^2.0.0 || ^3.0.0".
warning " > [email protected]" has unmet peer dependency "webpack@^2.2.0 || ^3.0.0".
[5/5] 📃 Building fresh packages...
✨ Done in 31.92s.
Local dev environment. Not CI.
Moving it to dependencies enabled |
@gauravtiwari I feel like this is a sufficiently reoccurring misconception that may justify a small warning in the README.md. I have created a PR to add a small section about this to |
Yeah, this is very misleading in the docs, if you run:
This will load There is no way to |
Any progress on this? At the very least, I think the Custom Rails environments part of the docs should be updated to reflect reality. I also wasted time trying to understand the difference in behavior between what the docs clearly state and what the gem is actually doing. |
From the docs @bobziuchkovski linked:
@quinn or @bobziuchkovski, can you tell me how we can make the docs less misleading? |
@jakeNiemiec Also from the docs:
This isn't true. Running The "Please note, NODE_ENV can either be set to production, development or test" and the example of specifying NODE_ENV=development and running the asset precompile lead you to believe specifying |
I can see what you mean. It was probably correct up until d8dd446. |
Yes, I believe that's the case. My own feeling is that explicitly-provided ENV vars should take precedence over defaults, and that there seems to be some friction in the current implementation regarding RAILS_ENV -> NODE_ENV mapping. My "magic wand" / ideal scenario would be:
I'm also okay accepting my opinion on the above might not be the consensus, but if that's the case, it would be nice to reflect reality in the docs. Since the docs called out being able to specify a NODE_ENV on the command line, I burned some amount of time trying to figure out why settings I placed in config/webpack/development.js weren't doing anything with |
But none of that matters since (as of webpack@4) you can now set From the webpack docs:
If not set, webpack sets Please remember that setting
As stated above, the PRs to address concerns are appreciated. |
PRs to address documentation concerns or PRs to address the behavioral concerns? I'll gladly submit a PR to change the NODE_ENV-handling behavior, but it doesn't sound like either you or @gauravtiwari see the current behavior as problematic? I'd prefer to pursue behavioral changes only if I think there's a chance my changes will be accepted.
Do you mean always using |
I'm inlining a client side API key with webpack / node-config (option 1 here) when I build my Docker image. The key must be different in QA / Prod environments. But since NODE_ENV=qa isn't respected, I get the prod key in both environments. It would be nice if webpacker recognized more environments than prod/dev. |
Assuming that you are still unable to use -NODE_ENV=qa
+NODE_ENV=test In general, I would avoid baking API keys into your packs. I would use a |
Using |
cc @ixti |
Running into this as well now since we configure production only stuff in |
@coding-bunny I've adjusted my process to have NODE_ENV to only ever be equal to "development" or "production", and to only be used for whether the bundle is in "development mode", or for the case of prod or CI, the minified / bundled version |
That doesn't help us. It also makes 0 sense that under a test environment, production code is loaded/executed. |
@coding-bunny it works out well for us, we can build a bundle once for CI, then deploy that same bundle to production (assuming it passes CI) |
@coding-bunny I'm under the impression that this is a major part of "integration testing"; to test the files you are sending to production as @quinn described. Perhaps you can give some more details on what you mean by "blowing up our CI"? |
When our applications are running in production mode, they need to call specific endpoints to register themselves. Only in production mode, because for the other environments, we simply don't care. With this issue from webpacker, this causes our tests in CI to simply crash because either the endpoint they need to call is not available/reachable, or they don't have the required environment variables available to make these calls. Why? Because webpacker decides to load production.js and the production configuration because of this issue, something that's not wanted at all when running explicitly in a testing environment. So right now we have the following choices:
Right now we're seriously going with option one if this is the behavior webpacker will be keeping. |
Sprockets still works great.
This would usually be achieved mocking your endpoints. The script that sets up the mocks would conditionally load based on None of this is to say that this is the "right way" of doing things, you could even do something quick and dirty like: <script>
window.env = '<%= Rails&.env || nil %>' || 'unknown';
window.isDev = function() {
return window['env'] === 'development' || false;
};
window.isTest = function() {
return window['env'] !== 'test';
};
window.isQa = function() {
return window['env'] !== 'qa';
};
window.isProd = function() {
return window['env'] !== 'development';
};
</script> Just put that script before before any webpacker pack is loaded. This way, the notion of |
I'm not even getting to the part of running the tests because of this behavior! The only thing that works to even get into the testing phase is running |
If the problem happens at the initializer level, not the webpack level, can't the initializers check against RAILS_ENV instead of NODE_ENV? Would you mind opening another issue with the error message and other details? |
Hey @jakeNiemiec, what's the high-level rationale for restricting NODE_ENV to only production/development rather than respecting the NODE_ENV passed by the developer? |
@dblarons from what I understand they're trying to "emulate sprockets". A Framework that's actually configured to respect ENV inputs and works in/with different environments. As is the Rails convention..... |
@dblarons @coding-bunny Please realize that both rails & node.js have been around for a long time. Both have established practices. Don't expect high-level: Webpacker is a wrapper around the JS tool webpack. Webpack only accepts lower-level: Restricting Hope that helps. |
No it doesn't help, because this is not even what's described in your README:
What's the point of having custom environments when it just boilds down loading either |
This was addressed here.
Perhaps you could create a PR; that way, we can work together to fix discrepancies that you see. I see that the reason this issue was reopened has been resolved, I am going t close this one since we have veered off topic. Feel free to create a new PR/issue with specific examples and I'm sure we can figure something out. (I don't want to spam the other folks subscribed) |
🎉 A solution by @coding-bunny has landed in #2341. |
🙏 thank you @coding-bunny !! |
It seems that
NODE_ENV=development bundle exec rails assets:precompile
ignoresNODE_ENV
environment variable.In order to confirm the problem, I added simple debug code to
config/webpack/production.js
And here is the result.
In spite of
NODE_ENV=development
,process.env.NODE_ENV
isproduction
.Probably, it's caused by this line https://github.com/rails/webpacker/blob/master/lib/tasks/webpacker/compile.rake#L24
This is a repository which includes the reproducing code.
https://github.com/y310/webpacker-test
The text was updated successfully, but these errors were encountered: