Sublime Forum

Pass args to build command

#1

I’m trying to pass some additional arguments to my build command but they seem to be getting filtered out:

self.window.run_command(
    'build', {
        'build_system': 'Packages/MyBuildSystem/MyBuildSystem.sublime-build',
        'variant': 'Run Tests',
        'test-targets': target_rules
    })

MyBuildSystem has a custom “target” attribute so it calls my own command. When my target command is executed the kwargs is missing the test-targets attribute. Is there a way I can pass additional data to my custom target?

I want to avoid calling my target directly because the .sublime-build file contains file_regexp, syntax, etc.

1 Like

#2

The build command accepts a variant argument to specify the variant inside of the current build system to run, and a select argument to tell it that it should prompt the user for the build system to use. It doesn’t appear to balk at any unrecognized arguments and instead just silently drops them, probably due to it being a command that is provided by the core, if I had to guess.

As such, any extra arguments (including build_system as you outline above, which at least for me seems to do nothing at all) are just silently ignored.

However, the keys that are in the sublime-build file (except for e.g. variant, which is processed for you so that you don’t have to worry about it) are passed as arguments to your custom target command. So I think what you want to do is add extra items to the sublime-build file, and then when you (or the user) invokes the build command, they’ll be passed to your custom target automatically. For example:

{
    "target": "my_custom_build",
    "shell_cmd": "python -u \"$file\"",
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python",

    "test-targets": "target_rules"
}

The command my_custom_build would receive a keyword argument named test-targets with a value of target_rules for you to do with as you please.


Note: if your custom target invokes the exec command behind the scenes you need to be careful not to pass it any arguments that it doesn’t expect. Although it won’t complain directly, the AsyncProcess that it creates gets the arguments that exec was passed, so in the case of a simple proxy (such as outlined below) it will throw an error:

__init__() got an unexpected keyword argument 'test-targets'
[shell_cmd: python -u "/home/tmartin/test.py"]
[dir: /home/tmartin/local/sublime_text_3_sandbox/Data/Packages/User]
[path:/usr/local/bin:/usr/bin:/bin]
[Finished]

Presumably the point of this exercise would be to modify target_rules on a build-by-build or project-by-project basis, as otherwise your target command wouldn’t need the extra option at all.

The post below links to a custom target command example that demonstrates a method by which you can provide your own custom variables to be expanded from within the build process. Using that, you could for example modify the build system to say:

    "test-targets": "$target_rules"

In the case of the example below, you could then set a target_rules setting in the currently active view (or set it via a project setting) and have it be expanded out every time the build executes. Since you’re in control of the build command being used, it could also get this information from somewhere outside of a setting as well if so desired.

4 Likes

#3

target_rules is actually a variable I’m trying to pass, so I can’t embed it into a variant. I managed to work around it by having a variant “Run Custom” that calls my target command. The target command then checks to see if the “test-targets” kwargs is set, if it’s not it prompts the user and then calls it’s self back with the same kwargs but with the appended “test-targets”.

1 Like