-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ruby.rb
1431 lines (1209 loc) · 48.5 KB
/
ruby.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require "tmpdir"
require "digest/md5"
require "benchmark"
require "rubygems"
require "language_pack"
require "language_pack/base"
require "language_pack/ruby_version"
require "language_pack/helpers/nodebin"
require "language_pack/helpers/node_installer"
require "language_pack/helpers/yarn_installer"
require "language_pack/helpers/jvm_installer"
require "language_pack/helpers/layer"
require "language_pack/helpers/binstub_check"
require "language_pack/version"
# base Ruby Language Pack. This is for any base ruby app.
class LanguagePack::Ruby < LanguagePack::Base
NAME = "ruby"
LIBYAML_VERSION = "0.1.7"
LIBYAML_PATH = "libyaml-#{LIBYAML_VERSION}"
RBX_BASE_URL = "http://binaries.rubini.us/heroku"
NODE_BP_PATH = "vendor/node/bin"
Layer = LanguagePack::Helpers::Layer
# detects if this is a valid Ruby app
# @return [Boolean] true if it's a Ruby app
def self.use?
instrument "ruby.use" do
File.exist?("Gemfile")
end
end
def self.bundler
@@bundler ||= LanguagePack::Helpers::BundlerWrapper.new.install
end
def bundler
self.class.bundler
end
def initialize(*args)
super(*args)
@fetchers[:mri] = LanguagePack::Fetcher.new(VENDOR_URL, @stack)
@fetchers[:rbx] = LanguagePack::Fetcher.new(RBX_BASE_URL, @stack)
@node_installer = LanguagePack::Helpers::NodeInstaller.new
@yarn_installer = LanguagePack::Helpers::YarnInstaller.new
@jvm_installer = LanguagePack::Helpers::JvmInstaller.new(slug_vendor_jvm, @stack)
end
def name
"Ruby"
end
def default_addons
instrument "ruby.default_addons" do
add_dev_database_addon
end
end
def default_config_vars
instrument "ruby.default_config_vars" do
vars = {
"LANG" => env("LANG") || "en_US.UTF-8",
}
ruby_version.jruby? ? vars.merge({
"JAVA_OPTS" => default_java_opts,
"JRUBY_OPTS" => default_jruby_opts
}) : vars
end
end
def default_process_types
instrument "ruby.default_process_types" do
{
"rake" => "bundle exec rake",
"console" => "bundle exec irb"
}
end
end
def best_practice_warnings
if bundler.has_gem?("asset_sync")
warn(<<-WARNING)
You are using the `asset_sync` gem.
This is not recommended.
See https://devcenter.heroku.com/articles/please-do-not-use-asset-sync for more information.
WARNING
end
end
def compile
instrument 'ruby.compile' do
# check for new app at the beginning of the compile
new_app?
Dir.chdir(build_path)
remove_vendor_bundle
warn_bundler_upgrade
warn_bad_binstubs
install_ruby(slug_vendor_ruby, build_ruby_path)
install_jvm
setup_language_pack_environment(
ruby_layer_path: File.expand_path("."),
gem_layer_path: File.expand_path("."),
bundle_path: "vendor/bundle",
bundle_default_without: "development:test"
)
allow_git do
install_bundler_in_app(slug_vendor_base)
load_bundler_cache
build_bundler
post_bundler
create_database_yml
install_binaries
run_assets_precompile_rake_task
end
config_detect
best_practice_warnings
warn_outdated_ruby
setup_profiled(ruby_layer_path: "$HOME", gem_layer_path: "$HOME") # $HOME is set to /app at run time
setup_export
cleanup
super
end
rescue => e
warn_outdated_ruby
raise e
end
def build
new_app?
remove_vendor_bundle
warn_bad_binstubs
ruby_layer = Layer.new(@layer_dir, "ruby", launch: true)
install_ruby("#{ruby_layer.path}/#{slug_vendor_ruby}")
ruby_layer.metadata[:version] = ruby_version.version
ruby_layer.metadata[:patchlevel] = ruby_version.patchlevel if ruby_version.patchlevel
ruby_layer.metadata[:engine] = ruby_version.engine.to_s
ruby_layer.metadata[:engine_version] = ruby_version.engine_version
ruby_layer.write
gem_layer = Layer.new(@layer_dir, "gems", launch: true, cache: true, build: true)
setup_language_pack_environment(
ruby_layer_path: ruby_layer.path,
gem_layer_path: gem_layer.path,
bundle_path: "#{gem_layer.path}/vendor/bundle",
bundle_default_without: "development:test"
)
allow_git do
# TODO install bundler in separate layer
topic "Loading Bundler Cache"
gem_layer.validate! do |metadata|
valid_bundler_cache?(gem_layer.path, gem_layer.metadata)
end
install_bundler_in_app("#{gem_layer.path}/#{slug_vendor_base}")
build_bundler
# TODO post_bundler might need to be done in a new layer
bundler.clean
gem_layer.metadata[:gems] = Digest::SHA2.hexdigest(File.read("Gemfile.lock"))
gem_layer.metadata[:stack] = @stack
gem_layer.metadata[:ruby_version] = run_stdout(%q(ruby -v)).strip
gem_layer.metadata[:rubygems_version] = run_stdout(%q(gem -v)).strip
gem_layer.metadata[:buildpack_version] = BUILDPACK_VERSION
gem_layer.write
create_database_yml
# TODO replace this with multibuildpack stuff? put binaries in their own layer?
install_binaries
run_assets_precompile_rake_task
end
setup_profiled(ruby_layer_path: ruby_layer.path, gem_layer_path: gem_layer.path)
setup_export(gem_layer)
config_detect
best_practice_warnings
cleanup
super
end
def cleanup
end
def config_detect
end
private
# A bad shebang line looks like this:
#
# ```
# #!/usr/bin/env ruby2.5
# ```
#
# Since `ruby2.5` is not a valid binary name
#
def warn_bad_binstubs
check = LanguagePack::Helpers::BinstubCheck.new(app_root_dir: Dir.pwd, warn_object: self)
check.call
end
def default_malloc_arena_max?
return true if @metadata.exists?("default_malloc_arena_max")
return @metadata.touch("default_malloc_arena_max") if new_app?
return false
end
def stack_not_14_not_16?
case stack
when "cedar-14", "heroku-16"
return false
else
return true
end
end
def warn_bundler_upgrade
old_bundler_version = @metadata.read("bundler_version").strip if @metadata.exists?("bundler_version")
if old_bundler_version && old_bundler_version != bundler.version
warn(<<-WARNING, inline: true)
Your app was upgraded to bundler #{ bundler.version }.
Previously you had a successful deploy with bundler #{ old_bundler_version }.
If you see problems related to the bundler version please refer to:
https://devcenter.heroku.com/articles/bundler-version#known-upgrade-issues
WARNING
end
end
# For example "vendor/bundle/ruby/2.6.0"
def self.slug_vendor_base
@slug_vendor_base ||= begin
command = %q(ruby -e "require 'rbconfig';puts \"vendor/bundle/#{RUBY_ENGINE}/#{RbConfig::CONFIG['ruby_version']}\"")
out = run_no_pipe(command, user_env: true).strip
error "Problem detecting bundler vendor directory: #{out}" unless $?.success?
out
end
end
# the relative path to the bundler directory of gems
# @return [String] resulting path
def slug_vendor_base
instrument 'ruby.slug_vendor_base' do
@slug_vendor_base ||= self.class.slug_vendor_base
end
end
# the relative path to the vendored ruby directory
# @return [String] resulting path
def slug_vendor_ruby
"vendor/#{ruby_version.version_without_patchlevel}"
end
# the relative path to the vendored jvm
# @return [String] resulting path
def slug_vendor_jvm
"vendor/jvm"
end
# the absolute path of the build ruby to use during the buildpack
# @return [String] resulting path
def build_ruby_path
"/tmp/#{ruby_version.version_without_patchlevel}"
end
# fetch the ruby version from bundler
# @return [String, nil] returns the ruby version if detected or nil if none is detected
def ruby_version
instrument 'ruby.ruby_version' do
return @ruby_version if @ruby_version
new_app = !File.exist?("vendor/heroku")
last_version_file = "buildpack_ruby_version"
last_version = nil
last_version = @metadata.read(last_version_file).strip if @metadata.exists?(last_version_file)
@ruby_version = LanguagePack::RubyVersion.new(bundler.ruby_version,
is_new: new_app,
last_version: last_version)
return @ruby_version
end
end
# default JAVA_OPTS
# return [String] string of JAVA_OPTS
def default_java_opts
"-Dfile.encoding=UTF-8"
end
def set_jvm_max_heap
<<-EOF
limit=$(ulimit -u)
case $limit in
512) # 2X, private-s: memory.limit_in_bytes=1073741824
echo "$opts -Xmx671m -XX:CICompilerCount=2"
;;
16384) # perf-m, private-m: memory.limit_in_bytes=2684354560
echo "$opts -Xmx2g"
;;
32768) # perf-l, private-l: memory.limit_in_bytes=15032385536
echo "$opts -Xmx12g"
;;
*) # Free, Hobby, 1X: memory.limit_in_bytes=536870912
echo "$opts -Xmx300m -Xss512k -XX:CICompilerCount=2"
;;
esac
EOF
end
def set_java_mem
<<-EOF
if ! [[ "${JAVA_OPTS}" == *-Xmx* ]]; then
export JAVA_MEM=${JAVA_MEM:--Xmx${JVM_MAX_HEAP:-384}m}
fi
EOF
end
def set_default_web_concurrency
<<-EOF
case $(ulimit -u) in
256)
export HEROKU_RAM_LIMIT_MB=${HEROKU_RAM_LIMIT_MB:-512}
export WEB_CONCURRENCY=${WEB_CONCURRENCY:-2}
;;
512)
export HEROKU_RAM_LIMIT_MB=${HEROKU_RAM_LIMIT_MB:-1024}
export WEB_CONCURRENCY=${WEB_CONCURRENCY:-4}
;;
16384)
export HEROKU_RAM_LIMIT_MB=${HEROKU_RAM_LIMIT_MB:-2560}
export WEB_CONCURRENCY=${WEB_CONCURRENCY:-8}
;;
32768)
export HEROKU_RAM_LIMIT_MB=${HEROKU_RAM_LIMIT_MB:-6144}
export WEB_CONCURRENCY=${WEB_CONCURRENCY:-16}
;;
*)
;;
esac
EOF
end
# default JRUBY_OPTS
# return [String] string of JRUBY_OPTS
def default_jruby_opts
"-Xcompile.invokedynamic=false"
end
# default Java Xmx
# return [String] string of Java Xmx
def default_java_mem
"-Xmx${JVM_MAX_HEAP:-384}m"
end
# sets up the environment variables for the build process
def setup_language_pack_environment(ruby_layer_path:, gem_layer_path:, bundle_path:, bundle_default_without:)
instrument 'ruby.setup_language_pack_environment' do
if ruby_version.jruby?
ENV["PATH"] += ":bin"
ENV["JAVA_MEM"] = run(<<~SHELL).strip
#{set_jvm_max_heap}
echo #{default_java_mem}
SHELL
ENV["JRUBY_OPTS"] = env('JRUBY_BUILD_OPTS') || env('JRUBY_OPTS')
ENV["JAVA_HOME"] = @jvm_installer.java_home
end
setup_ruby_install_env(ruby_layer_path)
# By default Node can address 1.5GB of memory, a limitation it inherits from
# the underlying v8 engine. This can occasionally cause issues during frontend
# builds where memory use can exceed this threshold.
#
# This passes an argument to all Node processes during the build, so that they
# can take advantage of all available memory on the build dynos.
ENV["NODE_OPTIONS"] ||= "--max_old_space_size=2560"
# TODO when buildpack-env-args rolls out, we can get rid of
# ||= and the manual setting below
default_config_vars.each do |key, value|
ENV[key] ||= value
end
paths = []
gem_path = "#{gem_layer_path}/#{slug_vendor_base}"
ENV["GEM_PATH"] = gem_path
ENV["GEM_HOME"] = gem_path
ENV["DISABLE_SPRING"] = "1"
# Rails has a binstub for yarn that doesn't work for all applications
# we need to ensure that yarn comes before local bin dir for that case
paths << yarn_preinstall_bin_path if yarn_preinstalled?
# Need to remove `./bin` folder since it links to the wrong --prefix ruby binstubs breaking require in Ruby 1.9.2 and 1.8.7.
# Because for 1.9.2 and 1.8.7 there is a "build" ruby and a non-"build" Ruby
paths << "#{File.expand_path(".")}/bin" unless ruby_version.ruby_192_or_lower?
paths << "#{gem_layer_path}/#{bundler_binstubs_path}" # Binstubs from bundler, eg. vendor/bundle/bin
paths << "#{gem_layer_path}/#{slug_vendor_base}/bin" # Binstubs from rubygems, eg. vendor/bundle/ruby/2.6.0/bin
paths << "#{slug_vendor_jvm}/bin" if ruby_version.jruby?
paths << ENV["PATH"]
ENV["PATH"] = paths.join(":")
ENV["BUNDLE_WITHOUT"] = env("BUNDLE_WITHOUT") || bundle_default_without
if ENV["BUNDLE_WITHOUT"].include?(' ')
ENV["BUNDLE_WITHOUT"] = ENV["BUNDLE_WITHOUT"].tr(' ', ':')
warn("Your BUNDLE_WITHOUT contains a space, we are converting it to a colon `:` BUNDLE_WITHOUT=#{ENV["BUNDLE_WITHOUT"]}", inline: true)
end
ENV["BUNDLE_PATH"] = bundle_path
ENV["BUNDLE_BIN"] = bundler_binstubs_path
ENV["BUNDLE_DEPLOYMENT"] = "1"
ENV["BUNDLE_GLOBAL_PATH_APPENDS_RUBY_SCOPE"] = "1" if bundler.needs_ruby_global_append_path?
end
end
# Sets up the environment variables for subsequent processes run by
# muiltibuildpack. We can't use profile.d because $HOME isn't set up
def setup_export(layer = nil)
instrument 'ruby.setup_export' do
if layer
paths = ENV["PATH"]
else
paths = ENV["PATH"].split(":").map do |path|
/^\/.*/ !~ path ? "#{build_path}/#{path}" : path
end.join(":")
end
# TODO ensure path exported is correct
set_export_path "PATH", paths, layer
if layer
gem_path = "#{layer.path}/#{slug_vendor_base}"
else
gem_path = "#{build_path}/#{slug_vendor_base}"
end
set_export_path "GEM_PATH", gem_path, layer
set_export_default "LANG", "en_US.UTF-8", layer
# TODO handle jruby
if ruby_version.jruby?
add_to_export set_jvm_max_heap
add_to_export set_java_mem
set_export_default "JAVA_OPTS", default_java_opts
set_export_default "JRUBY_OPTS", default_jruby_opts
end
set_export_default "BUNDLE_PATH", ENV["BUNDLE_PATH"], layer
set_export_default "BUNDLE_WITHOUT", ENV["BUNDLE_WITHOUT"], layer
set_export_default "BUNDLE_BIN", ENV["BUNDLE_BIN"], layer
set_export_default "BUNDLE_GLOBAL_PATH_APPENDS_RUBY_SCOPE", ENV["BUNDLE_GLOBAL_PATH_APPENDS_RUBY_SCOPE"], layer if bundler.needs_ruby_global_append_path?
set_export_default "BUNDLE_DEPLOYMENT", ENV["BUNDLE_DEPLOYMENT"], layer if ENV["BUNDLE_DEPLOYMENT"] # Unset on windows since we delete the Gemfile.lock
end
end
# sets up the profile.d script for this buildpack
def setup_profiled(ruby_layer_path: , gem_layer_path: )
instrument 'setup_profiled' do
profiled_path = []
# Rails has a binstub for yarn that doesn't work for all applications
# we need to ensure that yarn comes before local bin dir for that case
if yarn_preinstalled?
profiled_path << yarn_preinstall_bin_path.gsub(File.expand_path("."), "$HOME")
elsif has_yarn_binary?
profiled_path << "#{ruby_layer_path}/vendor/#{@yarn_installer.binary_path}"
end
profiled_path << "$HOME/bin" # /app in production
profiled_path << "#{gem_layer_path}/#{bundler_binstubs_path}" # Binstubs from bundler, eg. vendor/bundle/bin
profiled_path << "#{gem_layer_path}/#{slug_vendor_base}/bin" # Binstubs from rubygems, eg. vendor/bundle/ruby/2.6.0/bin
profiled_path << "$PATH"
set_env_default "LANG", "en_US.UTF-8"
set_env_override "GEM_PATH", "#{gem_layer_path}/#{slug_vendor_base}:$GEM_PATH"
set_env_override "PATH", profiled_path.join(":")
set_env_override "DISABLE_SPRING", "1"
set_env_default "MALLOC_ARENA_MAX", "2" if default_malloc_arena_max?
add_to_profiled set_default_web_concurrency if env("SENSIBLE_DEFAULTS")
# TODO handle JRUBY
if ruby_version.jruby?
add_to_profiled set_jvm_max_heap
add_to_profiled set_java_mem
set_env_default "JAVA_OPTS", default_java_opts
set_env_default "JRUBY_OPTS", default_jruby_opts
end
set_env_default "BUNDLE_PATH", ENV["BUNDLE_PATH"]
set_env_default "BUNDLE_WITHOUT", ENV["BUNDLE_WITHOUT"]
set_env_default "BUNDLE_BIN", ENV["BUNDLE_BIN"]
set_env_default "BUNDLE_GLOBAL_PATH_APPENDS_RUBY_SCOPE", ENV["BUNDLE_GLOBAL_PATH_APPENDS_RUBY_SCOPE"] if bundler.needs_ruby_global_append_path?
set_env_default "BUNDLE_DEPLOYMENT", ENV["BUNDLE_DEPLOYMENT"] if ENV["BUNDLE_DEPLOYMENT"] # Unset on windows since we delete the Gemfile.lock
end
end
def warn_outdated_ruby
return unless defined?(@outdated_version_check)
@warn_outdated ||= begin
@outdated_version_check.join
warn_outdated_minor
warn_outdated_eol
warn_stack_upgrade
true
end
end
def warn_stack_upgrade
return unless defined?(@ruby_download_check)
return unless @ruby_download_check.next_stack(current_stack: stack)
return if @ruby_download_check.exists_on_next_stack?(current_stack: stack)
warn(<<~WARNING)
Your Ruby version is not present on the next stack
You are currently using #{ruby_version.version_for_download} on #{stack} stack.
This version does not exist on #{@ruby_download_check.next_stack(current_stack: stack)}. In order to upgrade your stack you will
need to upgrade to a supported Ruby version.
For a list of supported Ruby versions see:
https://devcenter.heroku.com/articles/ruby-support#supported-runtimes
For a list of the oldest Ruby versions present on a given stack see:
https://devcenter.heroku.com/articles/ruby-support#oldest-available-runtimes
WARNING
end
def warn_outdated_eol
return unless @outdated_version_check.maybe_eol?
if @outdated_version_check.eol?
warn(<<~WARNING)
EOL Ruby Version
You are using a Ruby version that has reached its End of Life (EOL)
We strongly suggest you upgrade to Ruby #{@outdated_version_check.suggest_ruby_eol_version} or later
Your current Ruby version no longer receives security updates from
Ruby Core and may have serious vulnerabilities. While you will continue
to be able to deploy on Heroku with this Ruby version you must upgrade
to a non-EOL version to be eligible to receive support.
Upgrade your Ruby version as soon as possible.
For a list of supported Ruby versions see:
https://devcenter.heroku.com/articles/ruby-support#supported-runtimes
WARNING
else
# Maybe EOL
warn(<<~WARNING)
Potential EOL Ruby Version
You are using a Ruby version that has either reached its End of Life (EOL)
or will reach its End of Life on December 25th of this year.
We suggest you upgrade to Ruby #{@outdated_version_check.suggest_ruby_eol_version} or later
Once a Ruby version becomes EOL, it will no longer receive
security updates from Ruby core and may have serious vulnerabilities.
Please upgrade your Ruby version.
For a list of supported Ruby versions see:
https://devcenter.heroku.com/articles/ruby-support#supported-runtimes
WARNING
end
end
def warn_outdated_minor
return if @outdated_version_check.latest_minor_version?
warn(<<~WARNING)
There is a more recent Ruby version available for you to use:
#{@outdated_version_check.suggested_ruby_minor_version}
The latest version will include security and bug fixes. We always recommend
running the latest version of your minor release.
Please upgrade your Ruby version.
For all available Ruby versions see:
https://devcenter.heroku.com/articles/ruby-support#supported-runtimes
WARNING
end
# install the vendored ruby
# @return [Boolean] true if it installs the vendored ruby and false otherwise
def install_ruby(install_path, build_ruby_path = nil)
instrument 'ruby.install_ruby' do
# Could do a compare operation to avoid re-downloading ruby
return false unless ruby_version
installer = LanguagePack::Installers::RubyInstaller.installer(ruby_version).new(@stack)
@ruby_download_check = LanguagePack::Helpers::DownloadPresence.new(ruby_version.file_name)
@ruby_download_check.call
if ruby_version.build?
installer.fetch_unpack(ruby_version, build_ruby_path, true)
end
installer.install(ruby_version, install_path)
@outdated_version_check = LanguagePack::Helpers::OutdatedRubyVersion.new(
current_ruby_version: ruby_version,
fetcher: installer.fetcher
)
@outdated_version_check.call
@metadata.write("buildpack_ruby_version", ruby_version.version_for_download)
topic "Using Ruby version: #{ruby_version.version_for_download}"
if !ruby_version.set
warn(<<~WARNING)
You have not declared a Ruby version in your Gemfile.
To declare a Ruby version add this line to your Gemfile:
```
ruby "#{LanguagePack::RubyVersion::DEFAULT_VERSION_NUMBER}"
```
For more information see:
https://devcenter.heroku.com/articles/ruby-versions
WARNING
end
if ruby_version.warn_ruby_26_bundler?
warn(<<~WARNING, inline: true)
There is a known bundler bug with your version of Ruby
Your version of Ruby contains a problem with the built-in integration of bundler. If
you encounter a bundler error you need to upgrade your Ruby version. We suggest you upgrade to:
#{@outdated_version_check.suggested_ruby_minor_version}
For more information see:
https://devcenter.heroku.com/articles/bundler-version#known-upgrade-issues
WARNING
end
end
true
rescue LanguagePack::Fetcher::FetchError
if @ruby_download_check.does_not_exist?
message = <<~ERROR
The Ruby version you are trying to install does not exist: #{ruby_version.version_for_download}
ERROR
else
message = <<~ERROR
The Ruby version you are trying to install does not exist on this stack.
You are trying to install #{ruby_version.version_for_download} on #{stack}.
Ruby #{ruby_version.version_for_download} is present on the following stacks:
- #{@ruby_download_check.valid_stack_list.join("\n - ")}
ERROR
if env("CI")
message << <<~ERROR
On Heroku CI you can set your stack in the `app.json`. For example:
```
"stack": "heroku-16"
```
ERROR
end
end
message << <<~ERROR
Heroku recommends you use the latest supported Ruby version listed here:
https://devcenter.heroku.com/articles/ruby-support#supported-runtimes
For more information on syntax for declaring a Ruby version see:
https://devcenter.heroku.com/articles/ruby-versions
ERROR
error message
end
# TODO make this compatible with CNB
def new_app?
@new_app ||= !File.exist?("vendor/heroku")
end
# vendors JVM into the slug for JRuby
def install_jvm(forced = false)
instrument 'ruby.install_jvm' do
if ruby_version.jruby? || forced
@jvm_installer.install(ruby_version.engine_version, forced)
end
end
end
# find the ruby install path for its binstubs during build
# @return [String] resulting path or empty string if ruby is not vendored
def ruby_install_binstub_path(ruby_layer_path = ".")
@ruby_install_binstub_path ||=
if ruby_version.build?
"#{build_ruby_path}/bin"
elsif ruby_version
"#{ruby_layer_path}/#{slug_vendor_ruby}/bin"
else
""
end
end
# setup the environment so we can use the vendored ruby
def setup_ruby_install_env(ruby_layer_path = ".")
instrument 'ruby.setup_ruby_install_env' do
ENV["PATH"] = "#{File.expand_path(ruby_install_binstub_path(ruby_layer_path))}:#{ENV["PATH"]}"
if ruby_version.jruby?
ENV['JAVA_OPTS'] = default_java_opts
end
end
end
# installs vendored gems into the slug
def install_bundler_in_app(bundler_dir)
instrument 'ruby.install_language_pack_gems' do
FileUtils.mkdir_p(bundler_dir)
Dir.chdir(bundler_dir) do |dir|
`cp -R #{bundler.bundler_path}/. .`
end
# write bundler shim, so we can control the version bundler used
# Ruby 2.6.0 started vendoring bundler
write_bundler_shim("vendor/bundle/bin") if ruby_version.vendored_bundler?
end
end
# default set of binaries to install
# @return [Array] resulting list
def binaries
add_node_js_binary + add_yarn_binary
end
# vendors binaries into the slug
def install_binaries
instrument 'ruby.install_binaries' do
binaries.each {|binary| install_binary(binary) }
Dir["bin/*"].each {|path| run("chmod +x #{path}") }
end
end
# vendors individual binary into the slug
# @param [String] name of the binary package from S3.
# Example: https://s3.amazonaws.com/language-pack-ruby/node-0.4.7.tgz, where name is "node-0.4.7"
def install_binary(name)
topic "Installing #{name}"
bin_dir = "bin"
FileUtils.mkdir_p bin_dir
Dir.chdir(bin_dir) do |dir|
if name.match(/^node\-/)
@node_installer.install
# need to set PATH here b/c `node-gyp` can change the CWD, but still depends on executing node.
# the current PATH is relative, but it needs to be absolute for this.
# doing this here also prevents it from being exported during runtime
node_bin_path = File.absolute_path(".")
# this needs to be set after so other binaries in bin/ don't take precedence"
ENV["PATH"] = "#{ENV["PATH"]}:#{node_bin_path}"
elsif name.match(/^yarn\-/)
FileUtils.mkdir_p("../vendor")
Dir.chdir("../vendor") do |vendor_dir|
@yarn_installer.install
yarn_path = File.absolute_path("#{vendor_dir}/#{@yarn_installer.binary_path}")
ENV["PATH"] = "#{yarn_path}:#{ENV["PATH"]}"
end
else
@fetchers[:buildpack].fetch_untar("#{name}.tgz")
end
end
end
# removes a binary from the slug
# @param [String] relative path of the binary on the slug
def uninstall_binary(path)
FileUtils.rm File.join('bin', File.basename(path)), :force => true
end
def load_default_cache?
new_app? && ruby_version.default?
end
# loads a default bundler cache for new apps to speed up initial bundle installs
def load_default_cache
instrument "ruby.load_default_cache" do
if false # load_default_cache?
puts "New app detected loading default bundler cache"
patchlevel = run("ruby -e 'puts RUBY_PATCHLEVEL'").strip
cache_name = "#{LanguagePack::RubyVersion::DEFAULT_VERSION}-p#{patchlevel}-default-cache"
@fetchers[:buildpack].fetch_untar("#{cache_name}.tgz")
end
end
end
# install libyaml into the LP to be referenced for psych compilation
# @param [String] tmpdir to store the libyaml files
def install_libyaml(dir)
return false if stack_not_14_not_16?
instrument 'ruby.install_libyaml' do
FileUtils.mkdir_p dir
Dir.chdir(dir) do
@fetchers[:buildpack].fetch_untar("#{@stack}/#{LIBYAML_PATH}.tgz")
end
end
end
# remove `vendor/bundle` that comes from the git repo
# in case there are native ext.
# users should be using `bundle pack` instead.
# https://github.com/heroku/heroku-buildpack-ruby/issues/21
def remove_vendor_bundle
if File.exists?("vendor/bundle")
warn(<<-WARNING)
Removing `vendor/bundle`.
Checking in `vendor/bundle` is not supported. Please remove this directory
and add it to your .gitignore. To vendor your gems with Bundler, use
`bundle pack` instead.
WARNING
FileUtils.rm_rf("vendor/bundle")
end
end
def bundler_binstubs_path
"vendor/bundle/bin"
end
def bundler_path
@bundler_path ||= "#{slug_vendor_base}/gems/#{bundler.dir_name}"
end
def write_bundler_shim(path)
FileUtils.mkdir_p(path)
shim_path = "#{path}/bundle"
File.open(shim_path, "w") do |file|
file.print <<-BUNDLE
#!/usr/bin/env ruby
require 'rubygems'
version = "#{bundler.version}"
if ARGV.first
str = ARGV.first
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
version = $1
ARGV.shift
end
end
if Gem.respond_to?(:activate_bin_path)
load Gem.activate_bin_path('bundler', 'bundle', version)
else
gem "bundler", version
load Gem.bin_path("bundler", "bundle", version)
end
BUNDLE
end
FileUtils.chmod(0755, shim_path)
end
# runs bundler to install the dependencies
def build_bundler
instrument 'ruby.build_bundler' do
log("bundle") do
if File.exist?("#{Dir.pwd}/.bundle/config")
warn(<<~WARNING, inline: true)
You have the `.bundle/config` file checked into your repository
It contains local state like the location of the installed bundle
as well as configured git local gems, and other settings that should
not be shared between multiple checkouts of a single repo. Please
remove the `.bundle/` folder from your repo and add it to your `.gitignore` file.
https://devcenter.heroku.com/articles/bundler-configuration
WARNING
end
if bundler.windows_gemfile_lock?
log("bundle", "has_windows_gemfile_lock")
File.unlink("Gemfile.lock")
ENV.delete("BUNDLE_DEPLOYMENT")
warn(<<~WARNING, inline: true)
Removing `Gemfile.lock` because it was generated on Windows.
Bundler will do a full resolve so native gems are handled properly.
This may result in unexpected gem versions being used in your app.
In rare occasions Bundler may not be able to resolve your dependencies at all.
https://devcenter.heroku.com/articles/bundler-windows-gemfile
WARNING
end
bundle_command = String.new("")
bundle_command << "BUNDLE_WITHOUT='#{ENV["BUNDLE_WITHOUT"]}' "
bundle_command << "BUNDLE_PATH=#{ENV["BUNDLE_PATH"]} "
bundle_command << "BUNDLE_BIN=#{ENV["BUNDLE_BIN"]} "
bundle_command << "BUNDLE_DEPLOYMENT=#{ENV["BUNDLE_DEPLOYMENT"]} " if ENV["BUNDLE_DEPLOYMENT"] # Unset on windows since we delete the Gemfile.lock
bundle_command << "BUNDLE_GLOBAL_PATH_APPENDS_RUBY_SCOPE=#{ENV["BUNDLE_GLOBAL_PATH_APPENDS_RUBY_SCOPE"]} " if bundler.needs_ruby_global_append_path?
bundle_command << "bundle install -j4"
topic("Installing dependencies using bundler #{bundler.version}")
bundler_output = String.new("")
bundle_time = nil
env_vars = {}
Dir.mktmpdir("libyaml-") do |tmpdir|
libyaml_dir = "#{tmpdir}/#{LIBYAML_PATH}"
install_libyaml(libyaml_dir)
# need to setup compile environment for the psych gem
yaml_include = File.expand_path("#{libyaml_dir}/include").shellescape
yaml_lib = File.expand_path("#{libyaml_dir}/lib").shellescape
pwd = Dir.pwd
bundler_path = "#{pwd}/#{slug_vendor_base}/gems/#{bundler.dir_name}/lib"
# we need to set BUNDLE_CONFIG and BUNDLE_GEMFILE for
# codon since it uses bundler.
env_vars["BUNDLE_GEMFILE"] = "#{pwd}/Gemfile"
env_vars["BUNDLE_CONFIG"] = "#{pwd}/.bundle/config"
env_vars["CPATH"] = noshellescape("#{yaml_include}:$CPATH")
env_vars["CPPATH"] = noshellescape("#{yaml_include}:$CPPATH")
env_vars["LIBRARY_PATH"] = noshellescape("#{yaml_lib}:$LIBRARY_PATH")
env_vars["RUBYOPT"] = syck_hack
env_vars["NOKOGIRI_USE_SYSTEM_LIBRARIES"] = "true"
env_vars["BUNDLE_DISABLE_VERSION_CHECK"] = "true"
env_vars["JAVA_HOME"] = noshellescape("#{pwd}/$JAVA_HOME") if ruby_version.jruby?
env_vars["BUNDLER_LIB_PATH"] = "#{bundler_path}" if ruby_version.ruby_version == "1.8.7"
env_vars["BUNDLE_DISABLE_VERSION_CHECK"] = "true"
puts "Running: #{bundle_command}"
instrument "ruby.bundle_install" do
bundle_time = Benchmark.realtime do
bundler_output << pipe("#{bundle_command} --no-clean", out: "2>&1", env: env_vars, user_env: true)
end
end
end
if $?.success?
puts "Bundle completed (#{"%.2f" % bundle_time}s)"
log "bundle", :status => "success"
puts "Cleaning up the bundler cache."
instrument "ruby.bundle_clean" do
# Only show bundle clean output when not using default cache
if load_default_cache?
run("bundle clean > /dev/null", user_env: true, env: env_vars)
else
pipe("bundle clean", out: "2> /dev/null", user_env: true, env: env_vars)
end
end
@bundler_cache.store
# Keep gem cache out of the slug
FileUtils.rm_rf("#{slug_vendor_base}/cache")
else
mcount "fail.bundle.install"
log "bundle", :status => "failure"
error_message = "Failed to install gems via Bundler."
puts "Bundler Output: #{bundler_output}"
if bundler_output.match(/An error occurred while installing sqlite3/)
mcount "fail.sqlite3"
error_message += <<~ERROR
Detected sqlite3 gem which is not supported on Heroku:
https://devcenter.heroku.com/articles/sqlite3
ERROR
end
if bundler_output.match(/but your Gemfile specified/)
mcount "fail.ruby_version_mismatch"
error_message += <<~ERROR
Detected a mismatch between your Ruby version installed and
Ruby version specified in Gemfile or Gemfile.lock. You can
correct this by running:
$ bundle update --ruby
$ git add Gemfile.lock
$ git commit -m "update ruby version"
If this does not solve the issue please see this documentation:
https://devcenter.heroku.com/articles/ruby-versions#your-ruby-version-is-x-but-your-gemfile-specified-y
ERROR
end
error error_message