Skip to content

Commit 979dd50

Browse files
Seifeldin7claude
andauthored
Fix NODE_ENV not being set when running shakapacker-dev-server (#823)
The DevServerRunner class was missing calls to Shakapacker.ensure_node_env! in both the main run() method and run_with_build_config() method. This caused NODE_ENV to be undefined when users ran bin/shakapacker-dev-server, breaking webpack configs that dynamically require environment-specific files. This fix ensures NODE_ENV is properly set to match RAILS_ENV (or "production" by default) before executing the dev server, consistent with how WebpackRunner and the base Runner class handle environment initialization. Fixes #802 🤖 Generated with [Claude Code](https://claude.com/claude-code) ### Summary <!-- Describe the code changes in your pull request here - were there any bugs you had fixed, features you added, tradeoffs you made? If so, mention them. If these changes have open GitHub issues, tag them here as well to keep the conversation linked. --> ### Pull Request checklist <!-- If any of the items on this checklist do not apply to the PR, both check it out and wrap it by `~`. --> - [ ] Add/update test to cover these changes - [ ] Update documentation - [ ] Update CHANGELOG file <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Development server now ensures the Node environment is properly initialized before starting, preventing potential environment-related configuration issues. * **Tests** * Added verification that NODE_ENV is correctly set to "development" when required. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: Claude <[email protected]>
1 parent 272cb11 commit 979dd50

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

lib/shakapacker/dev_server_runner.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ def self.run(argv)
1818
exit(0)
1919
end
2020

21+
Shakapacker.ensure_node_env!
22+
2123
# Check for --build flag
2224
build_index = argv.index("--build")
2325
if build_index
@@ -65,6 +67,8 @@ def self.run(argv)
6567
end
6668

6769
def self.run_with_build_config(argv, build_config)
70+
Shakapacker.ensure_node_env!
71+
6872
# Apply build config environment variables
6973
build_config[:environment].each do |key, value|
7074
ENV[key] = value.to_s

spec/shakapacker/dev_server_runner_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,37 @@
136136
end
137137
end
138138

139+
describe "NODE_ENV environment variable" do
140+
it "sets NODE_ENV when not already set" do
141+
original_node_env = ENV.delete("NODE_ENV")
142+
143+
begin
144+
Dir.chdir(test_app_path) do
145+
klass = Shakapacker::DevServerRunner
146+
147+
allow(Shakapacker::Utils::Manager).to receive(:error_unless_package_manager_is_obvious!)
148+
149+
instance = klass.new([])
150+
151+
allow(klass).to receive(:new).and_return(instance)
152+
153+
# Stub build_cmd and system to prevent actual execution
154+
allow(instance).to receive(:build_cmd).and_return(["webpack", "serve"])
155+
allow(instance).to receive(:system) do |*args|
156+
system("true") # Sets $? to successful status
157+
true
158+
end
159+
160+
klass.run([])
161+
162+
expect(ENV["NODE_ENV"]).to eq("development")
163+
end
164+
ensure
165+
ENV["NODE_ENV"] = original_node_env if original_node_env
166+
end
167+
end
168+
end
169+
139170
private
140171

141172
def verify_command(cmd, argv: [], env: Shakapacker::Compiler.env)

0 commit comments

Comments
 (0)