Skip to content

Type to search extensions

Meson

v0.7.0

Extension for the Meson build system.

Updated 2 months ago
69k downloads 6 stars Repository ↗

Zed Meson Extension

Muon Version Badge MesonLSP Version Badge license

Extension for the Zed code editor, adding support for the Meson build system by allowing both Muon and MesonLSP to be used as language servers.

Features

  • Syntax Highlighting
  • Formatting
  • Jump-To-Definition
  • Jump-To-Subdir
  • A basic set of diagnostics
  • Basic autocompletion
  • Configurable language server with support for both Muon and MesonLSP
    • muon as the language server (features)
    • mesonlsp as the language server (features)
  • Meson tasks for setup, reconfigure, build, test, clean, and install
  • Build and debug actions for literal executable() targets

Tasks and Debugging

[!NOTE]
For the Meson tasks to show up in Zed's task picker, you need to have a meson.build file open in the focused editor tab.

Open a meson.build file and use Zed's task picker to run the Meson tasks. The extension uses build/ as the build directory. Run Meson: setup build directory once for a new checkout, then use Meson: build all targets (or one of the other provided tasks).

Literal executable declarations such as executable('demo', sources) also get inline build, build-and-run, and debug actions.

Limitations

Build Directory Selection

Zed does not currently expose any way to get extra input from the user when running a task. This means that the Meson extension cannot provide a build directory picker to language extensions.

If you need to use a directory other than build/, you can override the automatically provided tasks by creating your own .zed/tasks.json. For example, the following configuration uses out/:

Example .zed/tasks.json
[
  {
    "label": "Meson: setup build directory",
    "command": "meson",
    "args": ["setup", "out", "$ZED_WORKTREE_ROOT"],
    "cwd": "$ZED_WORKTREE_ROOT",
    "save": "all"
  },
  {
    "label": "Meson: reconfigure",
    "command": "meson",
    "args": ["setup", "--reconfigure", "out", "$ZED_WORKTREE_ROOT"],
    "cwd": "$ZED_WORKTREE_ROOT",
    "save": "all"
  },
  {
    "label": "Meson: build all targets",
    "command": "meson",
    "args": ["compile", "-C", "out"],
    "cwd": "$ZED_WORKTREE_ROOT",
    "save": "all"
  },
  {
    "label": "Meson: test",
    "command": "meson",
    "args": ["test", "-C", "out"],
    "cwd": "$ZED_WORKTREE_ROOT",
    "save": "all"
  },
  {
    "label": "Meson: clean",
    "command": "meson",
    "args": ["compile", "--clean", "-C", "out"],
    "cwd": "$ZED_WORKTREE_ROOT"
  },
  {
    "label": "Meson: install",
    "command": "meson",
    "args": ["install", "-C", "out"],
    "cwd": "$ZED_WORKTREE_ROOT"
  },
  {
    "label": "Meson: build $ZED_CUSTOM_meson_target",
    "command": "meson compile -C out \"./$ZED_RELATIVE_DIR/\"$ZED_CUSTOM_meson_target:executable",
    "env": {
      "ZED_MESON_BUILD_DIR": "$ZED_WORKTREE_ROOT/out",
      "ZED_MESON_COMMAND": "meson",
      "ZED_MESON_DEFINED_IN": "$ZED_FILE",
      "ZED_MESON_TARGET": "$ZED_CUSTOM_meson_target"
    },
    "cwd": "$ZED_WORKTREE_ROOT",
    "save": "all",
    "tags": ["meson-executable"]
  },
  {
    "label": "Meson: build and run $ZED_CUSTOM_meson_target",
    "command": "meson compile -C out \"./$ZED_RELATIVE_DIR/\"$ZED_CUSTOM_meson_target:executable && meson devenv -C out \"./$ZED_RELATIVE_DIR/\"$ZED_CUSTOM_meson_target",
    "cwd": "$ZED_WORKTREE_ROOT",
    "save": "all",
    "tags": ["meson-executable"]
  },
  {
    "label": "Meson: build $ZED_CUSTOM_meson_prefix$ZED_CUSTOM_meson_target",
    "command": "meson compile -C out \"./$ZED_RELATIVE_DIR/\"$ZED_CUSTOM_meson_target:executable",
    "env": {
      "ZED_MESON_BUILD_DIR": "$ZED_WORKTREE_ROOT/out",
      "ZED_MESON_COMMAND": "meson",
      "ZED_MESON_DEFINED_IN": "$ZED_FILE",
      "ZED_MESON_TARGET": "$ZED_CUSTOM_meson_target",
      "ZED_MESON_PREFIX": "$ZED_CUSTOM_meson_prefix"
    },
    "cwd": "$ZED_WORKTREE_ROOT",
    "save": "all",
    "tags": ["meson-executable-prefixed"]
  },
  {
    "label": "Meson: build and run $ZED_CUSTOM_meson_prefix$ZED_CUSTOM_meson_target",
    "command": "meson compile -C out \"./$ZED_RELATIVE_DIR/\"$ZED_CUSTOM_meson_target:executable && meson devenv -C out \"./$ZED_RELATIVE_DIR/\"$ZED_CUSTOM_meson_prefix$ZED_CUSTOM_meson_target",
    "cwd": "$ZED_WORKTREE_ROOT",
    "save": "all",
    "tags": ["meson-executable-prefixed"]
  },
  {
    "label": "Meson: build $ZED_CUSTOM_meson_target.$ZED_CUSTOM_meson_suffix",
    "command": "meson compile -C out \"./$ZED_RELATIVE_DIR/\"$ZED_CUSTOM_meson_target.$ZED_CUSTOM_meson_suffix:executable",
    "env": {
      "ZED_MESON_BUILD_DIR": "$ZED_WORKTREE_ROOT/out",
      "ZED_MESON_COMMAND": "meson",
      "ZED_MESON_DEFINED_IN": "$ZED_FILE",
      "ZED_MESON_TARGET": "$ZED_CUSTOM_meson_target",
      "ZED_MESON_SUFFIX": "$ZED_CUSTOM_meson_suffix"
    },
    "cwd": "$ZED_WORKTREE_ROOT",
    "save": "all",
    "tags": ["meson-executable-suffixed"]
  },
  {
    "label": "Meson: build and run $ZED_CUSTOM_meson_target.$ZED_CUSTOM_meson_suffix",
    "command": "meson compile -C out \"./$ZED_RELATIVE_DIR/\"$ZED_CUSTOM_meson_target.$ZED_CUSTOM_meson_suffix:executable && meson devenv -C out \"./$ZED_RELATIVE_DIR/\"$ZED_CUSTOM_meson_target.$ZED_CUSTOM_meson_suffix",
    "cwd": "$ZED_WORKTREE_ROOT",
    "save": "all",
    "tags": ["meson-executable-suffixed"]
  },
  {
    "label": "Meson: build $ZED_CUSTOM_meson_prefix$ZED_CUSTOM_meson_target.$ZED_CUSTOM_meson_suffix",
    "command": "meson compile -C out \"./$ZED_RELATIVE_DIR/\"$ZED_CUSTOM_meson_target.$ZED_CUSTOM_meson_suffix:executable",
    "env": {
      "ZED_MESON_BUILD_DIR": "$ZED_WORKTREE_ROOT/out",
      "ZED_MESON_COMMAND": "meson",
      "ZED_MESON_DEFINED_IN": "$ZED_FILE",
      "ZED_MESON_TARGET": "$ZED_CUSTOM_meson_target",
      "ZED_MESON_PREFIX": "$ZED_CUSTOM_meson_prefix",
      "ZED_MESON_SUFFIX": "$ZED_CUSTOM_meson_suffix"
    },
    "cwd": "$ZED_WORKTREE_ROOT",
    "save": "all",
    "tags": ["meson-executable-prefixed-suffixed"]
  },
  {
    "label": "Meson: build and run $ZED_CUSTOM_meson_prefix$ZED_CUSTOM_meson_target.$ZED_CUSTOM_meson_suffix",
    "command": "meson compile -C out \"./$ZED_RELATIVE_DIR/\"$ZED_CUSTOM_meson_target.$ZED_CUSTOM_meson_suffix:executable && meson devenv -C out \"./$ZED_RELATIVE_DIR/\"$ZED_CUSTOM_meson_prefix$ZED_CUSTOM_meson_target.$ZED_CUSTOM_meson_suffix",
    "cwd": "$ZED_WORKTREE_ROOT",
    "save": "all",
    "tags": ["meson-executable-prefixed-suffixed"]
  }
]
Automatic Executable Discovery

To get a specific executable to show up in the task picker, first run its build/run/debug action from the inline runnable indicator. Zed then keeps the resolved target task in its recent-task history, making it available in the task picker for the rest of the current project session.

You need to do this each time after reopening the project.

Language Server

Both MesonLSP and Muon's LSP mode are supported by this extension as separate language servers (mesonlsp and muon). Both support automatic installation and updates. The extension first checks if the server binary is available in your PATH. If not, it checks for the latest stable release and downloads the binary for your architecture and operating system, reusing an existing download of that version when available.

[!NOTE]
MesonLSP is the recommended language server because it is actively developed and currently provides the most complete Meson language-server feature set. Muon's LSP mode remains available as an alternative, but it is still in early development. See Muon's LSP feature tracking issue for an overview of the current state.

You can choose which Meson language servers run through Zed's built-in language settings.

Configuration

To choose which Meson language server(s) are used, configure Zed's languages settings in your settings.json:

{
  "languages": {
    "Meson": {
      "language_servers": ["mesonlsp", "!muon"]
    }
  }
}

In language_servers, use ! to disable a server. The current recommended configuration is to use only mesonlsp as shown above, since Muon's LSP mode is still in early development and lacks many features. However, you can also choose to use both servers at the same time.

Formatting with Muon

Muon supports formatting code in LSP mode, but currently it is not possible to configure the format when running in LSP mode.

If you want to change how code is formatted, you need to manually change the Zed formatter settings for Meson to a formatter that you prefer.

If the custom formatter configuration changes the tab size, you should also specify this using the Zed tab size setting.

Acknowledgments

This extension was created by taking parts of the now unmaintained zed-meson extension.

ZedExt An unofficial community project. Not affiliated with Zed Industries.

Data from Zed’s extension registry and GitHub · updated