From ea8e9a60e5e0a62f69c847dc3f7ff24594f57078 Mon Sep 17 00:00:00 2001 From: Mestery Date: Sun, 7 Mar 2021 18:33:59 +0100 Subject: [PATCH] support yarn pnp for nodeAddonIncludes This solves problems with Yarn PnP and node addon includes. --- .../src/LanguageServer/configurations.ts | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Extension/src/LanguageServer/configurations.ts b/Extension/src/LanguageServer/configurations.ts index 213f739f57..bef6a02b01 100644 --- a/Extension/src/LanguageServer/configurations.ts +++ b/Extension/src/LanguageServer/configurations.ts @@ -406,15 +406,23 @@ export class CppProperties { if (!error) { try { const pathToNode: string = which.sync("node"); - const nodeAddonMap: { [dependency: string]: string } = { - "nan": `"${pathToNode}" --no-warnings -e "require('nan')"`, - "node-addon-api": `"${pathToNode}" --no-warnings -p "require('node-addon-api').include"` - }; + const nodeAddonMap: [string, string][] = [ + ["node-addon-api", `"${pathToNode}" --no-warnings -p "require('node-addon-api').include"`], + ["nan", `"${pathToNode}" --no-warnings -e "require('nan')"`] + ]; + // Yarn (2) PnP support + const pathToYarn: string | null = which.sync("yarn", { nothrow: true }); + if (pathToYarn && await util.checkDirectoryExists(path.join(rootPath, ".yarn/cache"))) { + nodeAddonMap.push( + ["node-addon-api", `"${pathToYarn}" node --no-warnings -p "require('node-addon-api').include"`], + ["nan", `"${pathToYarn}" node --no-warnings -e "require('nan')"`] + ); + } - for (const dep in nodeAddonMap) { + for (const [dep, execCmd] of nodeAddonMap) { if (dep in package_json.dependencies) { - const execCmd: string = nodeAddonMap[dep]; - let stdout: string = await util.execChildProcess(execCmd, rootPath); + let stdout: string | void = await util.execChildProcess(execCmd, rootPath) + .catch((error) => console.log('readNodeAddonIncludeLocations', error.message)); if (!stdout) { continue; } @@ -700,6 +708,7 @@ export class CppProperties { const origIncludePath: string[] | undefined = configuration.includePath; if (settings.addNodeAddonIncludePaths) { const includePath: string[] = origIncludePath || []; + console.log(this.nodeAddonIncludes); configuration.includePath = includePath.concat(this.nodeAddonIncludes.filter(i => includePath.indexOf(i) < 0)); } configuration.defines = this.updateConfigurationStringArray(configuration.defines, settings.defaultDefines, env);