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

Fix vls initialize is slow #2458

Merged
merged 1 commit into from
Nov 11, 2020
Merged
Changes from all 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
69 changes: 45 additions & 24 deletions server/src/services/dependencyService.ts
Original file line number Diff line number Diff line change
@@ -14,30 +14,49 @@ import { performance } from 'perf_hooks';
import { logger } from '../log';

const readFileAsync = util.promisify(fs.readFile);
const accessFileAsync = util.promisify(fs.access);

async function findAllPackages(workspacePath: string, moduleName: string) {
async function createNodeModulesPaths(workspacePath: string) {
const startTime = performance.now();
const packages = await fg(`**/node_modules/${moduleName}/package.json`, {
cwd: workspacePath,
absolute: true,
unique: true
}).then(filePaths =>
Promise.all(
filePaths.map(filePath =>
readFileAsync(filePath, { encoding: 'utf8' }).then(content => {
const info = JSON.parse(content) as { name: string; version: string; main: string };

return {
name: info.name,
dir: path.dirname(filePath),
version: info.version,
module: require(path.resolve(path.dirname(filePath), info.main))
};
})
)
)
);
logger.logInfo(`Try to find ${moduleName} in ${workspacePath}. - ${Math.round(performance.now() - startTime)}ms`);
const nodeModules = (
await fg('**/node_modules', {
cwd: workspacePath,
absolute: true,
unique: true,
onlyDirectories: true,
onlyFiles: false
})
).filter(el => el.match(/node_modules/g)?.length === 1);

logger.logInfo(`Find node_modules paths in ${workspacePath} - ${Math.round(performance.now() - startTime)}ms`);
return nodeModules;
}

async function findAllPackages(nodeModulesPaths: string[], moduleName: string) {
async function getPackage(nodeModulesPath: string) {
const packageJSONPath = path.resolve(nodeModulesPath, moduleName, 'package.json');
try {
await accessFileAsync(packageJSONPath, fs.constants.R_OK);
const info: { name: string; version: string; main: string } = JSON.parse(
await readFileAsync(packageJSONPath, { encoding: 'utf8' })
);
return {
name: info.name,
dir: path.dirname(packageJSONPath),
version: info.version,
module: require(path.resolve(path.dirname(packageJSONPath), info.main))
};
} catch {
return null;
}
}

const packages = (await Promise.all(nodeModulesPaths.map(path => getPackage(path)))).filter(info => info) as Array<{
name: string;
dir: string;
version: string;
module: unknown;
}>;

return packages;
}
@@ -83,6 +102,8 @@ export const createDependencyService = () => {
let loaded: { [K in keyof RuntimeLibrary]: Dependency<RuntimeLibrary[K]>[] };

async function init(workspacePath: string, useWorkspaceDependencies: boolean, tsSDKPath?: string) {
const nodeModulesPaths = useWorkspaceDependencies ? await createNodeModulesPaths(workspacePath) : [];

const loadTypeScript = async (): Promise<Dependency<typeof ts>[]> => {
try {
if (useWorkspaceDependencies && tsSDKPath) {
@@ -103,7 +124,7 @@ export const createDependencyService = () => {
}

if (useWorkspaceDependencies) {
const packages = await findAllPackages(workspacePath, 'typescript');
const packages = await findAllPackages(nodeModulesPaths, 'typescript');
if (packages.length === 0) {
throw new Error(`No find any packages in ${workspacePath}.`);
}
@@ -140,7 +161,7 @@ export const createDependencyService = () => {
const loadCommonDep = async <N extends string, BM>(name: N, bundleModule: BM): Promise<Dependency<BM>[]> => {
try {
if (useWorkspaceDependencies) {
const packages = await findAllPackages(workspacePath, name);
const packages = await findAllPackages(nodeModulesPaths, name);
if (packages.length === 0) {
throw new Error(`No find ${name} packages in ${workspacePath}.`);
}