Skip to content

Commit

Permalink
Remove activation at startup, add helper
Browse files Browse the repository at this point in the history
This removes the gem activation from the startup and adds a
helper for properly activating gems based on the defined
runtime constraints of the vagrant specification at loading
time.
  • Loading branch information
chrisroberts committed Nov 7, 2024
1 parent ff445c2 commit b375e4d
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 13 deletions.
11 changes: 0 additions & 11 deletions bin/vagrant
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,6 @@ if Thread.respond_to?(:report_on_exception=)
Thread.report_on_exception = false
end

# Activate all the runtime dependencies before
# moving on.
begin
Gem::Specification.find_by_name("vagrant").runtime_dependencies.each do |dep|
gem(dep.name, dep.requirement.as_list)
end
rescue Gem::MissingSpecError
$stderr.puts "WARN: Failed to locate vagrant specification for dependency loading"
end

# Split arguments by "--" if its there, we'll recombine them later
argv = ARGV.dup
argv_extra = []
Expand Down Expand Up @@ -117,7 +107,6 @@ end

env = nil
begin
require 'log4r'
require 'vagrant'
require 'vagrant/bundler'
require 'vagrant/cli'
Expand Down
66 changes: 64 additions & 2 deletions lib/vagrant/shared_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ def self.enable_resolv_replace
if ENV["VAGRANT_ENABLE_RESOLV_REPLACE"]
if !ENV["VAGRANT_DISABLE_RESOLV_REPLACE"]
begin
require "resolv-replace"
Vagrant.require "resolv-replace"
true
rescue
false
Expand All @@ -192,7 +192,7 @@ def self.global_logger=(log)
# @return [Logger]
def self.global_logger
if @_global_logger.nil?
require "log4r"
Vagrant.require "log4r"
@_global_logger = Log4r::Logger.new("vagrant::global")
end
@_global_logger
Expand Down Expand Up @@ -226,6 +226,68 @@ def self.default_cli_options
@_default_cli_options.dup
end

# Loads the provided path. If the base of the path
# is a Vagrant runtime dependency, the gem will be
# activated with the proper constraint first.
#
# NOTE: This is currently disabled by default and
# will transition to enabled by default as more
# non-installer based environments are tested.
#
# @return [nil]
def self.require(path)
catch(:activation_complete) do
# If activation is not enabled, don't attempt activation
throw :activation_complete if ENV["VAGRANT_ENABLE_GEM_ACTIVATION"].nil?

# If it's a vagrant path, don't do anything.
throw :activation_complete if path.to_s.start_with?("vagrant/")

# Attempt to fetch the vagrant specification
if @_vagrant_spec.nil?
@_vagrant_activated_dependencies = {}
begin
@_vagrant_spec = Gem::Specification.find_by_name("vagrant")
rescue Gem::MissingSpecError
# If it couldn't be found, print a warning to stderr and bail
if !@_spec_load_failure_warning
$stderr.puts "WARN: Failed to locate vagrant specification for dependency loading"
@_spec_load_failure_warning = true
end

throw :activation_complete
end
end

# Attempt to get the name of the gem by the given path
dep_name = Gem::Specification.find_by_path(path)&.name

# Bail if a dependency name cannot be determined
throw :activation_complete if dep_name.nil?

# Bail if already activated
throw :activation_complete if @_vagrant_activated_dependencies[dep_name]

# Extract the dependency from the runtime dependency list
dependency = @_vagrant_spec.runtime_dependencies.detect do |d|
d.name == dep_name
end

# If the dependency isn't found, bail
throw :activation_complete if dependency.nil?

# Activate the gem
gem(dependency.name, dependency.requirement.as_list)
puts "Activated: #{dependency.name}"
@_vagrant_activated_dependencies[dependency.name] = true
end

# Finally, require the provided path.
::Kernel.require(path)

nil
end

# Check if Vagrant is running in server mode
#
# @return [Boolean]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
allow(comm).to receive(:execute).with(any_args)
allow(machine).to receive(:guest).and_return(guest)
allow(guest).to receive(:capability).with(:shell_expand_guest_path, mount_guest_path).and_return(mount_guest_path)
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with("VAGRANT_DISABLE_SMBMFSYMLINKS").and_return(false)
allow(ENV).to receive(:[]).with("GEM_SKIP").and_return(false)
allow(cap).to receive(:display_mfsymlinks_warning)
Expand Down

0 comments on commit b375e4d

Please sign in to comment.