Skip to content

Commit cd0312e

Browse files
committed
When using Cargo, change cwd to crate root. Fixes #13
1 parent a919f83 commit cd0312e

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

linter.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ def run(self, cmd, code):
5858
linting in the rest of the file.
5959
"""
6060
self.use_cargo = self.get_view_settings().get('use-cargo', False)
61-
self.use_crate_root = self.get_view_settings().get('use-crate-root', False)
61+
self.use_crate_root = self.get_view_settings().get(
62+
'use-crate-root', False)
6263

6364
if self.use_cargo:
6465
current_dir = os.path.dirname(self.filename)
@@ -67,11 +68,17 @@ def run(self, cmd, code):
6768
if self.cargo_config:
6869
self.tempfile_suffix = '-'
6970

70-
return util.communicate(
71-
['cargo', 'build', '--manifest-path', self.cargo_config],
72-
code=None,
73-
output_stream=self.error_stream,
74-
env=self.env)
71+
old_cwd = os.getcwd()
72+
os.chdir(os.path.dirname(self.cargo_config))
73+
try:
74+
return util.communicate(
75+
['cargo', 'build', '--manifest-path',
76+
self.cargo_config],
77+
code=None,
78+
output_stream=self.error_stream,
79+
env=self.env)
80+
finally:
81+
os.chdir(old_cwd)
7582

7683
if self.use_crate_root:
7784
self.crate_root = self.locate_crate_root()
@@ -108,6 +115,10 @@ def split_match(self, match):
108115
When working with a crate root, the working directory is the directory of the
109116
crate root source file.
110117
"""
118+
# if match:
119+
# if os.path.basename(self.filename) != os.path.basename(match.group('file')):
120+
# match = None
121+
111122
matched_file = match.group('file') if match else None
112123

113124
if matched_file:
@@ -163,9 +174,11 @@ def locate_crate_root(self):
163174
crate_root = self.get_view_settings().get('crate-root', None)
164175

165176
if not crate_root:
166-
crate_root = util.find_file(os.path.dirname(self.filename), 'main.rs')
177+
crate_root = util.find_file(
178+
os.path.dirname(self.filename), 'main.rs')
167179

168180
if not crate_root:
169-
crate_root = util.find_file(os.path.dirname(self.filename), 'lib.rs')
181+
crate_root = util.find_file(
182+
os.path.dirname(self.filename), 'lib.rs')
170183

171184
return crate_root

0 commit comments

Comments
 (0)