From 7f4ddf9f280012e2af7a43f8a96b310a4d26235a Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 2 Dec 2021 13:45:31 +0100 Subject: [PATCH] Stop depending on Set It is now a gem, and since spring load before bundler if you have multiple `set` versions installed, you might load the wrong one causing already defined constant warnings. --- CHANGELOG.md | 3 +++ lib/spring/application.rb | 12 ++++++------ lib/spring/client/binstub.rb | 2 +- lib/spring/client/rails.rb | 2 +- lib/spring/watcher/abstract.rb | 8 ++++---- lib/spring/watcher/polling.rb | 2 +- test/support/watcher_test.rb | 10 +++++----- 7 files changed, 21 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 88c5898f..55c66435 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## Next Release +* Stop requiring `set` before bundler can select the proper version. This could result in + `already defined constant` warnings during boot (#659). + ## 3.1.1 * Fix compatibility issues with code that raises exceptions with frozen backtraces. diff --git a/lib/spring/application.rb b/lib/spring/application.rb index 5e328f18..9261ac8c 100644 --- a/lib/spring/application.rb +++ b/lib/spring/application.rb @@ -11,8 +11,8 @@ def initialize(manager, original_env, spring_env = Env.new) @original_env = original_env @spring_env = spring_env @mutex = Mutex.new - @waiting = Set.new - @clients = Set.new + @waiting = {} + @clients = {} @preloaded = false @state = :initialized @interrupt = IO.pipe @@ -150,7 +150,7 @@ def serve(client) log "got client" manager.puts - @clients << client + @clients[client] = true _stdout, stderr, _stdin = streams = 3.times.map { client.recv_io } [STDOUT, STDERR, STDIN].zip(streams).each { |a, b| a.reopen(b) } @@ -181,7 +181,7 @@ def serve(client) pid = fork { # Make sure to close other clients otherwise their graceful termination # will be impossible due to reference from this fork. - @clients.select { |c| c != client }.each(&:close) + @clients.each_key { |c| c.close if c != client } Process.setsid IGNORE_SIGNALS.each { |sig| trap(sig, "DEFAULT") } @@ -245,7 +245,7 @@ def terminate if exiting? # Ensure that we do not ignore subsequent termination attempts log "forced exit" - @waiting.each { |pid| Process.kill("TERM", pid) } + @waiting.each_key { |pid| Process.kill("TERM", pid) } Kernel.exit else state! :terminating @@ -337,7 +337,7 @@ def reset_streams end def wait(pid, streams, client) - @mutex.synchronize { @waiting << pid } + @mutex.synchronize { @waiting[pid] = true } # Wait in a separate thread so we can run multiple commands at once Spring.failsafe_thread { diff --git a/lib/spring/client/binstub.rb b/lib/spring/client/binstub.rb index 47b6ac18..fb000d56 100644 --- a/lib/spring/client/binstub.rb +++ b/lib/spring/client/binstub.rb @@ -146,7 +146,7 @@ def initialize(args) @mode = :add @items = args.drop(1) .map { |name| find_commands name } - .inject(Set.new, :|) + .flatten.uniq .map { |command| Item.new(command) } end diff --git a/lib/spring/client/rails.rb b/lib/spring/client/rails.rb index dd497b4e..a241e0ea 100644 --- a/lib/spring/client/rails.rb +++ b/lib/spring/client/rails.rb @@ -3,7 +3,7 @@ module Spring module Client class Rails < Command - COMMANDS = Set.new %w(console runner generate destroy test) + COMMANDS = %w(console runner generate destroy test) ALIASES = { "c" => "console", diff --git a/lib/spring/watcher/abstract.rb b/lib/spring/watcher/abstract.rb index ce6ee95d..27275984 100644 --- a/lib/spring/watcher/abstract.rb +++ b/lib/spring/watcher/abstract.rb @@ -19,8 +19,8 @@ def initialize(root, latency) @root = File.realpath(root) @latency = latency - @files = Set.new - @directories = Set.new + @files = {} + @directories = {} @stale = false @listeners = [] @@ -63,10 +63,10 @@ def add(*items) synchronize { items.each do |item| if item.directory? - directories << item.realpath.to_s + directories[item.realpath.to_s] = true else begin - files << item.realpath.to_s + files[item.realpath.to_s] = true rescue Errno::ENOENT # Race condition. Ignore symlinks whose target was removed # since the check above, or are deeply chained. diff --git a/lib/spring/watcher/polling.rb b/lib/spring/watcher/polling.rb index 3d3312a7..2a8a0666 100644 --- a/lib/spring/watcher/polling.rb +++ b/lib/spring/watcher/polling.rb @@ -91,7 +91,7 @@ def compute_mtime end def expanded_files - files + Dir["{#{directories.map { |d| "#{d}/**/*" }.join(",")}}"] + (files.keys + Dir["{#{directories.keys.map { |d| "#{d}/**/*" }.join(",")}}"]).uniq end end end diff --git a/test/support/watcher_test.rb b/test/support/watcher_test.rb index acde8e3b..177ed847 100644 --- a/test/support/watcher_test.rb +++ b/test/support/watcher_test.rb @@ -149,13 +149,13 @@ def assert_not_stale test "add relative path" do File.write("#{dir}/foo", "foo") watcher.add "foo" - assert_equal ["#{dir}/foo"], watcher.files.to_a + assert_equal ["#{dir}/foo"], watcher.files.keys end test "add dot relative path" do File.write("#{dir}/foo", "foo") watcher.add "./foo" - assert_equal ["#{dir}/foo"], watcher.files.to_a + assert_equal ["#{dir}/foo"], watcher.files.keys end test "add non existent file" do @@ -167,20 +167,20 @@ def assert_not_stale File.write("#{dir}/foo", "foo") File.write("#{dir}/bar", "bar") watcher.add "foo", "bar" - assert_equal ["#{dir}/foo", "#{dir}/bar"], watcher.files.to_a + assert_equal ["#{dir}/foo", "#{dir}/bar"], watcher.files.keys end test "add files as nested array" do File.write("#{dir}/foo", "foo") watcher.add [["foo"]] - assert_equal ["#{dir}/foo"], watcher.files.to_a + assert_equal ["#{dir}/foo"], watcher.files.keys end test "add symlink" do File.write("#{dir}/bar", "bar") File.symlink("#{dir}/bar", "#{dir}/foo") watcher.add './foo' - assert_equal ["#{dir}/bar"], watcher.files.to_a + assert_equal ["#{dir}/bar"], watcher.files.keys end test "add dangling symlink" do