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

2

8

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": "/"
Hubbs answered 3/7, 2020 at 18:38 Comment(0)
N
10

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.

Naked answered 5/7, 2020 at 15:15 Comment(1)
Thanks @ABHIJEET KUMAR, "code-runner.terminalRoot":"/" worked.Hubbs
R
0

open settings.json

my settings.json looks like this

{
  "workbench.iconTheme": "material-icon-theme",
  "terminal.integrated.defaultProfile.windows": "Git Bash",
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.cursorBlinking": "smooth",
  "editor.cursorSmoothCaretAnimation": "on",
  "editor.formatOnSave": true,
  "code-runner.runInTerminal": true,
  "editor.fontFamily": "'Cascadia Code', Consolas, 'Courier New', monospace",
  "editor.fontLigatures": true,
  "editor.fontWeight": "normal",
  "editor.wordWrap": "on"
}

now insert this into settings.json

"code-runner.executorMap": {
    "python": "python -u",
    "javascript": "node",
    "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
    "c": "gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    "cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && \"./$fileNameWithoutExt.exe\"",
    "php": "php",
    "perl": "perl",
    "ruby": "ruby",
    "go": "go run",
    "bash": "bash $fileName",
    "dart": "dart run",
    "flutter": "flutter run lib/main.dart"
  },

after inserting it will look like this

{
  "workbench.iconTheme": "material-icon-theme",
  "terminal.integrated.defaultProfile.windows": "Git Bash",
  "editor.defaultFormatter": "esbenp.prettier-vscode",
  "editor.cursorBlinking": "smooth",
  "editor.cursorSmoothCaretAnimation": "on",
  "editor.formatOnSave": true,
  "code-runner.runInTerminal": true,
  "editor.fontFamily": "'Cascadia Code', Consolas, 'Courier New', monospace",
  "editor.fontLigatures": true,
  "editor.fontWeight": "normal",
  "editor.wordWrap": "on",
  "code-runner.executorMap": {
    "python": "python -u",
    "javascript": "node",
    "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
    "c": "gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
    "cpp": "cd $dir && g++ -std=c++11 $fileName -o $fileNameWithoutExt && \"./$fileNameWithoutExt.exe\"",
    "php": "php",
    "perl": "perl",
    "ruby": "ruby",
    "go": "go run",
    "bash": "bash $fileName",
    "dart": "dart run",
    "flutter": "flutter run lib/main.dart"
  }
}
Roots answered 25/8 at 16:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.