From 75332f118545579e304babe510b3c6ffeff8365a Mon Sep 17 00:00:00 2001 From: Andreea Isac Date: Thu, 27 Jul 2023 04:10:56 -0700 Subject: [PATCH] Fix1914 - Variable substitution stopped working with release 1.7, related to sourceDirectory and kit info --- src/cmakeProject.ts | 28 +++++++++++++++++++++------- src/drivers/cmakeDriver.ts | 19 +++++++++++++++++-- src/expand.ts | 2 +- src/presetsController.ts | 9 ++++++++- src/projectController.ts | 6 ++++-- 5 files changed, 51 insertions(+), 13 deletions(-) diff --git a/src/cmakeProject.ts b/src/cmakeProject.ts index 8aef48a35..ba9faa8d4 100644 --- a/src/cmakeProject.ts +++ b/src/cmakeProject.ts @@ -704,7 +704,8 @@ export class CMakeProject { } if (selectedFile) { const newSourceDirectory = path.dirname(selectedFile); - await this.setSourceDir(await util.normalizeAndVerifySourceDir(newSourceDirectory, CMakeDriver.sourceDirExpansionOptions(this.workspaceContext.folder.uri.fsPath))); + let kit = this.getActiveKit(); + await this.setSourceDir(await util.normalizeAndVerifySourceDir(newSourceDirectory, await CMakeDriver.sourceDirExpansionOptions(this.workspaceContext.folder.uri.fsPath, kit))); void vscode.workspace.getConfiguration('cmake', this.workspaceFolder.uri).update("sourceDirectory", this._sourceDir); if (config) { // Updating sourceDirectory here, at the beginning of the configure process, @@ -907,7 +908,9 @@ export class CMakeProject { */ private async init(sourceDirectory: string) { log.debug(localize('second.phase.init', 'Starting CMake Tools second-phase init')); - await this.setSourceDir(await util.normalizeAndVerifySourceDir(sourceDirectory, CMakeDriver.sourceDirExpansionOptions(this.workspaceContext.folder.uri.fsPath))); + this.kitsController = await KitsController.init(this); + let kit = this.getActiveKit(); + await this.setSourceDir(await util.normalizeAndVerifySourceDir(sourceDirectory, await CMakeDriver.sourceDirExpansionOptions(this.workspaceContext.folder.uri.fsPath, kit))); this.hideBuildButton = (this.workspaceContext.config.statusbar.advanced?.build?.visibility === "hidden") ? true : false; this.hideDebugButton = (this.workspaceContext.config.statusbar.advanced?.debug?.visibility === "hidden") ? true : false; this.hideLaunchButton = (this.workspaceContext.config.statusbar.advanced?.launch?.visibility === "hidden") ? true : false; @@ -935,7 +938,6 @@ export class CMakeProject { this.statusMessage.set(localize('ready.status', 'Ready')); - this.kitsController = await KitsController.init(this); this.presetsController = await PresetsController.init(this, this.kitsController, this.isMultiProjectFolder); await this.doUseCMakePresetsChange(); @@ -1005,6 +1007,20 @@ export class CMakeProject { return this.presetsController.onUserPresetsChanged(listener); } + public getActiveKit(): Kit | null { + if (this.activeKit) { + return this.activeKit; + } + + const kitName: string | null = this.workspaceContext.state.getActiveKitName(this.folderName, this.isMultiProjectFolder); + if (kitName) { + // It remembers a kit. Find it in the kits avail in this dir: + return this.kitsController.availableKits.find(k => k.name === kitName) || null; + } + + return null; + } + async initializeKitOrPresets() { if (this.useCMakePresets) { const latestConfigPresetName = this.workspaceContext.state.getConfigurePresetName(this.folderName, this.isMultiProjectFolder); @@ -1018,10 +1034,8 @@ export class CMakeProject { } } else { // Check if the CMakeProject remembers what kit it was last using in this dir: - const kitName = this.workspaceContext.state.getActiveKitName(this.folderName, this.isMultiProjectFolder); - if (kitName) { - // It remembers a kit. Find it in the kits avail in this dir: - const kit = this.kitsController.availableKits.find(k => k.name === kitName) || null; + const kit = this.getActiveKit(); + if (kit) { // Set the kit: (May do nothing if no kit was found) await this.setKit(kit); } diff --git a/src/drivers/cmakeDriver.ts b/src/drivers/cmakeDriver.ts index 22c55d5fe..8f6c82c21 100644 --- a/src/drivers/cmakeDriver.ts +++ b/src/drivers/cmakeDriver.ts @@ -34,6 +34,7 @@ import { getValue } from '@cmt/preset'; import { CacheEntry } from '@cmt/cache'; import { CMakeBuildRunner } from '@cmt/cmakeBuildRunner'; import { DebuggerInformation } from '@cmt/debug/debuggerConfigureDriver'; +import { getActiveProject } from '@cmt/extension'; nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); const localize: nls.LocalizeFunc = nls.loadMessageBundle(); @@ -320,6 +321,9 @@ export abstract class CMakeDriver implements vscode.Disposable { private _kitDetect: KitDetect | null = null; + public getKit(): Kit | null { return this._kit; } + public getKitDetect(): KitDetect | null { return this._kitDetect; } + private _useCMakePresets: boolean = true; get useCMakePresets(): boolean { @@ -390,12 +394,22 @@ export abstract class CMakeDriver implements vscode.Disposable { return { vars, variantVars }; } - static sourceDirExpansionOptions(workspaceFolderFspath: string | null): expand.ExpansionOptions { + static async sourceDirExpansionOptions(workspaceFolderFspath: string | null, kit?: Kit | null): Promise { const ws_root = util.lightNormalizePath(workspaceFolderFspath || '.'); // Fill in default replacements + const prj = getActiveProject(); + if (!kit) { + kit = prj ? prj.getActiveKit() : undefined; + } + const kitName: string = kit ? kit.name : ''; + const kitDetect = kit ? await getKitDetect(kit) : undefined; + const kitVendor: string = kitDetect ? kitDetect.vendor || '' : ''; + const vars: expand.MinimalPresetContextVars = { generator: 'generator', + buildKit: kitName, + buildKitVendor: kitVendor, workspaceFolder: ws_root, workspaceFolderBasename: path.basename(ws_root), sourceDir: '${sourceDir}', @@ -680,7 +694,8 @@ export abstract class CMakeDriver implements vscode.Disposable { private async _refreshExpansions(configurePreset?: preset.ConfigurePreset | null) { return this.doRefreshExpansions(async () => { - this.sourceDir = await util.normalizeAndVerifySourceDir(this.sourceDirUnexpanded, CMakeDriver.sourceDirExpansionOptions(this.workspaceFolder)); + let kit = this._kit; + this.sourceDir = await util.normalizeAndVerifySourceDir(this.sourceDirUnexpanded, await CMakeDriver.sourceDirExpansionOptions(this.workspaceFolder, kit)); const opts = this.expansionOptions; opts.envOverride = await this.getConfigureEnvironment(configurePreset); diff --git a/src/expand.ts b/src/expand.ts index 13b7967e9..837a6eb2f 100644 --- a/src/expand.ts +++ b/src/expand.ts @@ -145,7 +145,7 @@ async function expandStringHelper(input: string, opts: ExpansionOptions) { if (key !== 'dollar') { // Replace dollar sign at the very end of the expanding process const replacement = replacements[key]; - if (!replacement) { + if (replacement === undefined) { log.warning(localize('invalid.variable.reference', 'Invalid variable reference {0} in string: {1}', full, input)); } else { subs.set(full, replacement); diff --git a/src/presetsController.ts b/src/presetsController.ts index 05febbeff..9e5435bda 100644 --- a/src/presetsController.ts +++ b/src/presetsController.ts @@ -12,7 +12,7 @@ import rollbar from '@cmt/rollbar'; import { ExpansionOptions } from '@cmt/expand'; import paths from '@cmt/paths'; import { KitsController } from '@cmt/kitsController'; -import { descriptionForKit, Kit, SpecialKits } from '@cmt/kit'; +import { descriptionForKit, Kit, getKitDetect, SpecialKits } from '@cmt/kit'; import { getHostTargetArchString } from '@cmt/installs/visualStudio'; import { loadSchema } from '@cmt/schema'; import json5 = require('json5'); @@ -42,12 +42,19 @@ export class PresetsController { const presetsController = new PresetsController(project, kitsController, isMultiProject); const expandSourceDir = async (dir: string) => { const workspaceFolder = project.workspaceFolder.uri.fsPath; + let kit = project.getActiveKit(); + const kitName: string = kit ? kit.name : ''; + const kitDetect = kit ? await getKitDetect(kit) : undefined; + const kitVendor: string = kitDetect ? kitDetect.vendor || '' : ''; + const expansionOpts: ExpansionOptions = { vars: { workspaceFolder, workspaceFolderBasename: path.basename(workspaceFolder), workspaceHash: util.makeHashString(workspaceFolder), workspaceRoot: workspaceFolder, + buildKit: kitName, + buildKitVendor: kitVendor, workspaceRootFolderName: path.dirname(workspaceFolder), userHome: paths.userHome, // Following fields are not supported for sourceDir expansion diff --git a/src/projectController.ts b/src/projectController.ts index f5e0d3ba4..587254c62 100644 --- a/src/projectController.ts +++ b/src/projectController.ts @@ -231,7 +231,8 @@ export class ProjectController implements vscode.Disposable { } async getProjectForFolder(folder: string): Promise { - const sourceDir = util.platformNormalizePath(await util.normalizeAndVerifySourceDir(folder, CMakeDriver.sourceDirExpansionOptions(folder))); + let kit = this.activeProject?.activeKit; + const sourceDir = util.platformNormalizePath(await util.normalizeAndVerifySourceDir(folder, await CMakeDriver.sourceDirExpansionOptions(folder, kit))); const allCMakeProjects: CMakeProject[] = this.getAllCMakeProjects(); for (const project of allCMakeProjects) { if (util.platformNormalizePath(project.sourceDir) === sourceDir || @@ -390,7 +391,8 @@ export class ProjectController implements vscode.Disposable { } // Normalize the paths. for (let i = 0; i < sourceDirectories.length; i++) { - sourceDirectories[i] = await util.normalizeAndVerifySourceDir(sourceDirectories[i], CMakeDriver.sourceDirExpansionOptions(folder.uri.fsPath)); + let kit = this.activeProject?.activeKit; + sourceDirectories[i] = await util.normalizeAndVerifySourceDir(sourceDirectories[i], await CMakeDriver.sourceDirExpansionOptions(folder.uri.fsPath, kit)); } const projects: CMakeProject[] | undefined = this.getProjectsForWorkspaceFolder(folder);