When I execute Run Code
in selection mode, there is a temp file called tempCodeRunnerFile.go
will appear in the folder. How can I avoid this file appear in the project?

- 2,955
- 2
- 37
- 90
4 Answers
I finally realized that there is a code-runner.ignoreSelection
setting can ignore selection to always run entire file. The default is false
. I have to turn it on manually in my User Settings. In Go, there is always run entire file.
{
"code-runner.ignoreSelection": true
}

- 2,955
- 2
- 37
- 90
This temp file "tempCodeRunnerFile" indicates that you have selected part of the code snippet and run it. And if you are running code in terminal unfortunately it will not be deleted by default.
But the solution in this case could be customizing the code-runner.executorMap
to remove the temporary file automatically after running it.
As example (on Windows, using GitBash and running on terminal) for go
and some other languages, using && rm tempCodeRunnerFile
:
"code-runner.executorMap": {
"go": "go run $fullFileName && rm -f $dirWithoutTrailingSlash\\\\tempCodeRunnerFile.go",
"javascript": "node $fullFileName && rm -f $dirWithoutTrailingSlash\\\\tempCodeRunnerFile.js",
"typescript": "ts-node $fullFileName && rm -f $dirWithoutTrailingSlash\\\\tempCodeRunnerFile.cljs",
"clojure": "lumo $fullFileName && rm -f $dirWithoutTrailingSlash\\\\tempCodeRunnerFile.cljs",
},
Notes:
- the
-f
flag is needed to preventrm: cannot remove 'path/here': No such file or directory
when running the real file (without a selection). - using
$dirWithoutTrailingSlash
is needed because the path is surrounded by quotes and the final slash on windows will escape the quote. - double escaped slashes (
\\\\
) are needed otherwise it will end up as\tempCodeRunnerFile
, and\t
means escapet
. - will not delete the file if code execution fails.
It works but there are a few things to keep in mind when doing it manually.
More information in this github issue. And special thanks for github.com/filipesilva for helping in this solution.

- 491
- 8
- 8
-
1I didn't know about tempCodeRunnerFile this much. Thanks. – JuBaer AD Sep 10 '22 at 14:58
The accepted solution assumes that only saved files will be executed. For unnamed/temporary files, the accepted solution will not resolve the issue per the documentation:
[REPL support] To set whether to run code in Integrated Terminal (only support to run whole file in Integrated Terminal, neither untitled file nor code snippet) (default is false):

- 21
- 2
I wanted to tell you that why do we get this file generated in our folder,
So the basic reason is that when we run selected code using codeRunner
extention than a file is generated called tempCodeRunnerFile.js
or with some different suffix.
. So at that point of time your selected code is copied into this file and the compiler runs this file as a source.

- 1,361
- 1
- 8
- 26