File tree Expand file tree Collapse file tree 2 files changed +38
-8
lines changed
lib/ruby_lsp/ruby_lsp_rails Expand file tree Collapse file tree 2 files changed +38
-8
lines changed Original file line number Diff line number Diff line change @@ -12,10 +12,22 @@ class Server
12
12
VOID = Object . new
13
13
14
14
def initialize
15
- $stdin. sync = true
16
- $stdout. sync = true
17
- $stdin. binmode
18
- $stdout. binmode
15
+ # Grab references to the original pipes so that we can change the default output device further down
16
+ @stdin = $stdin
17
+ @stdout = $stdout
18
+ @stderr = $stderr
19
+ @stdin . sync = true
20
+ @stdout . sync = true
21
+ @stderr . sync = true
22
+ @stdin . binmode
23
+ @stdout . binmode
24
+ @stderr . binmode
25
+
26
+ # # Set the default output device to be $stderr. This means that using `puts` by itself will default to printing
27
+ # # to $stderr and only explicit `$stdout.puts` will go to $stdout. This reduces the chance that output coming
28
+ # # from the Rails app will be accidentally sent to the client
29
+ $> = $stderr
30
+
19
31
@running = true
20
32
end
21
33
@@ -25,18 +37,18 @@ def start
25
37
routes_reloader . execute_unless_loaded if routes_reloader &.respond_to? ( :execute_unless_loaded )
26
38
27
39
initialize_result = { result : { message : "ok" , root : ::Rails . root . to_s } } . to_json
28
- $ stdout. write ( "Content-Length: #{ initialize_result . length } \r \n \r \n #{ initialize_result } " )
40
+ @ stdout. write ( "Content-Length: #{ initialize_result . length } \r \n \r \n #{ initialize_result } " )
29
41
30
42
while @running
31
- headers = $ stdin. gets ( "\r \n \r \n " )
32
- json = $ stdin. read ( headers [ /Content-Length: (\d +)/i , 1 ] . to_i )
43
+ headers = @ stdin. gets ( "\r \n \r \n " )
44
+ json = @ stdin. read ( headers [ /Content-Length: (\d +)/i , 1 ] . to_i )
33
45
34
46
request = JSON . parse ( json , symbolize_names : true )
35
47
response = execute ( request . fetch ( :method ) , request [ :params ] )
36
48
next if response == VOID
37
49
38
50
json_response = response . to_json
39
- $ stdout. write ( "Content-Length: #{ json_response . length } \r \n \r \n #{ json_response } " )
51
+ @ stdout. write ( "Content-Length: #{ json_response . length } \r \n \r \n #{ json_response } " )
40
52
end
41
53
end
42
54
Original file line number Diff line number Diff line change @@ -134,4 +134,22 @@ def <(other)
134
134
assert_equal "GET" , result [ :verb ]
135
135
assert_equal "/users(.:format)" , result [ :path ]
136
136
end
137
+
138
+ test "prints in the Rails application or server are automatically redirected to stderr" do
139
+ server = RubyLsp ::Rails ::Server . new
140
+
141
+ server . instance_eval do
142
+ def resolve_route_info ( requirements )
143
+ puts "Hello"
144
+ super
145
+ end
146
+ end
147
+
148
+ stdout , stderr = capture_subprocess_io do
149
+ server . execute ( "route_info" , { controller : "UsersController" , action : "index" } )
150
+ end
151
+
152
+ assert_empty ( stdout )
153
+ assert_equal ( "Hello\n " , stderr )
154
+ end
137
155
end
You can’t perform that action at this time.
0 commit comments