Sublime Forum

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

#1

I just started learning Rust and I want to use Sublime as my editor. I run Linux.

My steps:

  1. I write a simple Hello world program
  2. Then press Ctrl+B and then choose Rust - Build & Run

I get an error message:

bash: rustc: Command not found

When I open a terminal and type in “env” I can see that my .bashrc correctly exported the Path to "/home/username/.cargo/bin <---- rustc is in there and works from a terminal.

So why the hell doesn’t Sublime just auto import my PATH? This is super annoying. Why would anyone not want his PATH in his programming environment.

Then I tried looking for how to do it manually, so I added this to my settings file:

“additional_path_items”: ["~/home/username/.cargo/bin"],

But I’m getting the same error message. WHY ??? This is so frustrating. I just want the Path to be valid WITHIN Sublime as well. I closed and started Sublime. But still no effect.

Please somebody let me know: HOW CAN I DO THIS?

I don’t want to have some project file or mess around with build systems. I just want Sublime to use my global PATH variable.

(And no I dont start Sublime from the terminal but from a *.desktop file.)

Please help!

0 Likes

#2

My hunch is that you are launching Sublime Text from a different parent process than your terminal. You can launch it from your terminal, in which case it will inherit the environment variables. Otherwise, you should be able to customize the .desktop file that the launcher uses to launch Sublime Text to include the PATH you want.

0 Likes

#3

But I dont want to start Sublime from a terminal.

Is there a settings command or whatever with which I can force Sublime to use a certain Path?

What about “additional_path_items”: ["~/home/username/.cargo/bin"] ?

Why is this not working?

Is it deprecated? Misspelled? …?

0 Likes

#4

It’s “not working” because additional_path_items is not part of Sublime. You need to setup the PATH in either ~/.bash_profile or ~/.zprofile (i.e. a file that runs with your login shell).

1 Like

#5

But isn’t the PATH in my .bashrc already doing that?

What is the difference between .bashrc and .bash_profile ?

I know I’m using the .bash_profile on my Macbook.

But this problem happens on my Linux machine and I dont even have a .bash_profile here, because I use .bashrc

0 Likes

#6
0 Likes

#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