Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VSC-1546] fix version switch by persist sys python in idfSetup #1384

Merged
merged 16 commits into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@
"type": "string",
"default": "python",
"description": "%param.pythonBinPath%",
"scope": "application"
"scope": "resource"
},
"idf.flashBaudRate": {
"type": "string",
Expand Down
1 change: 1 addition & 0 deletions src/checkExtensionSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export async function checkExtensionSettings(
idfPath,
gitPath,
toolsPath: idfToolsPath,
sysPythonPath: "/usr/bin/python3",
version: idfVersion,
isValid: false,
};
Expand Down
11 changes: 5 additions & 6 deletions src/examples/ExamplesPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,7 @@ export class ExamplesPlanel {
);
await this.setCurrentSettingsInTemplate(
settingsJsonPath,
idfSetup.idfPath,
idfSetup.toolsPath
idfSetup
);
vscode.commands.executeCommand("vscode.openFolder", projectPath);
} catch (error) {
Expand Down Expand Up @@ -219,13 +218,13 @@ export class ExamplesPlanel {

private async setCurrentSettingsInTemplate(
settingsJsonPath: string,
idfPathDir: string,
toolsPath: string
idfSetup: IdfSetup
) {
const settingsJson = await readJSON(settingsJsonPath);
const isWin = process.platform === "win32" ? "Win" : "";
settingsJson["idf.espIdfPath" + isWin] = idfPathDir;
settingsJson["idf.toolsPath" + isWin] = toolsPath;
settingsJson["idf.espIdfPath" + isWin] = idfSetup.idfPath;
settingsJson["idf.toolsPath" + isWin] = idfSetup.toolsPath;
settingsJson["idf.pythonInstallPath"] = idfSetup.sysPythonPath;
await writeJSON(settingsJsonPath, settingsJson, {
spaces: vscode.workspace.getConfiguration().get("editor.tabSize") || 2,
});
Expand Down
6 changes: 2 additions & 4 deletions src/newProject/newProjectInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ import { getPreviousIdfSetups } from "../setup/existingIdfSetups";
import { IdfSetup } from "../views/setup/types";

export interface INewProjectArgs {
espIdfPath: string;
espIdfToolsPath: string;
espIdfSetup: IdfSetup;
espAdfPath: string;
espMdfPath: string;
espMatterPath: string;
Expand Down Expand Up @@ -149,9 +148,8 @@ export async function getNewProjectArgs(
return {
boards: espBoards,
components,
espIdfToolsPath: idfSetup.toolsPath,
espIdfSetup: idfSetup,
espAdfPath: adfExists ? espAdfPath : undefined,
espIdfPath: idfExists ? idfSetup.idfPath : undefined,
espMdfPath: mdfExists ? espMdfPath : undefined,
espMatterPath: matterExists ? espMatterPath : undefined,
espHomeKitSdkPath: homekitSdkExists ? espHomeKitSdkPath : undefined,
Expand Down
14 changes: 6 additions & 8 deletions src/newProject/newProjectPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as utils from "../utils";
import { IExample } from "../examples/Example";
import { setCurrentSettingsInTemplate } from "./utils";
import { NotificationMode, readParameter } from "../idfConfiguration";
import { IdfSetup } from "../views/setup/types";

export class NewProjectPanel {
public static currentPanel: NewProjectPanel | undefined;
Expand Down Expand Up @@ -70,8 +71,8 @@ export class NewProjectPanel {
if (newProjectArgs.espAdfPath) {
localResourceRoots.push(vscode.Uri.file(newProjectArgs.espAdfPath));
}
if (newProjectArgs.espIdfPath) {
localResourceRoots.push(vscode.Uri.file(newProjectArgs.espIdfPath));
if (newProjectArgs.espIdfSetup.idfPath) {
localResourceRoots.push(vscode.Uri.file(newProjectArgs.espIdfSetup.idfPath));
}
if (newProjectArgs.espMdfPath) {
localResourceRoots.push(vscode.Uri.file(newProjectArgs.espMdfPath));
Expand Down Expand Up @@ -124,8 +125,7 @@ export class NewProjectPanel {
message.template
) {
this.createProject(
newProjectArgs.espIdfPath,
newProjectArgs.espIdfToolsPath,
newProjectArgs.espIdfSetup,
JSON.parse(message.components),
message.port,
message.containerFolder,
Expand Down Expand Up @@ -199,8 +199,7 @@ export class NewProjectPanel {
}

private async createProject(
idfPath: string,
idfToolsPath: string,
idfSetup: IdfSetup,
components: IComponent[],
port: string,
projectDirectory: string,
Expand Down Expand Up @@ -287,8 +286,7 @@ export class NewProjectPanel {
);
const settingsJson = await setCurrentSettingsInTemplate(
settingsJsonPath,
idfPath,
idfToolsPath,
idfSetup,
port,
openOcdConfigs,
workspaceFolder
Expand Down
15 changes: 9 additions & 6 deletions src/newProject/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import { readParameter } from "../idfConfiguration";
import { readJSON } from "fs-extra";
import { Uri } from "vscode";
import { IdfSetup } from "../views/setup/types";

export async function setCurrentSettingsInTemplate(
settingsJsonPath: string,
idfPathDir: string,
toolsPath: string,
idfSetup: IdfSetup,
port: string,
openOcdConfigs?: string,
workspace?: Uri
Expand All @@ -32,8 +32,11 @@ export async function setCurrentSettingsInTemplate(
const adfPathDir = readParameter("idf.espAdfPath", workspace);
const mdfPathDir = readParameter("idf.espMdfPath", workspace);
const isWin = process.platform === "win32" ? "Win" : "";
if (idfPathDir) {
settingsJson["idf.espIdfPath" + isWin] = idfPathDir;
if (idfSetup.idfPath) {
settingsJson["idf.espIdfPath" + isWin] = idfSetup.idfPath;
}
if (idfSetup.sysPythonPath) {
settingsJson["idf.pythonInstallPath"] = idfSetup.sysPythonPath;
}
if (adfPathDir) {
settingsJson["idf.espAdfPath" + isWin] = adfPathDir;
Expand All @@ -50,8 +53,8 @@ export async function setCurrentSettingsInTemplate(
if (port.indexOf("no port") === -1) {
settingsJson["idf.port" + isWin] = port;
}
if (toolsPath) {
settingsJson["idf.toolsPath" + isWin] = toolsPath;
if (idfSetup.toolsPath) {
settingsJson["idf.toolsPath" + isWin] = idfSetup.toolsPath;
}
return settingsJson;
}
19 changes: 10 additions & 9 deletions src/pythonManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,6 @@ export async function installPythonEnvFromIdfTools(
});
}

await writeParameter(
"idf.pythonInstallPath",
pythonBinPath,
ConfigurationTarget.Global
);

await execProcessWithLog(
pythonBinPath,
[idfToolsPyPath, "install-python-env"],
Expand Down Expand Up @@ -309,7 +303,10 @@ export async function execProcessWithLog(
}

export async function getVirtualEnvPythonPath(workspaceFolder: Uri) {
let pythonPath = readParameter("idf.pythonInstallPath") as string;
let pythonPath = readParameter(
"idf.pythonInstallPath",
workspaceFolder
) as string;
let espIdfDir = readParameter("idf.espIdfPath", workspaceFolder) as string;
let idfToolsDir = readParameter("idf.toolsPath", workspaceFolder) as string;
const idfPathExists = await pathExists(espIdfDir);
Expand All @@ -327,15 +324,19 @@ export async function getVirtualEnvPythonPath(workspaceFolder: Uri) {
}

export async function getPythonPath(workspaceFolder: Uri) {
let sysPythonBinPath = readParameter("idf.pythonInstallPath") as string;
let sysPythonBinPath = readParameter(
"idf.pythonInstallPath",
workspaceFolder
) as string;
const doesSysPythonBinPathExist = await pathExists(sysPythonBinPath);
if (!doesSysPythonBinPathExist) {
sysPythonBinPath = await getSystemPython(workspaceFolder);
if (sysPythonBinPath) {
await writeParameter(
"idf.pythonInstallPath",
sysPythonBinPath,
ConfigurationTarget.Global
ConfigurationTarget.WorkspaceFolder,
workspaceFolder
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/setup/existingIdfSetups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export async function clearPreviousIdfSetups() {
export async function createIdfSetup(
idfPath: string,
toolsPath: string,
sysPythonBinPath: string,
gitPath: string
) {
const idfSetupId = getIdfMd5sum(idfPath);
Expand All @@ -68,6 +69,7 @@ export async function createIdfSetup(
idfPath,
gitPath,
toolsPath,
sysPythonPath: sysPythonBinPath,
version: idfVersion,
isValid: false,
};
Expand Down
7 changes: 1 addition & 6 deletions src/setup/pyReqsInstallStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import { SetupPanel } from "./SetupPanel";
import { saveSettings } from "./setupInit";
import { getOpenOcdRules } from "./addOpenOcdRules";
import { addIdfPath } from "./espIdfJson";
import { writeParameter } from "../idfConfiguration";

export async function createPyReqs(
idfPath: string,
Expand All @@ -46,15 +45,11 @@ export async function createPyReqs(
progress,
cancelToken
);
await writeParameter(
"idf.pythonInstallPath",
pyPath,
vscode.ConfigurationTarget.Global
);
await saveSettings(
idfPath,
toolsPath,
gitPath,
pyPath,
saveScope,
workspaceFolderUri,
espIdfStatusBar
Expand Down
34 changes: 24 additions & 10 deletions src/setup/setupInit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ import { pathExists } from "fs-extra";
import path from "path";
import { Logger } from "../logger/logger";
import * as idfConf from "../idfConfiguration";
import {
addIdfPath,
getPropertyFromJson,
getSelectedIdfInstalled,
} from "./espIdfJson";
import { getPropertyFromJson, getSelectedIdfInstalled } from "./espIdfJson";
import {
createIdfSetup,
getPreviousIdfSetups,
Expand All @@ -35,7 +31,6 @@ import {
import { checkPyVenv } from "./setupValidation/pythonEnv";
import { packageJson } from "../utils";
import { getPythonPath, getVirtualEnvPythonPath } from "../pythonManager";
import { getCurrentIdfSetup } from "../versionSwitcher";
import { CommandKeys, createCommandDictionary } from "../cmdTreeView/cmdStore";

export interface ISetupInitArgs {
Expand Down Expand Up @@ -236,7 +231,15 @@ export async function isCurrentInstallValid(workspaceFolder: Uri) {
// REMOVE this line after next release
const sysPythonBinPath = await getPythonPath(workspaceFolder);

const pythonBinPath = await getVirtualEnvPythonPath(workspaceFolder);
let pythonBinPath: string = "";
if (sysPythonBinPath) {
pythonBinPath = await getVirtualEnvPythonPath(workspaceFolder);
} else {
pythonBinPath = idfConf.readParameter(
"idf.pythonBinPath",
workspaceFolder
) as string;
}

let espIdfPath = idfConf.readParameter("idf.espIdfPath", workspaceFolder);
let idfPathVersion = await utils.getEspIdfFromCMake(espIdfPath);
Expand Down Expand Up @@ -296,9 +299,11 @@ export async function saveSettings(
espIdfPath: string,
toolsPath: string,
gitPath: string,
sysPythonBinPath: string,
saveScope: ConfigurationTarget,
workspaceFolderUri: Uri,
espIdfStatusBar: StatusBarItem
espIdfStatusBar: StatusBarItem,
saveGlobalState: boolean = true
) {
const confTarget =
saveScope ||
Expand All @@ -324,13 +329,22 @@ export async function saveSettings(
gitPath,
ConfigurationTarget.Global
);
let currentIdfSetup = await createIdfSetup(espIdfPath, toolsPath, gitPath);
await idfConf.writeParameter(
"idf.pythonInstallPath",
sysPythonBinPath,
confTarget,
workspaceFolder
);
const idfPathVersion = await utils.getEspIdfFromCMake(espIdfPath);
if (saveGlobalState) {
await createIdfSetup(espIdfPath, toolsPath, sysPythonBinPath, gitPath);
}
if (espIdfStatusBar) {
const commandDictionary = createCommandDictionary();
espIdfStatusBar.text =
`$(${
commandDictionary[CommandKeys.SelectCurrentIdfVersion].iconId
}) ESP-IDF v` + currentIdfSetup.version;
}) ESP-IDF v` + idfPathVersion;
}
Logger.infoNotify("ESP-IDF has been configured");
}
25 changes: 18 additions & 7 deletions src/setup/setupValidation/espIdfSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ import { pathExists } from "fs-extra";
import { Logger } from "../../logger/logger";
import { checkPyVenv } from "./pythonEnv";
import { ConfigurationTarget, StatusBarItem, Uri } from "vscode";
import { getPythonEnvPath, getVirtualEnvPythonPath } from "../../pythonManager";
import { readParameter } from "../../idfConfiguration";
import { getPythonEnvPath } from "../../pythonManager";

export async function useIdfSetupSettings(
setupConf: IdfSetup,
Expand All @@ -37,14 +36,18 @@ export async function useIdfSetupSettings(
setupConf.idfPath,
setupConf.toolsPath,
setupConf.gitPath,
setupConf.sysPythonPath,
saveScope,
workspaceFolderUri,
espIdfStatusBar
espIdfStatusBar,
false
);
}

export async function checkIdfSetup(setupConf: IdfSetup,
logToChannel: boolean = true) {
export async function checkIdfSetup(
setupConf: IdfSetup,
logToChannel: boolean = true
) {
try {
if (!setupConf.idfPath) {
return false;
Expand Down Expand Up @@ -75,8 +78,16 @@ export async function checkIdfSetup(setupConf: IdfSetup,
if (failedToolsResult.length) {
return false;
}
let sysPythonBinPath = readParameter("idf.pythonInstallPath") as string;
const virtualEnvPython = await getPythonEnvPath(setupConf.idfPath, setupConf.toolsPath, sysPythonBinPath);
let virtualEnvPython = "";
if (setupConf.python) {
virtualEnvPython = setupConf.python;
} else {
virtualEnvPython = await getPythonEnvPath(
setupConf.idfPath,
setupConf.toolsPath,
setupConf.sysPythonPath
);
}

const pyEnvReqs = await checkPyVenv(virtualEnvPython, setupConf.idfPath);
return pyEnvReqs;
Expand Down
10 changes: 8 additions & 2 deletions src/test/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
setExtensionContext,
updateProjectNameInCMakeLists,
} from "../utils";
import { IdfSetup } from "../views/setup/types";

suite("Project tests", () => {
const absPath = (filename) => resolve(__dirname, "..", "..", filename);
Expand Down Expand Up @@ -147,10 +148,15 @@ suite("Project tests", () => {
assert.equal(settingsJson["idf.espIdfPath"], undefined);
const openOcdConfigs =
"interface/ftdi/esp32_devkitj_v1.cfg,target/esp32.cfg";

const idfSetup = {
idfPath: process.env.IDF_PATH,
toolsPath: process.env.IDF_TOOLS_PATH,
sysPythonPath: "python"
} as IdfSetup;
const newSettingsJson = await setCurrentSettingsInTemplate(
settingsJsonPath,
process.env.IDF_PATH,
process.env.IDF_TOOLS_PATH,
idfSetup,
"no port",
openOcdConfigs,
Uri.file(projectPath)
Expand Down
Loading
Loading