Skip to content

Commit

Permalink
feat: automatically call hook-env
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Dec 5, 2024
1 parent 3222e48 commit cbd4ce2
Show file tree
Hide file tree
Showing 17 changed files with 299 additions and 104 deletions.
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ docs/cli
docs/environments.md
docs/public/site.webmanifest
docs/registry.md
src/assets/bash_zsh_support/
tasks.md
tmp
xtasks/fig/src/mise.ts
2 changes: 1 addition & 1 deletion docs/continuous-integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ curl https://mise.run | sh
export PATH="$HOME/.local/bin:$PATH"
mise install # Installs the tools in mise.toml
eval "$(mise activate bash --shims)" # Adds the activated tools to $PATH
eval "$(mise activate bash)" # Adds the activated tools to $PATH
swiftlint {args}
```
12 changes: 10 additions & 2 deletions docs/dev-tools/shims.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
The [beginner's guide](https://dev.to/jdxcode/beginners-guide-to-rtx-ac4), and my [blog post](https://jdx.dev/posts/2024-04-13-shims-how-they-work-in-mise-en-place/) are helpful resources to dive deeper into shims.
:::

::: warning
mise the guidance around shims was changed. Previously, it was recommended to use shims in non-interactive
environments and `mise activate` in interactive environments. However, in recent versions of mise, `mise activate`
monitors PWD so shims should no longer be necessary. That said, there may be esoteric situtations where
shims may be useful so this document is here for reference, however the recommendation is not to use shims anymore.
:::

## Introduction

There are two ways for dev tools to be loaded into your shell: `mise activate` and `shims`.
Expand All @@ -12,9 +19,10 @@ There are two ways for dev tools to be loaded into your shell: `mise activate` a
- The "shims" method uses symlinks to the mise binary that intercept commands and load the appropriate environment

While the `PATH` design of mise works great in most cases, there are some situations where shims are
preferable. One example is when calling mise binaries from an IDE.
preferable. In general, the recommendation is to try using `mise activate` first and only use shims if
you run into issues.

To support this, mise does have a shim dir that can be used. It's located at `~/.local/share/mise/shims`.
To support shims, mise has a shim dir that can be added to PATH. It's located at `~/.local/share/mise/shims`.

```sh
$ mise use -g node@20
Expand Down
25 changes: 17 additions & 8 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ You can verify the installation by running:

## 2. Activate `mise`

Now that `mise` is installed, you can optionally activate it or add its [shims](dev-tools/shims.md) to `PATH`.
Now that `mise` is installed, you activate it which will add the tools to PATH. Once mise is activated
in your shell, it will execute an internal command called `hook-env` which updates environment variables
such as PATH to point to the correct versions of tools defined in that directory. This will run every time
the directory is changed _or_ the prompt is displayed—the latter is necessary if you edit a `mise.toml`
file and _don't_ change directories.

- [`mise activate`](/cli/activate) method updates your environment variable and `PATH` every time your prompt is run to ensure you use the correct versions.
- [Shims](dev-tools/shims.md) are symlinks to the `mise` binary that intercept commands and load the appropriate environment

For interactive shells, `mise activate` is recommended. In non-interactive sessions, like CI/CD, IDEs, and scripts, using `shims` might work best. You can also not use any and call `mise exec/run` directly instead.
See [this guide](dev-tools/shims.md) for more information.
:::info
[Shims](/dev-tools/shims) are no longer necessary with the latest version of mise but remains as an alternative if `mise activate` does not work correctly.
:::

:::info
Activation may be handled automatically if you use fish shell and installed via homebrew. This
Expand All @@ -44,11 +46,18 @@ can be disabled with `set -Ux MISE_FISH_AUTO_ACTIVATE 0`.
::: code-group

```sh [bash]
echo 'eval "$(~/.local/bin/mise activate bash)"' >> ~/.bashrc
# in bash you first need to set BASH_ENV to point to a file that will activate mise every time a new bash session is started.
# You may want to use ~/.profile instead if that's the file you're currently using as your login script.
echo 'export BASH_ENV="$HOME/.bashenv"' >> ~/.bash_profile

echo 'eval "$(~/.local/bin/mise activate bash)"' >> ~/.bashenv

# use ~/.bashrc instead if you only want mise to be used in interactive sessions
```

```sh [zsh]
echo 'eval "$(~/.local/bin/mise activate zsh)"' >> ~/.zshrc
# alternatively use ~/.zshrc if you only want mise to be used in interactive sessions
echo 'eval "$(~/.local/bin/mise activate zsh)"' >> ~/.zshenv
```

```sh [fish]
Expand Down
98 changes: 14 additions & 84 deletions docs/ide-integration.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# IDE Integration

IDEs work better with shims than they do environment variable modifications. The simplest way is
to add the mise shim directory to PATH.

For IntelliJ and VSCode—and likely others, you can modify your default shell's profile
script. Your default shell can be found with:
script to activate mise. Your default shell can be found with:

- macos – `dscl . -read /Users/$USER UserShell`
- linux – `getent passwd $USER | cut -d: -f7`
Expand All @@ -17,99 +14,32 @@ Once you know the right one, modify the appropriate file:
::: code-group

```zsh
# ~/.zprofile
eval "$(mise activate zsh --shims)"
# ~/.zshenv
eval "$(mise activate zsh)"
```

```bash
# in bash you need to edit your login shell so that all new bash sessions will call ~/.bashenv

# ~/.bash_profile or ~/.bash_login or ~/.profile
eval "$(mise activate bash --shims)"
export BASH_ENV="$HOME/.bashenv"

# ~/.bashenv
eval "$(mise activate bash)"
```

```fish
# ~/.config/fish/config.fish
if status is-interactive
mise activate fish | source
else
mise activate fish --shims | source
end
mise activate fish | source
```

:::

If this does not work right, you may have better luck with [shims](/dev-tools/shims).

This assumes that `mise` is on PATH. If it is not, you'll need to use the absolute path (
e.g.: `eval "$($HOME/.local/bin/mise activate zsh)"`).

:::: tip
Conditionally using shims is also possible. Some programs will set a `TERM_PROGRAM` environment
variable, which may be used to determine which activation strategy to use.

Here is an example using VSCode:

::: code-group

```zsh
# ~/.zprofile
if [[ "$TERM_PROGRAM" == "vscode" ]]; then
eval "$($HOME/.local/bin/mise activate zsh --shims)"
else
eval "$($HOME/.local/bin/mise activate zsh)"
fi
```

```bash
# ~/.bash_profile or ~/.bash_login or ~/.profile
if [[ "$TERM_PROGRAM" == "vscode" ]]; then
eval "$($HOME/.local/bin/mise activate bash --shims)"
else
eval "$($HOME/.local/bin/mise activate bash)"
fi
```

:::
::::

This won't work for all of mise's functionality. For example, arbitrary env vars in `[env]` will
only be set
if a shim is executed. For this we need tighter integration with the IDE and a custom plugin. If you
feel
ambitious, take a look at existing direnv extensions for your IDE and see if you can modify it to
work for mise.
Direnv and mise work similarly and there should be a direnv extension that can be used as a starting
point.

## Vim

```vim
" Prepend mise shims to PATH
let $PATH = $HOME . '/.local/share/mise/shims:' . $PATH
```

## Neovim

```lua
-- Prepend mise shims to PATH
vim.env.PATH = vim.env.HOME .. "/.local/share/mise/shims:" .. vim.env.PATH
```

## emacs

- Traditional shims way

```lisp
;; CLI tools installed by Mise
;; See: https://www.emacswiki.org/emacs/ExecPath
(setenv "PATH" (concat (getenv "PATH") ":/home/user/.local/share/mise/shims"))
(setq exec-path (append exec-path '("/home/user/.local/share/mise/shims")))
```

- Use with package [mise.el](https://github.com/liuyinz/mise.el)

```lisp
(require 'mise)
(add-hook 'after-init-hook #'global-mise-mode)
```

## Xcode

Xcode projects can run system commands from script build phases and schemes. Since Xcode sandboxes
Expand All @@ -121,7 +51,7 @@ reads to that file. Then, you can use `mise activate` to activate the tools you
```bash
# -C ensures that Mise loads the configuration from the Mise configuration
# file in the project's root directory.
eval "$($HOME/.local/bin/mise activate -C $SRCROOT bash --shims)"
eval "$($HOME/.local/bin/mise activate -C $SRCROOT bash)"

swiftlint
```
Expand All @@ -146,7 +76,7 @@ Or in the case of node (possibly other languages), it's under "Languages & Frame

## VSCode

While modifying `~/.zprofile` is likely the easiest solution, you can also set
While modifying `~/.zshenv` is likely the easiest solution, you can also set
the tools in `launch.json`:

```json
Expand Down
165 changes: 165 additions & 0 deletions src/assets/bash_zsh_support/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.


This version of the GNU Lesser General Public License incorporates
the terms and conditions of version 3 of the GNU General Public
License, supplemented by the additional permissions listed below.

0. Additional Definitions.

As used herein, "this License" refers to version 3 of the GNU Lesser
General Public License, and the "GNU GPL" refers to version 3 of the GNU
General Public License.

"The Library" refers to a covered work governed by this License,
other than an Application or a Combined Work as defined below.

An "Application" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.

A "Combined Work" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the "Linked
Version".

The "Minimal Corresponding Source" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.

The "Corresponding Application Code" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.

1. Exception to Section 3 of the GNU GPL.

You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.

2. Conveying Modified Versions.

If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:

a) under this License, provided that you make a good faith effort to
ensure that, in the event an Application does not supply the
function or data, the facility still operates, and performs
whatever part of its purpose remains meaningful, or

b) under the GNU GPL, with none of the additional permissions of
this License applicable to that copy.

3. Object Code Incorporating Material from Library Header Files.

The object code form of an Application may incorporate material from
a header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:

a) Give prominent notice with each copy of the object code that the
Library is used in it and that the Library and its use are
covered by this License.

b) Accompany the object code with a copy of the GNU GPL and this license
document.

4. Combined Works.

You may convey a Combined Work under terms of your choice that,
taken together, effectively do not restrict modification of the
portions of the Library contained in the Combined Work and reverse
engineering for debugging such modifications, if you also do each of
the following:

a) Give prominent notice with each copy of the Combined Work that
the Library is used in it and that the Library and its use are
covered by this License.

b) Accompany the Combined Work with a copy of the GNU GPL and this license
document.

c) For a Combined Work that displays copyright notices during
execution, include the copyright notice for the Library among
these notices, as well as a reference directing the user to the
copies of the GNU GPL and this license document.

d) Do one of the following:

0) Convey the Minimal Corresponding Source under the terms of this
License, and the Corresponding Application Code in a form
suitable for, and under terms that permit, the user to
recombine or relink the Application with a modified version of
the Linked Version to produce a modified Combined Work, in the
manner specified by section 6 of the GNU GPL for conveying
Corresponding Source.

1) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (a) uses at run time
a copy of the Library already present on the user's computer
system, and (b) will operate properly with a modified version
of the Library that is interface-compatible with the Linked
Version.

e) Provide Installation Information, but only if you would otherwise
be required to provide such information under section 6 of the
GNU GPL, and only to the extent that such information is
necessary to install and execute a modified version of the
Combined Work produced by recombining or relinking the
Application with a modified version of the Linked Version. (If
you use option 4d0, the Installation Information must accompany
the Minimal Corresponding Source and Corresponding Application
Code. If you use option 4d1, you must provide the Installation
Information in the manner specified by section 6 of the GNU GPL
for conveying Corresponding Source.)

5. Combined Libraries.

You may place library facilities that are a work based on the
Library side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:

a) Accompany the combined library with a copy of the same work based
on the Library, uncombined with any other library facilities,
conveyed under the terms of this License.

b) Give prominent notice with the combined library that part of it
is a work based on the Library, and explaining where to find the
accompanying uncombined form of the same work.

6. Revised Versions of the GNU Lesser General Public License.

The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.

Each version is given a distinguishing version number. If the
Library as you received it specifies that a certain numbered version
of the GNU Lesser General Public License "or any later version"
applies to it, you have the option of following the terms and
conditions either of that published version or of any later version
published by the Free Software Foundation. If the Library as you
received it does not specify a version number of the GNU Lesser
General Public License, you may choose any version of the GNU Lesser
General Public License ever published by the Free Software Foundation.

If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library.
3 changes: 3 additions & 0 deletions src/assets/bash_zsh_support/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# bash_zsh_support

support Zsh function hooks for Bash
Loading

0 comments on commit cbd4ce2

Please sign in to comment.