Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSCode: Code Runner extension is unable to execute the code on git bash terminal?

enter image description here I am trying to run a simple program "primeRange.cpp" using Code Runner extension available for VSCode.

I have selected my default terminal as git bash in VSCode, but when I hit Run on top right corner, it sends the command to bash to run the program using g++ compiler, but I am getting error as no such file or directory, when there exists a directory with the given name.

How can I fix this?

How can I customize which command to hit on bash when I press run on code runner?

I want to set the command as :

cd "c:\\Users\\Tushar\\Desktop\\contests\\Practice" && g++ primeRange.cpp -o primeRange && "c:\\Users\\Tushar\\Desktop\\contests\\Practice\\primeRange"

OR

cd "c:\Users\Tushar\Desktop\contests\Practice" && g++ primeRange.cpp -o primeRange && "c:\Users\Tushar\Desktop\contests\Practice\primeRange"

If I am executing any one of the above command manually on bash then it's working.

So I basically want to know that how to include the executable filename inside the path as:

"c:\Users\Tushar\Desktop\contests\Practice\primeRange"

instead of after the quotes as :

"c:\Users\Tushar\Desktop\contests\Practice\"primeRange

This is my settings.json. enter image description here

I have updated the path in settings.json to:

"code-runner.executorMap": {
        "cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && \"./$fileNameWithoutExt.exe\""
    },

But now I am getting prompt instead of asking for stdin as given below: enter image description here

RESOLVED

Fix 1:

Updating code-runner.executorMap property in settings.json as

"code-runner.executorMap": {
            "cpp": "cd $dirWithoutTrailingSlash && g++ -std=c++11 $fileName -o $fileNameWithoutExt && ./$fileNameWithoutExt"
        }

Fix 2:

Adding another property code-runner.terminalRoot in settings.json as:

"code-runner.terminalRoot": "/"
like image 392
tusharRawat Avatar asked Jul 03 '20 18:07

tusharRawat


1 Answers

I was having the same issue for quite some time and after hours of online loitering, I think I found the solution.

Windows and Unix/Linux have different ways of naming the address. To address a folder named my_codes inside the d drive, windows will have the following:-" D:\my_codes ", while Unix / Linux will have something like this-"/D/my_codes/". The difference worth noticing is use of different slashes( '/' & \ ). Unix considers \ as a delimiter. So the address being fed to bash is not actually the correct address for the bash. And that is why it throws the error "No such file or directory".

Did you notice that just before bash threw you the error "No such file or directory", it removed all the backslashes from the address and presented you 'c:UsersTusharDesktopcontestPractice' ?

luckily this issue has already been resolved by "Code Runner" devs.

All you have to do is include the following in the setting.json file:

"code-runner.terminalRoot":"/",

I hope I was of some help. Happy Coding.

like image 147
ABHIJEET KUMAR Avatar answered Oct 21 '22 02:10

ABHIJEET KUMAR