Huy Truong Le
Huy Truong Le

Reputation: 15

Sublime Text build system not working when put in variants

I have this user build system C Terminal.sublime-build to compile and run c programs:

{
    "cmd": ["gcc.exe", "-std=c11", "-o", "$file_base_name", "$file", "&&", "start", "cmd", "/c", "$file_base_name & echo. & echo. & pause"],
    "shell": true,
    "selector": "source.c"
}

I use it to compile and run some of my C single files and it works perfectly.

However, when I put it into the default C Single File.sublime-build as a variant like this:

{
    "shell_cmd": "gcc \"${file}\" -o \"${file_path}/${file_base_name}\"",
    "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
    "working_dir": "${file_path}",
    "selector": "source.c",

    "variants":
    [
        {
            "name": "Run",
            "shell_cmd": "gcc \"${file}\" -o \"${file_path}/${file_base_name}\" && \"${file_path}/${file_base_name}\""
        },

        {
            "name": "Terminal",
            "cmd": ["gcc.exe", "-std=c11", "-o", "$file_base_name", "$file", "&&", "start", "cmd", "/c", "$file_base_name & echo. & echo. & pause"],
            "shell": true
        }
    ]
}

When I select the C Single File - Terminal build system and build, the terminal doesn't pop up and it says [Finished in 0.3s] in the Sublime output.

Does anyone know do I get the build system variant to work?

Upvotes: 0

Views: 725

Answers (1)

OdatNurd
OdatNurd

Reputation: 22791

This doesn't work for you because the main build (and the other variant) use shell_cmd to specify what is to be executed, but that variant uses cmd instead. You shouldn't mix those in a build.

Variants apply their keys on top of the base build when they're selected, which allows them to override the top level build keys and change the build. The main build uses shell_cmd, but the Terminal variant uses cmd.

When the variant keys are applied, that makes the build have both a shell_cmd and a cmd in the build, and internally the exec command favours shell_cmd over cmd, which means that for your variant it's actually still executing the top level build.

This is visible in the console. After you execute your build, if you check in the Sublime console (View > Show Console) you can see a log that shows you what command it executed. It's actually executing the top level build, which is compiling and nothing else.

To solve the problem you need to either adjust the variant to also use shell_cmd instead, or inside of the variant add "shell_cmd": null to override it, which will cause it to use cmd instead.

Upvotes: 1

Related Questions