From 41e46896051a2cb7569ccfd141a989f89675683d Mon Sep 17 00:00:00 2001 From: SunderB <20426079+SunderB@users.noreply.github.com> Date: Sat, 4 Jul 2020 20:04:25 +0100 Subject: [PATCH 1/4] Include the file path in error messages when running external files Should hopefully fix https://github.com/sonic-pi-net/sonic-pi/issues/2336 --- app/server/ruby/bin/sonic-pi-server.rb | 6 +-- app/server/ruby/lib/sonicpi/lang/core.rb | 7 +++- app/server/ruby/lib/sonicpi/runtime.rb | 50 +++++++++++++++--------- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/app/server/ruby/bin/sonic-pi-server.rb b/app/server/ruby/bin/sonic-pi-server.rb index f2fa488c38..c4c998ab7b 100755 --- a/app/server/ruby/bin/sonic-pi-server.rb +++ b/app/server/ruby/bin/sonic-pi-server.rb @@ -346,7 +346,7 @@ # read in init.rb if exists if File.exists?(init_path) - sp.__spider_eval(File.read(init_path), silent: true) + sp.__spider_eval(File.read(init_path), type: :file, name: init_path, silent: true) else STDOUT.puts "Could not find init.rb file: #{init_path} " end @@ -375,7 +375,7 @@ server.add_method("/run-code") do |args| gui_id = args[0] code = args[1].force_encoding("utf-8") - sp.__spider_eval code + sp.__spider_eval(code, type: :eval, name: "osc-/run-code") end server.add_method("/save-and-run-buffer") do |args| @@ -384,7 +384,7 @@ code = args[2].force_encoding("utf-8") workspace = args[3] sp.__save_buffer(buffer_id, code) - sp.__spider_eval code, {workspace: workspace} + sp.__spider_eval(code, type: :workspace, name: workspace) end server.add_method("/save-buffer") do |args| diff --git a/app/server/ruby/lib/sonicpi/lang/core.rb b/app/server/ruby/lib/sonicpi/lang/core.rb index a5b15f05ae..0a6f504ee3 100644 --- a/app/server/ruby/lib/sonicpi/lang/core.rb +++ b/app/server/ruby/lib/sonicpi/lang/core.rb @@ -463,7 +463,7 @@ def with_swing(*args, &blk) def run_file(path) path = File.expand_path(path.to_s) raise IOError, "Unable to run file - no file found with path: #{path}" unless File.exist?(path) - __spider_eval(File.read(path)) + __spider_eval(File.read(path), type: :file, name: path) end doc name: :run_file, introduced: Version.new(2,11,0), @@ -477,7 +477,7 @@ def run_file(path) run_file \"~/path/to/sonic-pi-code.rb\" #=> will run the contents of this file"] def run_code(code) - __spider_eval(code.to_s) + __spider_eval(code.to_s, type: :eval, name: "run_code") end doc name: :run_code, introduced: Version.new(2,11,0), @@ -4785,6 +4785,9 @@ def assert_similar(a, b, msg=nil) def load_buffer(path) path = File.expand_path(path.to_s) raise IOError, "Unable to load buffer - no file found with path: #{path}" unless File.exist?(path) + + raise RuntimeError, "Unable to retrieve the current workspace. Current job info: #{__current_job_info}" unless __current_job_info[:workspace] + buf = __current_job_info[:workspace] __info "loading #{buf} with #{path}" __replace_buffer(buf, File.read(path)) diff --git a/app/server/ruby/lib/sonicpi/runtime.rb b/app/server/ruby/lib/sonicpi/runtime.rb index 9fa59bcd86..f5c2a8a8a9 100644 --- a/app/server/ruby/lib/sonicpi/runtime.rb +++ b/app/server/ruby/lib/sonicpi/runtime.rb @@ -55,7 +55,6 @@ module RuntimeMethods def load_snippets(path=snippets_path, quiet=false) path = File.expand_path(path) Dir["#{path}/**/*.sps"].each do |p| - lines = File.readlines(p) key = nil completion = "" @@ -315,14 +314,8 @@ def __error(e, m=nil) info = __current_job_info err_msg.gsub(/for #/, '') res = "" - w = info[:workspace] + w = info[:display_name] if line != -1 - - # TODO: Remove this hack when we have projects - w = normalise_buffer_name(w) - w = "buffer " + w - # TODO: end of hack - res = res + "[#{w}, line #{line}]" else res = res + "[#{w}]" @@ -792,10 +785,12 @@ def __spider_eval(code, info={}) firstline = 1 firstline -= code.lines.to_a.take_while{|l| l.include? "#__nosave__"}.count start_t_prom = Promise.new - info[:workspace] = 'eval' unless info[:workspace] + + display_name = get_spider_eval_display_name(info[:type], info[:name]) - info[:workspace].freeze + info[:name].freeze info.freeze + display_name.freeze job_in_thread = nil job = Thread.new do @@ -827,7 +822,7 @@ def __spider_eval(code, info={}) code = PreParser.preparse(code, SonicPi::Lang::Core.vec_fns) job_in_thread = in_thread seed: 0 do - eval(code, nil, info[:workspace], firstline) + eval(code, nil, info[:name], firstline) end __schedule_delayed_blocks_and_messages! rescue Stop => e @@ -841,13 +836,7 @@ def __spider_eval(code, info={}) if line line = line.to_i - # TODO: Remove this hack when we have projects - w = info[:workspace] - w = normalise_buffer_name(w) - w = "buffer #{w}" - # TODO: end of hack - - err_msg = "[#{w}, line #{line}] \n #{message}" + err_msg = "[#{display_name}, line #{line}] \n #{message}" error_line = code.lines.to_a[line - firstline] || "" else line = -1 @@ -1278,6 +1267,31 @@ def beautify_ruby_source(source) res = res.gsub(') ___SONIC_PI_RND_TMP_PLACEHOLDER___ /', ')/') res = res.gsub('] ___SONIC_PI_SQR_TMP_PLACEHOLDER___ /', ']/') end + + def get_spider_eval_display_name(type, name) + case info[:type] + when :workspace + "buffer #{normalise_buffer_name(name)}" + when :file + name + when :eval + normalise_eval_name(name) + end + end + + def normalise_eval_name(name) + if (name.nil?) + return "unnamed eval" + end + + norm = case name + when "osc-/run-code" + "eval" + else + name + end + return norm + end def normalise_buffer_name(name) norm = case name From 4a55445b95964bf2d9a39766c823f21541e60957 Mon Sep 17 00:00:00 2001 From: SunderB <20426079+SunderB@users.noreply.github.com> Date: Sun, 4 Oct 2020 17:00:36 +0100 Subject: [PATCH 2/4] Fix a server-crashing bug introduced by the previous commit --- app/server/ruby/lib/sonicpi/runtime.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/server/ruby/lib/sonicpi/runtime.rb b/app/server/ruby/lib/sonicpi/runtime.rb index f5c2a8a8a9..897ee2fe5e 100644 --- a/app/server/ruby/lib/sonicpi/runtime.rb +++ b/app/server/ruby/lib/sonicpi/runtime.rb @@ -1269,7 +1269,7 @@ def beautify_ruby_source(source) end def get_spider_eval_display_name(type, name) - case info[:type] + case type when :workspace "buffer #{normalise_buffer_name(name)}" when :file From 3e2547b33f511b2314b6ac11f732cdaa8e2a5cc6 Mon Sep 17 00:00:00 2001 From: SunderB <20426079+SunderB@users.noreply.github.com> Date: Sun, 4 Oct 2020 19:15:54 +0100 Subject: [PATCH 3/4] Fix bug which caused the displayed path/name in errors to be blank --- app/server/ruby/lib/sonicpi/runtime.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/server/ruby/lib/sonicpi/runtime.rb b/app/server/ruby/lib/sonicpi/runtime.rb index 897ee2fe5e..7d9b6ff465 100644 --- a/app/server/ruby/lib/sonicpi/runtime.rb +++ b/app/server/ruby/lib/sonicpi/runtime.rb @@ -314,7 +314,7 @@ def __error(e, m=nil) info = __current_job_info err_msg.gsub(/for #/, '') res = "" - w = info[:display_name] + w = get_job_display_name(info[:type], info[:name]) if line != -1 res = res + "[#{w}, line #{line}]" else @@ -785,10 +785,9 @@ def __spider_eval(code, info={}) firstline = 1 firstline -= code.lines.to_a.take_while{|l| l.include? "#__nosave__"}.count start_t_prom = Promise.new - - display_name = get_spider_eval_display_name(info[:type], info[:name]) - info[:name].freeze + display_name = get_job_display_name(info[:type], info[:name]) + info.freeze display_name.freeze @@ -1267,8 +1266,8 @@ def beautify_ruby_source(source) res = res.gsub(') ___SONIC_PI_RND_TMP_PLACEHOLDER___ /', ')/') res = res.gsub('] ___SONIC_PI_SQR_TMP_PLACEHOLDER___ /', ']/') end - - def get_spider_eval_display_name(type, name) + + def get_job_display_name(type, name) case type when :workspace "buffer #{normalise_buffer_name(name)}" From 200fa7779e1e58fd73fef922a34a5fb49804f818 Mon Sep 17 00:00:00 2001 From: SunderB <20426079+SunderB@users.noreply.github.com> Date: Thu, 22 Oct 2020 22:42:19 +0100 Subject: [PATCH 4/4] Server - Fix bug introduced by last commit that caused 'value must be immutable' errors --- app/server/ruby/lib/sonicpi/runtime.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/server/ruby/lib/sonicpi/runtime.rb b/app/server/ruby/lib/sonicpi/runtime.rb index 7d9b6ff465..d90e97edb4 100644 --- a/app/server/ruby/lib/sonicpi/runtime.rb +++ b/app/server/ruby/lib/sonicpi/runtime.rb @@ -788,6 +788,7 @@ def __spider_eval(code, info={}) display_name = get_job_display_name(info[:type], info[:name]) + info[:name].freeze info.freeze display_name.freeze