Skip to content

Commit eba2992

Browse files
committed
fix: clear stale WSL import paths before install
1 parent 0112ab5 commit eba2992

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

apps/WindowsLauncher/internal/launcher/launcher.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,11 +384,27 @@ func (l *Launcher) importFreshRuntime(ctx context.Context, distroName, installPa
384384
return err
385385
}
386386
}
387-
if err := os.MkdirAll(installPath, 0o755); err != nil {
388-
return fmt.Errorf("create distro install dir: %w", err)
387+
if err := prepareFreshImportPath(installPath); err != nil {
388+
return err
389389
}
390390
_, err := l.exec.Run(ctx, BuildWSLImportCommand(distroName, installPath, importPath))
391-
return err
391+
if err != nil {
392+
_, _ = l.exec.Run(ctx, BuildWSLUnregisterCommand(distroName))
393+
_ = os.RemoveAll(installPath)
394+
return err
395+
}
396+
return nil
397+
}
398+
399+
func prepareFreshImportPath(installPath string) error {
400+
parentDir := filepath.Dir(installPath)
401+
if err := os.MkdirAll(parentDir, 0o755); err != nil {
402+
return fmt.Errorf("create distro parent dir: %w", err)
403+
}
404+
if err := os.RemoveAll(installPath); err != nil {
405+
return fmt.Errorf("clear distro install dir: %w", err)
406+
}
407+
return nil
392408
}
393409

394410
func (l *Launcher) applyOverlay(ctx context.Context, distroName string, manifest Manifest) error {

apps/WindowsLauncher/internal/launcher/launcher_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,31 @@ func TestBuildCurlDownloadArgs(t *testing.T) {
168168
}
169169
}
170170

171+
func TestPrepareFreshImportPathRemovesStaleContents(t *testing.T) {
172+
t.Parallel()
173+
174+
root := t.TempDir()
175+
installPath := filepath.Join(root, "wsl", "LoRAPilot")
176+
if err := os.MkdirAll(installPath, 0o755); err != nil {
177+
t.Fatalf("MkdirAll returned error: %v", err)
178+
}
179+
staleFile := filepath.Join(installPath, "ext4.vhdx")
180+
if err := os.WriteFile(staleFile, []byte("stale"), 0o644); err != nil {
181+
t.Fatalf("WriteFile returned error: %v", err)
182+
}
183+
184+
if err := prepareFreshImportPath(installPath); err != nil {
185+
t.Fatalf("prepareFreshImportPath returned error: %v", err)
186+
}
187+
188+
if _, err := os.Stat(staleFile); !os.IsNotExist(err) {
189+
t.Fatalf("stale file still exists, err=%v", err)
190+
}
191+
if _, err := os.Stat(filepath.Dir(installPath)); err != nil {
192+
t.Fatalf("parent dir missing after cleanup: %v", err)
193+
}
194+
}
195+
171196
func TestToWSLPath(t *testing.T) {
172197
t.Parallel()
173198

0 commit comments

Comments
 (0)