Browse Source

Fix handling of editorPaths (#656)

David Kincaid 1 năm trước cách đây
mục cha
commit
f55d36e86c

+ 10 - 8
src/debugger/godot3/server_controller.ts

@@ -8,7 +8,7 @@ import { RawObject } from "./variables/variants";
 import { GodotStackFrame, GodotStackVars } from "../debug_runtime";
 import { GodotDebugSession } from "./debug_session";
 import { parse_next_scene_node, split_buffers, build_sub_values } from "./helpers";
-import { get_configuration, get_free_port, createLogger, verify_godot_version, get_project_version, clean_godot_path } from "../../utils";
+import { get_configuration, get_free_port, createLogger, verify_godot_version, get_project_version } from "../../utils";
 import { prompt_for_godot_executable } from "../../utils/prompts";
 import { subProcess, killSubProcesses } from "../../utils/subspawn";
 import { LaunchRequestArguments, AttachRequestArguments, pinnedScene } from "../debugger";
@@ -107,12 +107,11 @@ export class ServerController {
 		if (args.editor_path) {
 			log.info("Using 'editor_path' variable from launch.json");
 
-			godotPath = clean_godot_path(args.editor_path);
-
-			log.info(`Verifying version of '${godotPath}'`);
-			result = verify_godot_version(godotPath, "3");
-
+			log.info(`Verifying version of '${args.editor_path}'`);
+			result = verify_godot_version(args.editor_path, "3");
+			godotPath = result.godotPath;
 			log.info(`Verification result: ${result.status}, version: "${result.version}"`);
+
 			switch (result.status) {
 				case "WRONG_VERSION": {
 					const projectVersion = await get_project_version();
@@ -129,16 +128,19 @@ export class ServerController {
 					this.abort();
 					return;
 				}
+				default: {
+					break;
+				}
 			}
 		} else {
 			log.info("Using 'editorPath.godot3' from settings");
 
 			const settingName = "editorPath.godot3";
-			godotPath = clean_godot_path(get_configuration(settingName));
+			godotPath = get_configuration(settingName);
 
 			log.info(`Verifying version of '${godotPath}'`);
 			result = verify_godot_version(godotPath, "3");
-
+			godotPath = result.godotPath;
 			log.info(`Verification result: ${result.status}, version: "${result.version}"`);
 
 			switch (result.status) {

+ 10 - 8
src/debugger/godot4/server_controller.ts

@@ -8,7 +8,7 @@ import { RawObject } from "./variables/variants";
 import { GodotStackFrame, GodotVariable, GodotStackVars } from "../debug_runtime";
 import { GodotDebugSession } from "./debug_session";
 import { parse_next_scene_node, split_buffers, build_sub_values } from "./helpers";
-import { get_configuration, get_free_port, createLogger, verify_godot_version, get_project_version, clean_godot_path } from "../../utils";
+import { get_configuration, get_free_port, createLogger, verify_godot_version, get_project_version } from "../../utils";
 import { prompt_for_godot_executable } from "../../utils/prompts";
 import { subProcess, killSubProcesses } from "../../utils/subspawn";
 import { LaunchRequestArguments, AttachRequestArguments, pinnedScene } from "../debugger";
@@ -108,12 +108,11 @@ export class ServerController {
 		if (args.editor_path) {
 			log.info("Using 'editor_path' variable from launch.json");
 
-			godotPath = clean_godot_path(args.editor_path);
-
-			log.info(`Verifying version of '${godotPath}'`);
-			result = verify_godot_version(godotPath, "4");
-
+			log.info(`Verifying version of '${args.editor_path}'`);
+			result = verify_godot_version(args.editor_path, "4");
+			godotPath = result.godotPath;
 			log.info(`Verification result: ${result.status}, version: "${result.version}"`);
+
 			switch (result.status) {
 				case "WRONG_VERSION": {
 					const projectVersion = await get_project_version();
@@ -130,16 +129,19 @@ export class ServerController {
 					this.abort();
 					return;
 				}
+				default: {
+					break;
+				}
 			}
 		} else {
 			log.info("Using 'editorPath.godot4' from settings");
 
 			const settingName = "editorPath.godot4";
-			godotPath = clean_godot_path(get_configuration(settingName));
+			godotPath = get_configuration(settingName);
 
 			log.info(`Verifying version of '${godotPath}'`);
 			result = verify_godot_version(godotPath, "4");
-
+			godotPath = result.godotPath;
 			log.info(`Verification result: ${result.status}, version: "${result.version}"`);
 
 			switch (result.status) {

+ 4 - 4
src/extension.ts

@@ -82,8 +82,8 @@ export function activate(context: vscode.ExtensionContext) {
 async function initial_setup() {
 	const projectVersion = await get_project_version();
 	const settingName = `editorPath.godot${projectVersion[0]}`;
-	const godotPath = clean_godot_path(get_configuration(settingName));
-	const result = verify_godot_version(godotPath, projectVersion[0]);
+	const result = verify_godot_version(get_configuration(settingName), projectVersion[0]);
+	const godotPath = result.godotPath;
 
 	switch (result.status) {
 		case "SUCCESS": {
@@ -153,8 +153,8 @@ async function open_workspace_with_editor() {
 	const projectVersion = await get_project_version();
 
 	const settingName = `editorPath.godot${projectVersion[0]}`;
-	const godotPath = clean_godot_path(get_configuration(settingName));
-	const result = verify_godot_version(godotPath, projectVersion[0]);
+	const result = verify_godot_version(get_configuration(settingName), projectVersion[0]);
+	const godotPath = result.godotPath;
 
 	switch (result.status) {
 		case "SUCCESS": {

+ 3 - 2
src/lsp/ClientConnectionManager.ts

@@ -10,7 +10,6 @@ import {
 	set_configuration,
 	createLogger,
 	verify_godot_version,
-	clean_godot_path,
 } from "../utils";
 import { prompt_for_godot_executable, prompt_for_reload, select_godot_executable } from "../utils/prompts";
 import { subProcess, killSubProcesses } from "../utils/subspawn";
@@ -106,9 +105,11 @@ export class ClientConnectionManager {
 			targetVersion = "4.2";
 		}
 		const settingName = `editorPath.godot${projectVersion[0]}`;
-		const godotPath = clean_godot_path(get_configuration(settingName));
+		let godotPath = get_configuration(settingName);
 
 		const result = verify_godot_version(godotPath, projectVersion[0]);
+		godotPath = result.godotPath;
+
 		switch (result.status) {
 			case "WRONG_VERSION": {
 				const message = `Cannot launch headless LSP: The current project uses Godot v${projectVersion}, but the specified Godot executable is v${result.version}`;

+ 28 - 23
src/utils/project_utils.ts

@@ -3,7 +3,6 @@ import * as path from "path";
 import * as fs from "fs";
 import * as os from "os";
 import { execSync } from "child_process";
-import { get_configuration } from "./vscode_utils";
 
 let projectDir: string | undefined = undefined;
 let projectFile: string | undefined = undefined;
@@ -105,40 +104,46 @@ export async function convert_resource_path_to_uri(resPath: string): Promise<vsc
 type VERIFY_STATUS = "SUCCESS" | "WRONG_VERSION" | "INVALID_EXE";
 type VERIFY_RESULT = {
 	status: VERIFY_STATUS;
+	godotPath: string;
 	version?: string;
 };
 
 export function verify_godot_version(godotPath: string, expectedVersion: "3" | "4" | string): VERIFY_RESULT {
-	try {
-		if (os.platform() === 'darwin' && godotPath.endsWith('.app')) {
-			godotPath = path.join(godotPath, 'Contents', 'MacOS', 'Godot');
-		}
+	let target = clean_godot_path(godotPath);
 
-		const output = execSync(`"${godotPath}" --version`).toString().trim();
-		const pattern = /^(([34])\.([0-9]+)(?:\.[0-9]+)?)/m;
-		const match = output.match(pattern);
-		if (!match) {
-			return { status: "INVALID_EXE" };
+	let output = "";
+	try {
+		output = execSync(`"${target}" --version`).toString().trim();
+	} catch {
+		if (path.isAbsolute(target)) {
+			return { status: "INVALID_EXE", godotPath: target };
 		}
-		if (match[2] !== expectedVersion) {
-			return { status: "WRONG_VERSION", version: match[1] };
+		const workspacePath = vscode.workspace.workspaceFolders[0].uri.fsPath;
+		target = path.resolve(workspacePath, target);
+		try {
+			output = execSync(`"${target}" --version`).toString().trim();
+		} catch {
+			return { status: "INVALID_EXE", godotPath: target };
 		}
-		return { status: "SUCCESS", version: match[1] };
-	} catch {
-		return { status: "INVALID_EXE" };
 	}
+
+	const pattern = /^(([34])\.([0-9]+)(?:\.[0-9]+)?)/m;
+	const match = output.match(pattern);
+	if (!match) {
+		return { status: "INVALID_EXE", godotPath: target };
+	}
+	if (match[2] !== expectedVersion) {
+		return { status: "WRONG_VERSION", godotPath: target, version: match[1] };
+	}
+	return { status: "SUCCESS", godotPath: target, version: match[1] };
 }
 
 export function clean_godot_path(godotPath: string): string {
-	const cleanPath = godotPath.replace(/^"/, "").replace(/"$/, "");
-	const resolvedPath = resolve_workspace_relative_path(cleanPath);
-	return resolvedPath;
-}
+	let target = godotPath.replace(/^"/, "").replace(/"$/, "");
 
-function resolve_workspace_relative_path(target: string) {
-	if (!fs.existsSync(target)) {
-		const workspacePath = vscode.workspace.workspaceFolders[0].uri.fsPath;
-		return path.resolve(workspacePath, target);
+	if (os.platform() === "darwin" && target.endsWith(".app")) {
+		target = path.join(target, "Contents", "MacOS", "Godot");
 	}
+
 	return target;
 }