* [ruby-core:122235] [Ruby Feature#21361] Set execution file and line
@ 2025-05-22 16:46 kddnewton (Kevin Newton) via ruby-core
2025-05-22 19:53 ` [ruby-core:122236] " Eregon (Benoit Daloze) via ruby-core
2025-06-03 6:57 ` [ruby-core:122390] " ko1 (Koichi Sasada) via ruby-core
0 siblings, 2 replies; 3+ messages in thread
From: kddnewton (Kevin Newton) via ruby-core @ 2025-05-22 16:46 UTC (permalink / raw)
To: ruby-core; +Cc: kddnewton (Kevin Newton)
Issue #21361 has been reported by kddnewton (Kevin Newton).
----------------------------------------
Feature #21361: Set execution file and line
https://bugs.ruby-lang.org/issues/21361
* Author: kddnewton (Kevin Newton)
* Status: Open
----------------------------------------
I'd like to be able to set the execution file and execution line for the purpose of generated Ruby code. My specific use case is the Ruby files that are templated in Prism, but I also believe it would be beneficial for ERB. The functionality I'm looking for effectively mirrors the `#line` macro in C. The goal would be for this to be entirely statically analyzable, so whatever syntax ends up being used would have to be required to be constant. A couple of options for this would be:
* An entirely new syntax construct (`__SOURCE__ 5 "foo.erb"`)
* Re-using existing syntax constructs (`__LINE__ = 5; __FILE__ = "foo.erb"`)
* A magic comment (`# -*- source-line: 5; source-file: foo.erb -*-` or `# source: 5 foo.erb`)
I don't particularly care which one is selected, it's just the end-result that I'm looking for that we can mark the source line and file within the generated code.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
* [ruby-core:122236] [Ruby Feature#21361] Set execution file and line
2025-05-22 16:46 [ruby-core:122235] [Ruby Feature#21361] Set execution file and line kddnewton (Kevin Newton) via ruby-core
@ 2025-05-22 19:53 ` Eregon (Benoit Daloze) via ruby-core
2025-06-03 6:57 ` [ruby-core:122390] " ko1 (Koichi Sasada) via ruby-core
1 sibling, 0 replies; 3+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2025-05-22 19:53 UTC (permalink / raw)
To: ruby-core; +Cc: Eregon (Benoit Daloze)
Issue #21361 has been updated by Eregon (Benoit Daloze).
I'm not sure we want to encourage generating Ruby code (generated code in many cases is pretty bad IMO, e.g. huge methods), and this feature would encourage it implicitly.
Ruby is often expressive and flexible enough that there is no need to eval or to use templates (I know Prism uses templates and generated code a lot, and that's good for Prism, but I think it's rather rare among gems).
I think setting that is very low-level (it feels like adding a part of the C preprocessor to Ruby),
and crucially is a gigantic source of complexity for tooling which makes it not worth it.
For example it means line 5 when you open the generated file in your editor isn't what the backtrace will tell you.
That's pretty bad, because the true code that ran is the generated code, i.e., the only one that can be run with a debugger, etc.
Showing the original file would mislead into thinking Ruby executed that file, but it didn't.
AFAIK what people use when they have this issue is eval with explicit file and line, like:
```ruby
eval <<~RUBY, __FILE__, __LINE__+1
def generated_foo
#{generated_code}
end
RUBY
```
which seems fine enough if used few times and around method definition(s) for efficiency.
If `eval` is not an option one can always add helpful comments in the generated source like `# from template.rb:42`
----------------------------------------
Feature #21361: Set execution file and line
https://bugs.ruby-lang.org/issues/21361#change-113384
* Author: kddnewton (Kevin Newton)
* Status: Open
----------------------------------------
I'd like to be able to set the execution file and execution line for the purpose of generated Ruby code. My specific use case is the Ruby files that are templated in Prism, but I also believe it would be beneficial for ERB. The functionality I'm looking for effectively mirrors the `#line` macro in C. The goal would be for this to be entirely statically analyzable, so whatever syntax ends up being used would have to be required to be constant. A couple of options for this would be:
* An entirely new syntax construct (`__SOURCE__ 5 "foo.erb"`)
* Re-using existing syntax constructs (`__LINE__ = 5; __FILE__ = "foo.erb"`)
* A magic comment (`# -*- source-line: 5; source-file: foo.erb -*-` or `# source: 5 foo.erb`)
I don't particularly care which one is selected, it's just the end-result that I'm looking for that we can mark the source line and file within the generated code.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
* [ruby-core:122390] [Ruby Feature#21361] Set execution file and line
2025-05-22 16:46 [ruby-core:122235] [Ruby Feature#21361] Set execution file and line kddnewton (Kevin Newton) via ruby-core
2025-05-22 19:53 ` [ruby-core:122236] " Eregon (Benoit Daloze) via ruby-core
@ 2025-06-03 6:57 ` ko1 (Koichi Sasada) via ruby-core
1 sibling, 0 replies; 3+ messages in thread
From: ko1 (Koichi Sasada) via ruby-core @ 2025-06-03 6:57 UTC (permalink / raw)
To: ruby-core; +Cc: ko1 (Koichi Sasada)
Issue #21361 has been updated by ko1 (Koichi Sasada).
Note: As you may know, ISeq depends on the fact that one iseq has only one filename, so we need to change many places (we may need to include filename information along with the line number).
----------------------------------------
Feature #21361: Set execution file and line
https://bugs.ruby-lang.org/issues/21361#change-113556
* Author: kddnewton (Kevin Newton)
* Status: Open
----------------------------------------
I'd like to be able to set the execution file and execution line for the purpose of generated Ruby code. My specific use case is the Ruby files that are templated in Prism, but I also believe it would be beneficial for ERB. The functionality I'm looking for effectively mirrors the `#line` macro in C. The goal would be for this to be entirely statically analyzable, so whatever syntax ends up being used would have to be required to be constant. A couple of options for this would be:
* An entirely new syntax construct (`__SOURCE__ 5 "foo.erb"`)
* Re-using existing syntax constructs (`__LINE__ = 5; __FILE__ = "foo.erb"`)
* A magic comment (`# -*- source-line: 5; source-file: foo.erb -*-` or `# source: 5 foo.erb`)
I don't particularly care which one is selected, it's just the end-result that I'm looking for that we can mark the source line and file within the generated code.
--
https://bugs.ruby-lang.org/
______________________________________________
ruby-core mailing list -- ruby-core@ml.ruby-lang.org
To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-06-03 6:58 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-05-22 16:46 [ruby-core:122235] [Ruby Feature#21361] Set execution file and line kddnewton (Kevin Newton) via ruby-core
2025-05-22 19:53 ` [ruby-core:122236] " Eregon (Benoit Daloze) via ruby-core
2025-06-03 6:57 ` [ruby-core:122390] " ko1 (Koichi Sasada) via ruby-core
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).