wslrun

package
v0.1.4921 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package wslrun runs, from magpie on Windows, a command that is installed only in a WSL distro: Claude Code signed in to inside Ubuntu, say. The command is found in a distro that is running (a stopped one is never started to look), and run there through wsl.exe, in the distro's own home and login, with what magpie hands it (its config directory, the files it writes, its own executable for a helper) told by their paths inside the distro.

Index

Constants

This section is empty.

Variables

View Source
var ErrDown = errors.New("the distro isn't running")

ErrDown is In's answer for a distro that isn't running, which it doesn't start to look.

View Source
var ErrNotFound = errors.New("not in the distro")

ErrNotFound is In's answer for a command the distro hasn't.

View Source
var Nets = nets

Nets are the networks Windows has an address on that WSL distros reach it through: the IPv4 addresses of its vEthernet (WSL…) adapters (NAT's "vEthernet (WSL)" or "vEthernet (WSL (Hyper-V firewall))", a bridged switch's "vEthernet (WSL_Bridge_Switch)"), there while WSL's VM runs. None off Windows. A var for the tests.

View Source
var On = runtime.GOOS == "windows"

On is whether there is WSL to look in: on Windows, or in tests.

View Source
var Run = func(timeout time.Duration, args ...string) ([]byte, error) {
	ctx, cancel := context.WithTimeout(context.Background(), timeout)
	defer cancel()
	cmd := proc.CommandContext(ctx, "wsl.exe", args...)
	cmd.Env = append(os.Environ(), "WSL_UTF8=1")
	out, err := cmd.Output()

	proc.WSLSaid(out, err)
	return out, err
}

Run runs wsl.exe and returns what it printed; a var for tests.

Functions

func Distros

func Distros(b []byte) []string

Distros reads wsl.exe -l -q: UTF-16LE (with or without a BOM), or UTF-8 under WSL_UTF8. Docker Desktop's own distros are left out.

func Forget

func Forget()

Forget drops what Find and In know, so the next one looks again.

func HostAt added in v0.1.3100

func HostAt(mode, route string, nets []*net.IPNet) string

HostAt is the address a distro in networking mode reaches Windows at, from its default route's next hop (route) and the networks Windows has on WSL's adapters (nets, Nets): "" where its 127.0.0.1 is Windows' (HostLoopback). Under NAT the next hop is Windows' address on vEthernet (WSL) itself, and is kept. Under bridged networking the distro is a peer on the LAN and its next hop is the LAN's router (192.168.9.1, ecalpal_ on Discord), not Windows: Windows is at its own address on the bridge's adapter, the one whose network holds that router. A next hop on no WSL network is kept as it was: NAT's vEthernet (WSL) can be up beside a bridge on another subnet, and is not where a bridged distro reaches Windows.

func HostLoopback added in v0.1.1109

func HostLoopback(mode string) bool

HostLoopback reports whether 127.0.0.1 inside a WSL 2 distro reaches Windows' own 127.0.0.1 in networking mode, as wslinfo --networking-mode prints it: mirrored, and consomme (WSL 2.9's name for what was virtioproxy), whose user-mode stack relays the distro's 127.0.0.1 to Windows' while localhostForwarding is on, as it is unless .wslconfig turns it off (microsoft/WSL's ConsommeTests::LoopbackGuestToHost). Its default route there is Windows' own next hop (198.18.0.2 under a TUN proxy, #1230), never Windows. Under nat, bridged or none, and for a mode not known (""), 127.0.0.1 is the distro's own.

func SetVia

func SetVia(via, trying []string) error

SetVia makes via the vias and trying those tried (Via); none of either removes the file.

func Up added in v0.1.1080

func Up(distro string) bool

Up is whether distro runs now. Anything magpie does in a distro on its own (not the user's asking) asks first: wsl.exe -d, or opening its files through \\wsl.localhost, starts a stopped one (TJHHHH on Discord: WSL kept starting while they repaired it). False off Windows, without WSL, or when wsl.exe can't say.

func Via

func Via() (via, trying []net.IP)

Via are Windows' own addresses off the WSL networks that a distro under NAT reaches Windows at instead of its address on vEthernet (WSL), where a firewall keeps WSL off that one (agent.wslFindVia, gateway.WSLVia), and those being tried for it (listened at while a distro is asked, never written into an agent): kept in magpie's folder (wsl-via), one a line, a tried one after "try ", read again after viaAge. A file that can't be read keeps the last read.

Types

type Tool

type Tool struct {
	Distro string // as wsl.exe -l names it
	Path   string // the command, inside the distro
	PATH   string // the distro's PATH it was found on, which it may need (node)
	Mount  string // where Windows' drives are mounted: "/mnt/"
	Host   string // the Windows host's address inside the distro, "" when it is 127.0.0.1 (HostLoopback)
	Root   bool   // the distro's default user, as whom the command runs, is root
	Home   string // that user's home folder, inside the distro
}

A Tool is a command of a WSL distro's own.

func Find

func Find(name string) (Tool, bool)

Find is the command name of a running WSL distro's own, the default distro first. ok is false off Windows, without WSL, with Settings' Detect agents in WSL off, or when no running distro has it.

func In added in v0.1.1326

func In(distro, name string) (Tool, error)

In is command name, a program's name or its path, as distro has it, for a run there: found as Find finds one (a login shell's PATH, then an interactive one's, where nvm puts node), in that distro only. A distro that isn't running is ErrDown and isn't started. A path the probe can't name for the shell is taken as it is, on a login shell's usual PATH. What was found is kept for findAge.

func Known

func Known(name string) (Tool, bool)

Known is the tool Find found last for name, however long ago, without looking again: for a page to say where it runs without waiting on WSL.

func (Tool) Args added in v0.1.2367

func (t Tool) Args(args ...string) []string

Args is Command's arguments to wsl.exe, without what Command adds to end the run with ctx: for a program that starts wsl.exe itself.

func (Tool) Command

func (t Tool) Command(ctx context.Context, args ...string) *exec.Cmd

Command is the wsl.exe that runs t with args, in the distro, on the PATH it was found on. Nothing of the distro's shell runs: what the run is given of Windows' environment is what Env passes. ctx ending ends the run inside the distro too: killing wsl.exe alone leaves it there (a `claude auth login` would wait on for a browser that never comes).

func (Tool) Env

func (t Tool) Env(env []string, names ...string) []string

Env is env for a run of t: WSLENV names the variables of env that go into the distro — those in names, their values Windows paths when they end in /p (WSL then gives the distro's path for them), and the proxy variables, whose proxy on this machine's loopback is the Windows host's address under NAT.

func (Tool) Exe

func (t Tool) Exe() (string, error)

Exe is magpie's own executable as a run in the distro starts it (a Windows program, which WSL runs on Windows).

func (Tool) Linux

func (t Tool) Linux(win string) string

Linux is where Windows path win is inside the distro: C:\Users\me is /mnt/c/Users/me. A path on no drive letter is given back as it is.

func (Tool) Probe

func (t Tool) Probe(ctx context.Context, args ...string) *exec.Cmd

Probe is Command for a short answer, as proc.ProbeContext runs one.

func (Tool) Stop

func (t Tool) Stop(mark string)

Stop ends the processes of the run marked mark in the distro.

func (Tool) Windows added in v0.1.2367

func (t Tool) Windows(linux string) string

Windows is where path linux inside the distro is on Windows: \\wsl$\<distro>\home\me for /home/me.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL