[Feature request] Enhanced Path Handling and Automatic Directory Management for `executorMap`
Is your feature request related to a problem? Please describe.
I'm always frustrated when I want to have custom output directories not to mess the folder of code files with executables. Especially, when working on C++ projects using the Code Runner extension in Visual Studio Code, I often want to compile my executables into a separate output folder, such as bin or outputs, to keep my project directory organized. However, setting up this workflow has proven difficult due to several issues:
-
Path Formatting Issues: When I try to set a custom output directory using the existing
executorMap, the paths are inconsistently formatted, causing extra double quotes to be inserted. For example, in my current configuration:
"cpp": "cd $dir && g++ $fileName -o $dirWithoutTrailingSlash\\outputs\\$fileNameWithoutExt.exe && $dirWithoutTrailingSlash\\outputs\\$fileNameWithoutExt.exe\""
-
$dirWithoutTrailingSlashis now required the command to work. Otherwise, it looks weird. -
\\are required because I am on the Windows CMD terminal. - I've added
.exebecause I want Windows executables. - And set the output directory to the
outputsfolder, which is in the same folder as the source file. - I've added extra
\"because I wanted it to look cool.
Now it results this:
cd "c:\path\to\source" && g++ myapp.cpp -o "c:\path\to\source"\outputs"\myapp.exe" && "c:\path\to\source"\outputs\myapp.exe"
It works, even if there is an extra " double quote before the \outputs part. But this looks weird. As we don't have an option like $dirWithoutQuotes, we could have a solution like: accepting the part after predefined paths inside quotes, producing "dir\path" instead of just adding quotes, which causes "dir"\path. But this is cumbersome. So, please provide an option to get the path without quotes.
-
Directory Creation Issues: Another challenge is managing the output directory itself. When I try to direct compiled files to a new folder (e.g.,
outputs), the build fails if the folder doesn't already exist. I attempted to usemkdir -p $dirWithoutTrailingSlash/outputsto create the directory, but it doesn't work as expected since the command fails when the directory already exists for the subsequent runs after the first run. Handling directory creation within theexecutorMapis essential to avoid these errors.
Describe the solution you'd like I propose adding several features to make this process more intuitive and reliable:
-
New Path Variables: Introduce new path variables to handle directories and filenames more cleanly:
-
$dirWithoutQuotesor$dirNoQuotes: Provides a directory path without the need for quotes around it. This would prevent issues with mismatched quotes and extra characters in the path. -
$fileDirname: Returns the directory path with a trailing slash, which would simplify handling directory structures. -
$fileBasenameNoExt: The filename without its extension, making it easier to reference compiled executables without manually stripping extensions.
-
Or with an extra plus to this, we can introduce:
- Enhanced JSON Configuration: Provide a more flexible and structured approach for handling custom outputs, directory creation, and commands. A proposed syntax could look like this:
"cpp": {
"outputPath": "outputs", // Defines the directory where executables should be placed
"preLaunchTask": "mkdir -p $dirWithoutTrailingSlash\\$outputPath", // Automatically creates the output folder if it doesn't exist
"command": "g++ $fileName -o $dirWithoutTrailingSlash\\$outputPath\\$fileBasenameNoExt.exe", // Compiles the source code
"commandToRun": "$dirWithoutTrailingSlash\\$outputPath\\$fileBasenameNoExt.exe" // Runs the executable from the output folder
}
outputPath: This option specifies where the compiled files should go, allowing for cleaner output management.
preLaunchCommand: This task ensures the output folder is created before compiling, eliminating the need for manual folder management.
command: This compiles the C++ source file into the specified output folder.
runningCommand: The executable is run directly from the custom output folder, making the whole setup clearer and more maintainable.
And you can make this optional. Otherwise, the old style can work. But if someone wants to configure deeply, can also set up smth like above.
Additional Context These changes would make the Code Runner extension much more flexible for developers working on multi-file projects or those wanting a clean, organized output directory. The new path variables and structured JSON configuration would provide greater control and reduce errors related to path formatting and folder creation, making the development process smoother for C++ and other languages.
This is a bit more explained version of #1172
I made some changes to deal with the quoting and directory creation issues. If you have time to take a look, please let me know if this solves those particular issues for you.
https://github.com/ericchase/fork--vscode-code-runner
I made some changes to deal with the quoting and directory creation issues. If you have time to take a look, please let me know if this solves those particular issues for you.
https://github.com/ericchase/fork--vscode-code-runner
Hi @ericchase — thanks for the fork and the changes so far. I checked the README/CHANGELOG: removing the automatic quoting for placeholders and adding recursive mkdir are great and already solve my two biggest pain points (no more weird quotes and directory creation errors).
Two follow-ups I’m interested in:
(1) do you plan to add extra path variables like $fileBasenameNoExt or $dirWithoutQuotes to simplify output paths, and
(2) any plans to support a structured executor config (outputPath / preLaunchTask / command / commandToRun) to avoid long workarounds in settings?
If you’d like, I can test builds/commands and report back — thanks again!
I haven't planned to add anything more for a few reasons:
- IIRC, the original code relies on a very old node version, making it a bit difficult to add utility
- I wanted to modify the code as little as possible in case the original project gets updated
I personally don't need any more features, but I don't have a problem with working on new ones. Can you do a write up for each of the new features you want with detailed information about what and why, and with some examples?
Oh, your original post has a lot of those details, so I'll read through that for now.
Wow, you're pushing this extension to the limit. It looks like you want to write mini custom build scripts. I've written a full-blown custom build system for my projects, so I don't use one-liners like this in code runner or npm scripts. I can see why you're frustrated.
i'm writing out a todo list for improvements while looking through issues on original repo
feel free to tack on ideas and what not to the thread: https://github.com/ericchase/vsce--code-runner-fork/issues/1
oh cool