Skip to content

Commit cf81124

Browse files
committed
Better fix for checking when we should stop looking for JSON output.
The previous fix caused issues with Clippy.
1 parent e01b25e commit cf81124

File tree

5 files changed

+30
-15
lines changed

5 files changed

+30
-15
lines changed

cargo_build.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -185,18 +185,21 @@ def __init__(self, window, settings, command_info, initial_settings,
185185
self.working_dir = working_dir
186186

187187
def run(self):
188-
cmd_info = self.settings.get_command(self.command_info,
189-
self.settings_path,
190-
self.initial_settings)
191-
if not cmd_info:
188+
cmd = self.settings.get_command(self.command_info,
189+
self.settings_path,
190+
self.initial_settings)
191+
if not cmd:
192192
return
193193
messages.clear_messages(self.window)
194194
p = rust_proc.RustProc()
195195
listener = opanel.OutputListener(self.window, self.working_dir)
196+
print('command_info=%r' % self.command_info)
196197
try:
197-
p.run(self.window, cmd_info['command'],
198+
p.run(self.window, cmd['command'],
198199
self.working_dir, listener,
199-
env=cmd_info['env'])
200+
env=cmd['env'],
201+
decode_json=self.command_info.get('allows_json', False),
202+
json_stop_pattern=self.command_info.get('json_stop_pattern'))
200203
p.wait()
201204
except rust_proc.ProcessTerminatedError:
202205
return

docs/build.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ Setting Name | Default | Description
169169
`allows_release` | False | If True, allows `--release` flag.
170170
`allows_features` | False | If True, allows feature flags.
171171
`allows_json` | False | If True, allows `--message-format=json` flag.
172+
`json_stop_pattern` | None | A regular expression matched against Cargo's output to detect when it should stop looking for JSON messages (used by `cargo run` to stop looking for JSON messages once compilation is finished).
172173
`requires_manifest` | True | If True, the command must be run in a directory with a `Cargo.toml` manifest.
173174
`requires_view_path` | False | If True, then the active view must be a Rust source file, and the path to that file will be passed into Cargo (used mainly by `cargo script`).
174175
`wants_run_args` | False | If True, it will ask for extra args to pass to the executable (after the `--` flag separator).

rust/cargo_settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
'allows_release': True,
4949
'allows_features': True,
5050
'allows_json': True,
51+
'json_stop_pattern': '^\s*Running ',
5152
},
5253
'check': {
5354
'name': 'Check',

rust/rust_proc.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,21 @@ class RustProc(object):
140140
# The thread used for reading output.
141141
_stdout_thread = None
142142

143-
def run(self, window, cmd, cwd, listener, env=None):
143+
def run(self, window, cmd, cwd, listener, env=None,
144+
decode_json=True, json_stop_pattern=None):
144145
"""Run the process.
145146
146147
:param window: Sublime window.
147148
:param cmd: The command to run (list of strings).
148149
:param cwd: The directory where to run the command.
149150
:param listener: `ProcListener` to receive the output.
150151
:param env: Dictionary of environment variables to add.
152+
:param decode_json: If True, will check for lines starting with `{` to
153+
decode as a JSON message.
154+
:param json_stop_pattern: Regular expression used to detect when it
155+
should stop looking for JSON messages. This is used by `cargo
156+
run` so that it does not capture output from the user's program
157+
that might start with an open curly brace.
151158
152159
:raises ProcessTermiantedError: Process was terminated by another
153160
thread.
@@ -157,6 +164,8 @@ def run(self, window, cmd, cwd, listener, env=None):
157164
self.listener = listener
158165
self.start_time = time.time()
159166
self.window = window
167+
self.decode_json = decode_json
168+
self.json_stop_pattern = json_stop_pattern
160169

161170
from . import rust_thread
162171
try:
@@ -267,7 +276,6 @@ def wait(self):
267276
return rc
268277

269278
def _read_stdout(self):
270-
decode_json = True
271279
while True:
272280
line = self.proc.stdout.readline()
273281
if not line:
@@ -280,7 +288,7 @@ def _read_stdout(self):
280288
self.listener.on_error(self,
281289
'[Error decoding UTF-8: %r]' % line)
282290
continue
283-
if decode_json and line.startswith('{'):
291+
if self.decode_json and line.startswith('{'):
284292
try:
285293
result = json.loads(line)
286294
except:
@@ -293,10 +301,11 @@ def _read_stdout(self):
293301
self._cleanup()
294302
raise
295303
else:
296-
if re.match('^\s*Finished', line):
297-
# If using "cargo run", we don't want to capture lines
298-
# starting with open bracket.
299-
decode_json = False
304+
print('Checking pattern %r to line %r' % (self.json_stop_pattern, line))
305+
if self.json_stop_pattern and \
306+
re.match(self.json_stop_pattern, line):
307+
# Stop looking for JSON open curly bracket.
308+
self.decode_json = False
300309
# Sublime always uses \n internally.
301310
line = line.replace('\r\n', '\n')
302311
self.listener.on_data(self, line)

tests/test_cargo_build.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,12 @@ def test_run_with_args(self):
194194

195195
def _test_run_with_args(self, view):
196196
window = view.window()
197+
# Curly braces to ensure it is not captured as JSON.
197198
self._run_build_wait('run',
198-
settings={'extra_run_args': 'this is a test',
199+
settings={'extra_run_args': '{this is a test}',
199200
'target': '--bin bin2'})
200201
output = self._get_build_output(window)
201-
self.assertRegex(output, '(?m)^this is a test$')
202+
self.assertRegex(output, '(?m)^{this is a test}$')
202203

203204
def test_test(self):
204205
"""Test "Test" variant."""

0 commit comments

Comments
 (0)