Skip to content

Commit d58fe85

Browse files
bors[bot]ehuss
andcommitted
Merge #365
365: On-save check: Only call `cargo metadata` once. r=ehuss a=ehuss Also fixes setting the toolchain when manually configured. Co-authored-by: Eric Huss <[email protected]>
2 parents 9afec89 + ef3b50c commit d58fe85

File tree

3 files changed

+23
-11
lines changed

3 files changed

+23
-11
lines changed

SyntaxCheckPlugin.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,21 @@ def get_rustc_messages(self):
163163
print('Unknown setting for `rust_syntax_checking_method`: %r' % (method,))
164164
return -1
165165

166+
# Try to grab metadata only once. `target` is None since that's what
167+
# we're trying to figure out.
168+
toolchain = settings.get_computed(self.cwd, method, None, 'toolchain')
169+
metadata = util.get_cargo_metadata(self.window, self.cwd, toolchain=toolchain)
170+
if not metadata:
171+
return -1
166172
td = target_detect.TargetDetector(self.window)
167-
targets = td.determine_targets(self.triggered_file_name)
173+
targets = td.determine_targets(self.triggered_file_name, metadata=metadata)
168174
if not targets:
169175
return -1
170176
rc = 0
171177
for (target_src, target_args) in targets:
172178
cmd = settings.get_command(method, command_info, self.cwd, self.cwd,
173179
initial_settings={'target': ' '.join(target_args)},
174-
force_json=True)
180+
force_json=True, metadata=metadata)
175181
self.msg_rel_path = cmd['msg_rel_path']
176182
if (util.get_setting('rust_syntax_checking_include_tests', True) and
177183
semver.match(cmd['rustc_version'], '>=1.23.0')):

rust/cargo_settings.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ def get_merged(self, settings_path, variant, target, key,
353353

354354
def get_command(self, cmd_name, cmd_info,
355355
settings_path, working_dir,
356-
initial_settings={}, force_json=False):
356+
initial_settings={}, force_json=False,
357+
metadata=None):
357358
"""Generates the command arguments for running Cargo.
358359
359360
:param cmd_name: The name of the command, the key used to select a
@@ -367,6 +368,8 @@ def get_command(self, cmd_name, cmd_info,
367368
:keyword initial_settings: Initial settings to inject which override
368369
all other settings.
369370
:keyword force_json: If True, will force JSON output.
371+
:keyword metadata: Output from `get_cargo_metadata`. If None, will run
372+
it manually.
370373
371374
:Returns: A dictionary with the keys:
372375
- `command`: The command to run as a list of strings.
@@ -466,7 +469,8 @@ def expand(s):
466469
#
467470
# Starting in Rust 1.24, all messages and symbols are relative to the
468471
# workspace root instead of the package root.
469-
metadata = util.get_cargo_metadata(self.window, working_dir, toolchain)
472+
if metadata is None:
473+
metadata = util.get_cargo_metadata(self.window, working_dir, toolchain)
470474
if metadata and 'workspace_root' in metadata:
471475
# 'workspace_root' key added in 1.24.
472476
msg_rel_path = metadata['workspace_root']

rust/target_detect.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ class TargetDetector(object):
1515
def __init__(self, window):
1616
self.window = window
1717

18-
def determine_targets(self, file_name):
18+
def determine_targets(self, file_name, metadata=None):
1919
"""Detect the target/filters needed to pass to Cargo to compile
2020
file_name.
2121
Returns list of (target_src_path, target_command_line_args) tuples.
2222
23+
:keyword metadata: Output from `get_cargo_metadata`. If None, will run
24+
it manually.
25+
2326
:raises ProcessTerminatedError: Thread should shut down.
2427
"""
2528
# Try checking for target match in settings.
@@ -28,13 +31,12 @@ def determine_targets(self, file_name):
2831
return result
2932

3033
# Try a heuristic to detect the filename.
31-
result = rust_proc.slurp_json(self.window,
32-
'cargo metadata --no-deps'.split(),
33-
cwd=os.path.dirname(file_name))
34-
if not result:
35-
return []
34+
if metadata is None:
35+
metadata = util.get_cargo_metadata(self.window, os.path.dirname(file_name))
36+
if not metadata:
37+
return []
3638
# Each "workspace" shows up as a separate package.
37-
for package in result[0]['packages']:
39+
for package in metadata['packages']:
3840
root_path = os.path.dirname(package['manifest_path'])
3941
targets = package['targets']
4042
# targets is list of dictionaries:

0 commit comments

Comments
 (0)