Sublime Forum

Why doesn't Sublime3 use global PATH variable? And how can I force it to use it?

#7

OK so I created a .bash_profile and added this to it:

if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi

However it still isn’t working. Sublime still cannot use rustc.

Please please please please: How can I force Sublime to just use my PATH. That’s all I want to know.

I want to click on the Sublime icon in my dock and then Sublime starts, has the correct Path variables and I can just start programming.

That’s all I want.

So again: PLEASE Somebody tell me how I can achieve that!

0 Likes

#8

@kli6891: I dont think thats true. The error message also shows this:

[path: /usr/local/bin:/usr/local/sbin:/usr/bin:/var/lib/flatpak/exports/bin:/usr/bin/site_perl:/usr/bin/vendor_perl:/usr/bin/core_perl]

So Sublime has some PATH. Just not the correct one. Shouldn’t I be able to add additional PATHs to this?

0 Likes

#9

If you don’t want to solve the underlying problem, you can always fix it at the closest level to where you are invoking rustc. See https://www.sublimetext.com/docs/3/build_systems.html#exec_options and the env key.

0 Likes

#10

@wbond: So there is really absolutely no way to specify the PATH that Sublime should use? (If I dont want to start it from the command-line, but per *.desktop file) ?

I just want to be sure I understood this correctly. No settings-option I can use or anything similar?

The only way to get Sublime to use my PATH is to start Sublime from the terminal.

Did I understand tis correctly?

0 Likes

#11

I think I’ve told you two different ways to do it. Modify your .desktop file, or tweak the .sublime-build file. Either should work.

The third way is to actually fix your login shell environment, as @kli6891 suggested. Did you log out of your machine and log back in after modifying .bash_profile?

0 Likes

#12

I have added this to my .bash_profile:

if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
export PATH="$HOME/.cargo/bin:$PATH"

and rebooted my system. Sublime still doesn’t take the correct PATH variables.

I don’t want to have a custom build system because then auto-detection isnt going to be working anymore right? And I switch a lot between languages. Unfortunately there’s also no keyboard shortcut for changing build systems. I just want to press Ctrl+B and have the autodetected build system being run.

I guess I’ll have to give up :frowning:

Thanks for your help everybody though!!

0 Likes

#13

If I were to customize a .sublime-build, I would use an override, as discussed at the end of https://www.sublimetext.com/docs/3/packages.html. That way Sublime will only know about your version and nothing in your workflow needs to change.

0 Likes

#14

So just to make sure I understand this.

Sublime comes with a standard Rust sublime build system file? Thats the one I’m using right now correct?
Where is this file located?

Can you please just tell me what to do? What file exactly do I open and what do I change within that file. I am completely and utterly lost.

0 Likes

#15

UPDATE: I just built a custom Rust build system and added the PATH manually.

Is there a way to specify that this new custom build system is to be used when Sublime auto-detects that this is a Rust file?

So that I can have the Build System setting set to “Automatic” and whenever I open a rust file, Sublime automatically uses MY CUSTOM build system instead of the one coming with Sublime?

That would pretty much solve my problem completely!

0 Likes

#16

Sublime ships with a Rust.sublime-build file in the Rust package. Packages that ship with Sublime are stored as sublime-package files (really just a zip file) and are stored alongside the binary to stop you from having the urge to modify them directly, since when Sublime updates they get replaced wholesale.

You can view the contents of the file by using the View Package File command from the command palette and selecting Rust/Rust.sublime-build from the list (enter rust build to filter the list quickly).

The results of that are this:

{
    "cmd": ["rustc", "$file"],
    "selector": "source.rust",
    "file_regex": "^(.*?):([0-9]+):([0-9]+):\\s[0-9]+:[0-9]+\\s(.*)$",
    "osx":
    {
        "path": "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin"
    },

    "variants": [
        {
            "selector": "source.rust",
            "cmd": ["./$file_base_name"],
            "name": "Run",
            "windows":
            {
                "cmd": ["$file_base_name.exe"]
            }
        }
    ]
}

What @wbond was getting at above is that you should follow the directions at the link to create an override. That means you want to:

  1. Use Preferences/Browse Packages... from the menu to open the Packages folder
  2. Create a folder named Rust if one does not already exist
  3. Create a file inside of that folder named Rust.sublime-build with your modified contents

When you create those files/folders, make sure that you get the case correct.

When Sublime loads the Rust package, it’s going to get to the part of the sublime-package file that says that there is a file named Rust.sublime-build. Then it will notice that there’s a file in the Rust folder with that name and load that file instead of the one that’s inside of the sublime-package file, which overrides the file in the package with your version.

So in your case, you should move the file that you created to be in that folder by that name to have it take the place of the existing one. Depending on what you used in your own build file you may or may not want to copy the contents above into your own and modify it as needed, in case you didn’t have a file_regex or what have you.

Note also that the build above has a special osx key that gives the build a specific path to use on that platform; you can create a similar key for your platform if it’s not osx to provide a similar path.

1 Like

#17

@OdatNurd: You my friend are a legend!! :slight_smile:

This is exactly what I needed and the way you explained it totally makes sense. I actually just tried it and it works just fine. I changed the “osx” section to “linux” and added the env PATH.

Thank you so much for going out of your way and explaing this to me. This is just what I needed!

(I switch between different projects and languages a lot, so having the language/build system autodetected is worth a lot to me!)

I will mark this as fixed!

0 Likes

#18

@code Glad to hear you found a solution, and sorry I wasn’t able to help more earlier. I don’t know why your ~/.bash_profile is not being run correctly.

This article https://scriptingosx.com/2017/04/about-bash_profile-and-bashrc-on-macos/ goes into a bit more detail for how it fits together on Mac OS.

Unfortunately, it seems that Mac OS has its own rules that is separate from Linux. (I’m on Linux and thus won’t be able to test.) On a Linux machine, your ~/.bash_profile should work.

All of this is assuming you are using bash as your login shell. I personally use zsh, so I would need to modify ~/.zprofile instead (on Linux; again here Mac OS may or may not have different conventions). If you’re using fish as your login shell, you’d need to follow the convention for fish, etc.


Upon further investigation, it seems that for Mac OS, programs do not automatically inherit the PATH from the login shell (!!!). I just remembered this because I recall Emacs has similar problems.

This answer https://emacs.stackexchange.com/a/20425/117 will help you set the path that is visible to applications (e.g. Sublime Text and Emacs). In my opinion, this is the “proper” solution.

0 Likes

#19

@kli6891

Actually the macOS version of Sublime works just fine.
The problems I’m having are all LINUX based. I created a .bash_profile and had it export the PATH to the .cargo/bin directory, but it is being completely ignored by Sublime. That’s why it cannot find rustc.

0 Likes

#20

For anyone coming in, this worked for me. and I am using multiple different versions of python. I havent tested which one it uses (pretty sure the current active one in pyenv) but it works.

if [ -f ~/.zshrc ]; then
    . ~/.zshrc
fi

PATH=/Users/brolafski/.pyenv/shims:$PATH

Im glad I didnt have to change any other sublime setting tbh, I dont want to sync up yet another folder of sublime settings or package onto another machine everytime I set up a new env.
…and unfortunately with every new client that only accepts code from a specific device in their network I do have to set up a new env quite frequently.

Id rather edit the lowest point in my user config files and let all the apps use that rather than set up each apps configs individually.

0 Likes

#21

The issue is one I’ve run across frequently. The problem is that the GUI (Gnome, KDE, etc.) is equivalent to a login shell. In the olden days you logged in on a terminal and that was your environment. Now the GUI performs a login. The shells you run in the terminals have their own environment. X is the underlying graphical environment so you want to modify the environment of your X session. For my Ubuntu the display manager/X session manager reads ~/.profile when you log in. This will also be used by bash when bash is run as a login shell only. I’ve gotten in the habit of setting my path in ~/.profile then putting anything else in the appropriate .zshrc or .bashrc file. The *rc files are read when the shell is interactive. This will make the path available to all apps run in your X session (including Sublime no matter how it’s launched) and includes your shells running in a Terminal.

I know it’s an older thread but this does answer your question of why it’s not working when you modify .bash_profile.

2 Likes

#22

I faced nearly the same problem trying to setup GoGuru and other packages for golang development. I’m using Mac and /bin/zsh and any changes to .bash_profile and .bashrc had no effect for sublime text.

Finally, I’ve found this package https://packagecontrol.io/packages/Environment%20Settings which allows to execute shell script from env_file which sets environment variable. I think this may be the third way to solve your problem

0 Likes

#23

That shouldn’t be necessary in recent versions unless you have a very slow bashrc, or don’t have zsh set as your login shell. A copy of your console output would help diagnose.

0 Likes

#24

I also have a very similar problem, in my case I do not get any output error from the ST3 console. In the console I get
Running pandoc …
with the whole command I set in my build system. In the build console I don’t get any error, just a print of the varibles…I have absolutely no idea how to solve this issue

I made a post about it

0 Likes

#25

found a solution

0 Likes

#26

Simplest solution: Move your path modifications (export PATH=/bla/bla:$PATH) from ~/.bashrc to ~/.profile. Restart system. Related info.

0 Likes