Skip to content

Commit a11a4f7

Browse files
committed
Add temporary directory handling to improve linter accuracy
When the user modifies a file but doesn’t saves the changes, the linter is unable to report warnings and errors because golangci-lint is still scanning the unmodified version of the code. From now on, the linter will copy the project into a temporary directory, then take the editor buffer as the content for the file that’s being modified. Then execute the command against the temporary folder.
1 parent 6cfe0c7 commit a11a4f7

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

linter.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,27 @@ def _live_lint(self, cmd, code):
3636

3737
def _in_place_lint(self, cmd):
3838
return self.execute(cmd)
39+
40+
def tmpdir(self, cmd, dir, files, filename, code):
41+
"""Run an external executable using a temp dir filled with files and return its output."""
42+
try:
43+
with tempfile.TemporaryDirectory(dir=dir, prefix=".golangcilint-") as tmpdir:
44+
for filepath in files:
45+
target = os.path.join(tmpdir, filepath)
46+
filepath = os.path.join(dir, filepath)
47+
if os.path.basename(target) != os.path.basename(filename):
48+
os.link(filepath, target)
49+
continue
50+
# source file hasn't been saved since change
51+
# so update it from our live buffer for now
52+
with open(target, 'wb') as w:
53+
if isinstance(code, str):
54+
code = code.encode('utf8')
55+
w.write(code)
56+
return self.execute(cmd + [tmpdir])
57+
except FileNotFoundError:
58+
print("golangcilint file not found error on `{}`".format(dir))
59+
return ""
60+
except PermissionError:
61+
print("golangcilint permission error on `{}`".format(dir))
62+
return ""

0 commit comments

Comments
 (0)