vscode-code-runner icon indicating copy to clipboard operation
vscode-code-runner copied to clipboard

Support $relativeDir as executorMap parameter

Open Jiia opened this issue 6 years ago • 5 comments

My project is running inside a docker container, and I'd like to create a custom execute command to run the file inside the container. However as there is no parameter for relative path I'm unable to build a command that would be able to locate the file inside the container.

The closest I can get is this:

docker-compose exec -T my-container bash -c 'php -f $fullFileName'

But that doesn't work since the path to the file is completely different inside the container.

Solution Relative path parameter:

docker-compose exec -T my-container bash -c 'php -f /container/path/to/project/$relativeDir/$fileName'

Jiia avatar Nov 12 '19 16:11 Jiia

Managed to work around it by setting fileDirectoryAsCwd to false and using realpath.

"code-runner.executorMap": {
    "php": "docker-compose exec -T --env codeRunnerFile=$(realpath --relative-to=${PWD} $fullFileName) my-container bash -c \"php -d display_errors=1 -d /container/path/toProject/\\${codeRunnerFile}\""
},
"code-runner.fileDirectoryAsCwd": false

Jiia avatar Nov 13 '19 14:11 Jiia

In go, I need to execute go run . to the program. Hope @formulahendry can add this $relativeDir to Code Runner soon. Thanks!

My config now:

    "code-runner.cwd": "",
    "code-runner.fileDirectoryAsCwd": true,
    "code-runner.executorMap": {
        "go": "go run $dir"
    }

When Run Code:

g:\Projects\demo1\cmd>go run "g:\Projects\demo1\cmd\"
CreateFile g:\Projects\demo1\cmd": The filename, directory name, or volume label syntax is incorrect.

I'd like to have this:

go run .

When I change config to this:

    "code-runner.cwd": "",
    "code-runner.fileDirectoryAsCwd": true,
    "code-runner.executorMap": {
        "go": "go run $dirWithoutTrailingSlash"
    }

If I put main.go to the workspace root folder. I'll get this:

G:\Projects\demo1>go run "g:\Projects\demo1"
directory . outside available modules

I'd like to have this:

go run .

If I put main.go to any subfolder, I'll run correctly.

So the best way to have is:

Config like this, but relative path if based on cwd or relative to fileDirectoryAsCwd.

    "code-runner.cwd": "",
    "code-runner.fileDirectoryAsCwd": true,
    "code-runner.executorMap": {
        "go": "go run $relativeDirWithoutTrailingSlash"
    }

I hope to get this:

go run .

doggy8088 avatar Oct 29 '20 16:10 doggy8088

$relativeDir would be a great feature.

benjaminpinto avatar Sep 18 '22 01:09 benjaminpinto

yes, I it is reaaly useful to imlement such vars as is in VS CODE: ${relativeFile} - the current opened file relative to workspaceFolder ${relativeFileDirname} - the current opened file's dirname relative to workspaceFolder

Workaround works, thank you!

palansher avatar Sep 20 '22 20:09 palansher

It seems that the addition of this variable is still a long way off, so I provide a temporary alternative. For those who can use shell commands, one way to get the relative path is to use the realpath command, for example, $relativeDir can be replaced with the following command:

`realpath -s --relative-to=$workspaceRoot $dir`

For example, I use the following configuration to run cmake to generate executable files under build that have the same path structure as the source code:

    "code-runner.executorMap": {
        "cpp": "./build/`realpath -s --relative-to=$workspaceRoot $dir`/$fileNameWithoutExt",
    },

Of course, this does not look elegant, but at least it is an optional solution.

ldeng-ustc avatar Jun 11 '24 08:06 ldeng-ustc