From 4c87e32bea1d10c8fcbebe3e15ce3bfd484143e1 Mon Sep 17 00:00:00 2001 From: merceyz Date: Mon, 16 Nov 2020 15:13:44 +0100 Subject: [PATCH 1/6] fix(vls): use require.resolve to locate tag providers --- .../modes/template/tagProviders/externalTagProviders.ts | 7 +++++-- server/src/modes/template/tagProviders/index.ts | 5 ++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/server/src/modes/template/tagProviders/externalTagProviders.ts b/server/src/modes/template/tagProviders/externalTagProviders.ts index 8164c9c615..29f18735fc 100644 --- a/server/src/modes/template/tagProviders/externalTagProviders.ts +++ b/server/src/modes/template/tagProviders/externalTagProviders.ts @@ -52,10 +52,13 @@ export function getDependencyTagProvider(workspacePath: string, depPkgJson: any) return null; } - const tagsPath = findConfigFile(workspacePath, path.join('node_modules/', depPkgJson.name, depPkgJson.vetur.tags)); + const tagsPath = findConfigFile( + workspacePath, + require.resolve(path.join(depPkgJson.name, depPkgJson.vetur.tags), { paths: [workspacePath] }) + ); const attrsPath = findConfigFile( workspacePath, - path.join('node_modules/', depPkgJson.name, depPkgJson.vetur.attributes) + require.resolve(path.join(depPkgJson.name, depPkgJson.vetur.attributes), { paths: [workspacePath] }) ); try { diff --git a/server/src/modes/template/tagProviders/index.ts b/server/src/modes/template/tagProviders/index.ts index 8cbbbe4ab7..c2e0b6bc74 100644 --- a/server/src/modes/template/tagProviders/index.ts +++ b/server/src/modes/template/tagProviders/index.ts @@ -115,7 +115,10 @@ export function getTagProviderSettings(workspacePath: string | null | undefined) } for (const dep of [...Object.keys(dependencies), ...Object.keys(devDependencies)]) { - const runtimePkgJsonPath = findConfigFile(workspacePath, join('node_modules', dep, 'package.json')); + const runtimePkgJsonPath = findConfigFile( + workspacePath, + require.resolve(join(dep, 'package.json'), { paths: [workspacePath] }) + ); if (!runtimePkgJsonPath) { continue; From 320755fd1d390c7cbc01fbb8da23bbaee9d52bcc Mon Sep 17 00:00:00 2001 From: merceyz Date: Mon, 16 Nov 2020 15:33:41 +0100 Subject: [PATCH 2/6] feat(vls): add Yarn PnP support --- server/src/services/vls.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/server/src/services/vls.ts b/server/src/services/vls.ts index 4ad6b4fc42..b2f5d2b5f1 100644 --- a/server/src/services/vls.ts +++ b/server/src/services/vls.ts @@ -1,4 +1,5 @@ import path from 'path'; +import fs from 'fs'; import { getFileFsPath, normalizeFileNameToFsPath } from '../utils/paths'; import { @@ -106,6 +107,15 @@ export class VLS { this.workspacePath = normalizeFileNameToFsPath(workspacePath); + // Enable PnP API + if (this.workspacePath && !process.versions.pnp && config.vetur.useWorkspaceDependencies) { + if (fs.existsSync(path.join(this.workspacePath, '.pnp.js'))) { + require(path.join(workspacePath, '.pnp.js')).setup(); + } else if (fs.existsSync(path.join(this.workspacePath, '.pnp.cjs'))) { + require(path.join(workspacePath, '.pnp.cjs')).setup(); + } + } + this.vueInfoService.init(this.languageModes); await this.dependencyService.init( this.workspacePath, From f816ae0e5721537814be985a9daf8e3c65c580fc Mon Sep 17 00:00:00 2001 From: merceyz Date: Mon, 16 Nov 2020 15:36:08 +0100 Subject: [PATCH 3/6] fix(vls): use require.resolve when infering vue version --- server/src/services/typescriptService/vueVersion.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server/src/services/typescriptService/vueVersion.ts b/server/src/services/typescriptService/vueVersion.ts index 33bc7a0b0d..7d26cdc83b 100644 --- a/server/src/services/typescriptService/vueVersion.ts +++ b/server/src/services/typescriptService/vueVersion.ts @@ -31,7 +31,10 @@ export function inferVueVersion(workspacePath: string): VueVersion { return floatVersionToEnum(sloppyVersion); } - const nodeModulesVuePackagePath = findConfigFile(path.resolve(workspacePath, 'node_modules/vue'), 'package.json'); + const nodeModulesVuePackagePath = findConfigFile( + workspacePath, + require.resolve('vue/package.json', { paths: [workspacePath] }) + ); const nodeModulesVuePackageJSON = nodeModulesVuePackagePath && JSON.parse(readFileSync(nodeModulesVuePackagePath, { encoding: 'utf-8' })!); const nodeModulesVueVersion = parseFloat(nodeModulesVuePackageJSON.version.match(/\d+\.\d+/)[0]); From 46cb81e51c86a6047bb032c54561c63a6b374a11 Mon Sep 17 00:00:00 2001 From: merceyz Date: Tue, 1 Dec 2020 11:15:51 +0100 Subject: [PATCH 4/6] chore: remove findConfigFile --- .../template/tagProviders/externalTagProviders.ts | 12 ++++-------- server/src/modes/template/tagProviders/index.ts | 5 +---- server/src/services/typescriptService/vueVersion.ts | 6 +----- 3 files changed, 6 insertions(+), 17 deletions(-) diff --git a/server/src/modes/template/tagProviders/externalTagProviders.ts b/server/src/modes/template/tagProviders/externalTagProviders.ts index 29f18735fc..47a00a922c 100644 --- a/server/src/modes/template/tagProviders/externalTagProviders.ts +++ b/server/src/modes/template/tagProviders/externalTagProviders.ts @@ -52,14 +52,10 @@ export function getDependencyTagProvider(workspacePath: string, depPkgJson: any) return null; } - const tagsPath = findConfigFile( - workspacePath, - require.resolve(path.join(depPkgJson.name, depPkgJson.vetur.tags), { paths: [workspacePath] }) - ); - const attrsPath = findConfigFile( - workspacePath, - require.resolve(path.join(depPkgJson.name, depPkgJson.vetur.attributes), { paths: [workspacePath] }) - ); + const tagsPath = require.resolve(path.join(depPkgJson.name, depPkgJson.vetur.tags), { paths: [workspacePath] }); + const attrsPath = require.resolve(path.join(depPkgJson.name, depPkgJson.vetur.attributes), { + paths: [workspacePath] + }); try { if (tagsPath && attrsPath) { diff --git a/server/src/modes/template/tagProviders/index.ts b/server/src/modes/template/tagProviders/index.ts index c2e0b6bc74..e228166c34 100644 --- a/server/src/modes/template/tagProviders/index.ts +++ b/server/src/modes/template/tagProviders/index.ts @@ -115,10 +115,7 @@ export function getTagProviderSettings(workspacePath: string | null | undefined) } for (const dep of [...Object.keys(dependencies), ...Object.keys(devDependencies)]) { - const runtimePkgJsonPath = findConfigFile( - workspacePath, - require.resolve(join(dep, 'package.json'), { paths: [workspacePath] }) - ); + const runtimePkgJsonPath = require.resolve(join(dep, 'package.json'), { paths: [workspacePath] }); if (!runtimePkgJsonPath) { continue; diff --git a/server/src/services/typescriptService/vueVersion.ts b/server/src/services/typescriptService/vueVersion.ts index 7d26cdc83b..835eca24be 100644 --- a/server/src/services/typescriptService/vueVersion.ts +++ b/server/src/services/typescriptService/vueVersion.ts @@ -1,5 +1,4 @@ import { readFileSync } from 'fs'; -import path from 'path'; import { findConfigFile } from '../../utils/workspace'; export enum VueVersion { @@ -31,10 +30,7 @@ export function inferVueVersion(workspacePath: string): VueVersion { return floatVersionToEnum(sloppyVersion); } - const nodeModulesVuePackagePath = findConfigFile( - workspacePath, - require.resolve('vue/package.json', { paths: [workspacePath] }) - ); + const nodeModulesVuePackagePath = require.resolve('vue/package.json', { paths: [workspacePath] }); const nodeModulesVuePackageJSON = nodeModulesVuePackagePath && JSON.parse(readFileSync(nodeModulesVuePackagePath, { encoding: 'utf-8' })!); const nodeModulesVueVersion = parseFloat(nodeModulesVuePackageJSON.version.match(/\d+\.\d+/)[0]); From 57e708c5033f4400e7e52e62dae97f6f16d58383 Mon Sep 17 00:00:00 2001 From: merceyz Date: Fri, 4 Dec 2020 12:30:57 +0100 Subject: [PATCH 5/6] fix: handle resolve failing --- .../tagProviders/externalTagProviders.ts | 19 ++++++++----------- .../src/modes/template/tagProviders/index.ts | 7 ++++--- .../services/typescriptService/vueVersion.ts | 3 +-- 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/server/src/modes/template/tagProviders/externalTagProviders.ts b/server/src/modes/template/tagProviders/externalTagProviders.ts index 47a00a922c..15144b93f2 100644 --- a/server/src/modes/template/tagProviders/externalTagProviders.ts +++ b/server/src/modes/template/tagProviders/externalTagProviders.ts @@ -52,18 +52,15 @@ export function getDependencyTagProvider(workspacePath: string, depPkgJson: any) return null; } - const tagsPath = require.resolve(path.join(depPkgJson.name, depPkgJson.vetur.tags), { paths: [workspacePath] }); - const attrsPath = require.resolve(path.join(depPkgJson.name, depPkgJson.vetur.attributes), { - paths: [workspacePath] - }); - try { - if (tagsPath && attrsPath) { - const tagsJson = JSON.parse(fs.readFileSync(tagsPath, 'utf-8')); - const attrsJson = JSON.parse(fs.readFileSync(attrsPath, 'utf-8')); - return getExternalTagProvider(depPkgJson.name, tagsJson, attrsJson); - } - return null; + const tagsPath = require.resolve(path.join(depPkgJson.name, depPkgJson.vetur.tags), { paths: [workspacePath] }); + const attrsPath = require.resolve(path.join(depPkgJson.name, depPkgJson.vetur.attributes), { + paths: [workspacePath] + }); + + const tagsJson = JSON.parse(fs.readFileSync(tagsPath, 'utf-8')); + const attrsJson = JSON.parse(fs.readFileSync(attrsPath, 'utf-8')); + return getExternalTagProvider(depPkgJson.name, tagsJson, attrsJson); } catch (err) { return null; } diff --git a/server/src/modes/template/tagProviders/index.ts b/server/src/modes/template/tagProviders/index.ts index e228166c34..b3480ab99c 100644 --- a/server/src/modes/template/tagProviders/index.ts +++ b/server/src/modes/template/tagProviders/index.ts @@ -115,9 +115,10 @@ export function getTagProviderSettings(workspacePath: string | null | undefined) } for (const dep of [...Object.keys(dependencies), ...Object.keys(devDependencies)]) { - const runtimePkgJsonPath = require.resolve(join(dep, 'package.json'), { paths: [workspacePath] }); - - if (!runtimePkgJsonPath) { + let runtimePkgJsonPath; + try { + runtimePkgJsonPath = require.resolve(join(dep, 'package.json'), { paths: [workspacePath] }); + } catch { continue; } diff --git a/server/src/services/typescriptService/vueVersion.ts b/server/src/services/typescriptService/vueVersion.ts index 835eca24be..d3868bdf30 100644 --- a/server/src/services/typescriptService/vueVersion.ts +++ b/server/src/services/typescriptService/vueVersion.ts @@ -31,8 +31,7 @@ export function inferVueVersion(workspacePath: string): VueVersion { } const nodeModulesVuePackagePath = require.resolve('vue/package.json', { paths: [workspacePath] }); - const nodeModulesVuePackageJSON = - nodeModulesVuePackagePath && JSON.parse(readFileSync(nodeModulesVuePackagePath, { encoding: 'utf-8' })!); + const nodeModulesVuePackageJSON = JSON.parse(readFileSync(nodeModulesVuePackagePath, { encoding: 'utf-8' })!); const nodeModulesVueVersion = parseFloat(nodeModulesVuePackageJSON.version.match(/\d+\.\d+/)[0]); return floatVersionToEnum(nodeModulesVueVersion); From 2889691d540afc4a09f4096e65a19b2844d609c3 Mon Sep 17 00:00:00 2001 From: merceyz Date: Fri, 4 Dec 2020 12:57:09 +0100 Subject: [PATCH 6/6] fix: remove useWorkspaceDependencies check --- server/src/services/vls.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server/src/services/vls.ts b/server/src/services/vls.ts index b2f5d2b5f1..e3a48fba63 100644 --- a/server/src/services/vls.ts +++ b/server/src/services/vls.ts @@ -107,8 +107,8 @@ export class VLS { this.workspacePath = normalizeFileNameToFsPath(workspacePath); - // Enable PnP API - if (this.workspacePath && !process.versions.pnp && config.vetur.useWorkspaceDependencies) { + // Enable Yarn PnP support https://yarnpkg.com/features/pnp + if (!process.versions.pnp) { if (fs.existsSync(path.join(this.workspacePath, '.pnp.js'))) { require(path.join(workspacePath, '.pnp.js')).setup(); } else if (fs.existsSync(path.join(this.workspacePath, '.pnp.cjs'))) {