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 ¶
- Variables
- func Distros(b []byte) []string
- func Forget()
- func HostAt(mode, route string, nets []*net.IPNet) string
- func HostLoopback(mode string) bool
- func SetVia(via, trying []string) error
- func Up(distro string) bool
- func Via() (via, trying []net.IP)
- type Tool
- func (t Tool) Args(args ...string) []string
- func (t Tool) Command(ctx context.Context, args ...string) *exec.Cmd
- func (t Tool) Env(env []string, names ...string) []string
- func (t Tool) Exe() (string, error)
- func (t Tool) Linux(win string) string
- func (t Tool) Probe(ctx context.Context, args ...string) *exec.Cmd
- func (t Tool) Stop(mark string)
- func (t Tool) Windows(linux string) string
Constants ¶
This section is empty.
Variables ¶
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.
var ErrNotFound = errors.New("not in the distro")
ErrNotFound is In's answer for a command the distro hasn't.
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.
var On = runtime.GOOS == "windows"
On is whether there is WSL to look in: on Windows, or in tests.
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 ¶
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 HostAt ¶ added in v0.1.3100
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
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 ¶
SetVia makes via the vias and trying those tried (Via); none of either removes the file.
func Up ¶ added in v0.1.1080
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 ¶
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 ¶
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
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 ¶
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
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 ¶
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 ¶
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 ¶
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 ¶
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.