fix: Smarter toolchain path resolution

This commit is contained in:
FalsePattern 2024-08-21 15:20:58 +02:00
parent 4461df14a0
commit 68d1e60b3e
Signed by: falsepattern
GPG key ID: E930CDEC50C50E23
2 changed files with 81 additions and 2 deletions

View file

@ -17,6 +17,11 @@ Changelog structure reference:
## [Unreleased] ## [Unreleased]
### Fixed
- Project
- Relative paths in zig toolchain configuration would break the entire IDE
## [17.0.0] ## [17.0.0]
### Added ### Added

View file

@ -19,10 +19,15 @@ package com.falsepattern.zigbrains.project.toolchain;
import com.falsepattern.zigbrains.project.openapi.components.ZigProjectSettingsService; import com.falsepattern.zigbrains.project.openapi.components.ZigProjectSettingsService;
import com.falsepattern.zigbrains.zig.environment.ZLSConfig; import com.falsepattern.zigbrains.zig.environment.ZLSConfig;
import com.falsepattern.zigbrains.zig.environment.ZLSConfigProvider; import com.falsepattern.zigbrains.zig.environment.ZLSConfigProvider;
import com.intellij.notification.Notification;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectUtil; import com.intellij.openapi.project.ProjectUtil;
import lombok.val; import lombok.val;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path; import java.nio.file.Path;
public class ToolchainZLSConfigProvider implements ZLSConfigProvider { public class ToolchainZLSConfigProvider implements ZLSConfigProvider {
@ -39,7 +44,76 @@ public class ToolchainZLSConfigProvider implements ZLSConfigProvider {
state.setToolchain(toolchain); state.setToolchain(toolchain);
} }
val projectDir = ProjectUtil.guessProjectDir(project); val projectDir = ProjectUtil.guessProjectDir(project);
val env = toolchain.zig().getEnv(projectDir == null ? Path.of(".") : projectDir.toNioPath()); val oEnv = toolchain.zig().getEnv(projectDir == null ? null : projectDir.toNioPath());
env.ifPresent(e -> builder.zig_exe_path(e.zigExecutable()).zig_lib_path(state.overrideStdPath ? Path.of(state.explicitPathToStd).getParent().toString() : e.libDirectory())); if (oEnv.isEmpty()) {
return;
}
val env = oEnv.get();
Path exe;
try {
exe = Path.of(env.zigExecutable());
} catch (InvalidPathException e) {
Notifications.Bus.notify(new Notification("ZigBrains.Project",
"Invalid zig executable path: " + env.zigExecutable(),
NotificationType.ERROR));
return;
}
if (!exe.isAbsolute()) {
exe = toolchain.getLocation().resolve(exe);
}
if (!Files.exists(exe)) {
Notifications.Bus.notify(new Notification("ZigBrains.Project",
"Zig executable path does not exist: " + env.zigExecutable(),
NotificationType.ERROR));
return;
}
Path lib = null;
override:
if (state.overrideStdPath) {
try {
lib = Path.of(state.explicitPathToStd);
} catch (InvalidPathException e) {
Notifications.Bus.notify(new Notification("ZigBrains.Project",
"Invalid zig standard library path override: " + env.zigExecutable(),
NotificationType.ERROR));
state.overrideStdPath = false;
break override;
}
if (!lib.isAbsolute()) {
lib = toolchain.getLocation().resolve(lib);
}
if (!Files.exists(lib)) {
Notifications.Bus.notify(new Notification("ZigBrains.Project",
"Zig standard library path override does not exist: " + env.zigExecutable(),
NotificationType.ERROR));
lib = null;
state.overrideStdPath = false;
break override;
} else {
state.explicitPathToStd = lib.toString();
}
}
if (lib == null) {
try {
lib = Path.of(env.libDirectory());
} catch (InvalidPathException e) {
Notifications.Bus.notify(new Notification("ZigBrains.Project",
"Invalid zig standard library path: " + env.zigExecutable(),
NotificationType.ERROR));
return;
}
if (!lib.isAbsolute()) {
lib = toolchain.getLocation().resolve(lib);
}
if (!Files.exists(lib)) {
Notifications.Bus.notify(new Notification("ZigBrains.Project",
"Zig standard library path does not exist: " +
env.zigExecutable(), NotificationType.ERROR));
return;
}
}
builder.zig_exe_path(exe.toString());
builder.zig_lib_path(lib.toString());
} }
} }