Search code examples
pythonwindowsvisual-studio-2015environment-variablessublimetext3

How to run SublimeText with visual studio environment enabled


OVERVIEW

Right now I got these 2 programs on my windows taskbar:

  • SublimeText3 target:

    "D:\software\SublimeText 3_x64\sublime_text.exe"
    
  • VS2015 x64 Native Tools Command Prompt target:

    %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"" amd64
    

Goal here is running Sublime Text with vs2015 environment enabled.

  1. One option would be open the vs command prompt and then run sublime text from there, > sublime_text (this is not good one, I want it to be a non-interactive process)
  2. Another option would be modifying somehow the sublimetext symlink target from the taskbar so I could open sublime with vs2015 environment enabled just clicking the icon

QUESTION

How could I acomplish option 2?

NS: I want to get Sublime Text 3 to run vcvarsall.bat properly only once at startup (not at build time on any build system)

ATTEMPTS

My first attempt was trying to understand how bat files executed so I tested some basic batch files:

  • bat1.bat: It opens sublime text succesfully

    sublime_text
    
  • bat2.bat: It opens vs command prompt succesfully and it waits for user to type commands

    cmd /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"" amd64
    
  • bat3.bat: Open vs command prompt but it won't open ST, unless you type exit once the command prompt is shown

    %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"" amd64
    sublime_text
    
  • bat4.bat: Open vs command prompt but it doesn't open ST, same case than bat3.bat

    %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"" amd64 && sublime_text
    
  • bat5.bat: Open vs command prompt but it doesn't open ST, same case than bat{4,3}.bat

    %comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat"" amd64 & sublime_text
    

After these attempts I've decided to read some docs trying to find some hints about cmd but it didn't make any difference.

Another idea was using conemu customized tasks, something like:

{vs2015}: cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" amd64 & sublime_text

and then having a bat file calling conemu like this:

D:\software\ConEmuPack.151205\ConEmu64.exe /single -cmd {vs2015}

the result was +/- what I wanted, but 2 terminals and a new sublime session would be spawned. What I'm looking for is just opening a SublimeText session with the right environment, so I consider this not a good solution, plus, it requires to have conemu installed.

After all those attempts I thought maybe using python to open a command prompt and typing&running "magically" some commands could be helpful but I don't know how to do it.

Conclusion: Till the date the problem remains unsolved...


Solution

  • Aside of the ways that have already been mentioned in the question and the previous answer, another way to go is something akin to what @LinuxDisciple mentioned in their answer; have Sublime internally fire up the batch file once at Sublime start up to do what you need to do.

    An example of this in action is the following plugin. To start with, add the following settings to your user preferences (Preferences > Settings or Preferences > Settings - User). Here I'm using the setup for my own local machine; update the path to suit for your version of Visual Studio:

    "vc_vars_cmd": "C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\vcvarsall.bat",
    "vc_vars_arch": "amd64",
    

    The vc_vars_cmd setting is required by the following plugin, but the vc_vars_arch setting is optional; if it is not given, it defaults to amd64.

    Next, select Tools > Developer > New Plugin from the menu and replace the stub plugin code with the following and save it in the default location that Sublime defaults to as something like set_vc_vars.py:

    import sublime
    import sublime_plugin
    
    from threading import Thread
    from subprocess import Popen, PIPE
    from os import environ
    
    SENTINEL="SUBL_VC_VARS"
    
    def _get_vc_env():
        """
        Run the batch file specified in the vc_vars_cmd setting (with an
        optional architecture type) and return back a dictionary of the
        environment that the batch file sets up.
    
        Returns None if the preference is missing or the batch file fails.
        """
        settings = sublime.load_settings("Preferences.sublime-settings")
        vars_cmd = settings.get("vc_vars_cmd")
        vars_arch = settings.get("vc_vars_arch", "amd64")
    
        if vars_cmd is None:
            print("set_vc_vars: Cannot set Visual Studio Environment")
            print("set_vc_vars: Add 'vc_vars_cmd' setting to settings and restart")
            return None
    
        try:
            # Run the batch, outputting a sentinel value so we can separate out
            # any error messages the batch might generate.
            shell_cmd = "\"{0}\" {1} && echo {2} && set".format(
                vars_cmd, vars_arch, SENTINEL)
    
            output = Popen(shell_cmd, stdout=PIPE, shell=True).stdout.read()
    
            lines = [line.strip() for line in output.decode("utf-8").splitlines()]
            env_lines = lines[lines.index(SENTINEL) + 1:]
        except:
            return None
    
        # Convert from var=value to dictionary key/value pairs. We upper case the
        # keys, since Python does that to the mapping it stores in environ.
        env = {}
        for env_var in env_lines:
            parts = env_var.split("=", maxsplit=1)
            env[parts[0].upper()] = parts[1]
    
        return env
    
    def install_vc_env():
        """
        Try to collect the appropriate Visual Studio environment variables and
        set them into the current environment.
        """
        vc_env = _get_vc_env()
        if vc_env is None:
            print("set_vc_vars: Unable to fetch the Visual Studio Environment")
            return sublime.status_message("Error fetching VS Environment")
    
        # Add newly set environment variables
        for key in vc_env.keys():
            if key not in environ:
                environ[key] = vc_env[key]
    
        # Update existing variables whose values changed.
        for key in environ:
            if key in vc_env and environ[key] != vc_env[key]:
                environ[key] = vc_env[key]
    
        # Set a sentinel variable so we know not to try setting up the path again.
        environ[SENTINEL] = "BOOTSTRAPPED"
        sublime.status_message("VS Environment enabled")
    
    def plugin_loaded():
        if sublime.platform() != "windows":
            return sublime.status_message("VS is not supported on this platform")
    
        # To reload the environment if it changes, restart Sublime.
        if SENTINEL in environ:
            return sublime.status_message("VS Environment already enabled")
    
        # Update in the background so we don't block the UI
        Thread(target=install_vc_env).start()
    

    Once you save the plugin, Sublime will load it and execute the plugin_loaded function, which will do all of the work.You should see the status bar say VS Environment Enabled if it worked.

    If you see Error Fetching VS Environment double check that the path to the batch file is correct (note that you need to double all path separators to make them JSON compliant).

    This operates very similarly to (and is vaguely based on) the Fix Mac Path package, which does something similar to update the path in Sublime on MacOS, where the vagaries of the OS cause GUI applications to have their environment set in a way different than terminal applications.

    The general idea is that the batch file is executed in a sub-process, and then before returning we also execute the set command to get the command interpreter to dump its entire environment to the console, which the plugin captures.

    Once we have that data, we can easily compare it with the current environment to add in any environment variables that the batch file set up but which didn't already exist, and update the values of any environment variables that were changed (e.g. the PATH).

    As a part of this process we also set a new environment variable that tells the plugin that the environment has already been set up, so that if the plugin gets reloaded it doesn't try to run the operation again.

    As such, if you need to change the path to the batch file or the architecture that you want to be set up for, you need to change the preference and restart Sublime.

    This could be made more robust by having it do something like store the current environment before modifying it and then watching the preferences to see if that setting is changed and act accordingly, but presumably it's not often that you would need to modify these settings.

    For testing purposes, I created the following simple C program:

    #include <stdio.h>
    
    int main(int argc, char const *argv[])
    {
        printf("Hello VC World!\n");
    
        return 0;
    }
    

    I also created the following sublime-build file, based loosely off of the C++ Single File.sublime-build that ships with Sublime:

    {
        "shell_cmd": "cl \"${file}\" /Fe\"${file_base_name}\"",
        "working_dir": "${file_path}",
        "selector": "source.c, source.c++",
    
        "variants":
        [
            {
                "name": "Run",
                "shell_cmd": "cl \"${file}\" /Fe\"${file_base_name}\" && \"${file_base_name}\""
            }
        ]
    }
    

    If you attempt to build the sample C file without the plugin, an error is generated about not being able to find cl (unless you have started Sublime from a developer prompt or otherwise already set up the path). With the plugin in place, you get output similar to the following:

    Sample C Program being executed