Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/build/npm/preinstall.ts
4770 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
import path from 'path';
6
import * as fs from 'fs';
7
import * as child_process from 'child_process';
8
import * as os from 'os';
9
10
if (!process.env['VSCODE_SKIP_NODE_VERSION_CHECK']) {
11
// Get the running Node.js version
12
const nodeVersion = /^(\d+)\.(\d+)\.(\d+)/.exec(process.versions.node);
13
const majorNodeVersion = parseInt(nodeVersion![1]);
14
const minorNodeVersion = parseInt(nodeVersion![2]);
15
const patchNodeVersion = parseInt(nodeVersion![3]);
16
17
// Get the required Node.js version from .nvmrc
18
const nvmrcPath = path.join(import.meta.dirname, '..', '..', '.nvmrc');
19
const requiredVersion = fs.readFileSync(nvmrcPath, 'utf8').trim();
20
const requiredVersionMatch = /^(\d+)\.(\d+)\.(\d+)/.exec(requiredVersion);
21
22
if (!requiredVersionMatch) {
23
console.error('\x1b[1;31m*** Unable to parse required Node.js version from .nvmrc\x1b[0;0m');
24
throw new Error();
25
}
26
27
const requiredMajor = parseInt(requiredVersionMatch[1]);
28
const requiredMinor = parseInt(requiredVersionMatch[2]);
29
const requiredPatch = parseInt(requiredVersionMatch[3]);
30
31
if (majorNodeVersion < requiredMajor ||
32
(majorNodeVersion === requiredMajor && minorNodeVersion < requiredMinor) ||
33
(majorNodeVersion === requiredMajor && minorNodeVersion === requiredMinor && patchNodeVersion < requiredPatch)) {
34
console.error(`\x1b[1;31m*** Please use Node.js v${requiredVersion} or later for development. Currently using v${process.versions.node}.\x1b[0;0m`);
35
throw new Error();
36
}
37
}
38
39
if (process.env.npm_execpath?.includes('yarn')) {
40
console.error('\x1b[1;31m*** Seems like you are using `yarn` which is not supported in this repo any more, please use `npm i` instead. ***\x1b[0;0m');
41
throw new Error();
42
}
43
44
if (process.platform === 'win32') {
45
if (!hasSupportedVisualStudioVersion()) {
46
console.error('\x1b[1;31m*** Invalid C/C++ Compiler Toolchain. Please check https://github.com/microsoft/vscode/wiki/How-to-Contribute#prerequisites.\x1b[0;0m');
47
console.error('\x1b[1;31m*** If you have Visual Studio installed in a custom location, you can specify it via the environment variable:\x1b[0;0m');
48
console.error('\x1b[1;31m*** set vs2022_install=<path> (or vs2019_install for older versions)\x1b[0;0m');
49
throw new Error();
50
}
51
}
52
53
installHeaders();
54
55
if (process.arch !== os.arch()) {
56
console.error(`\x1b[1;31m*** ARCHITECTURE MISMATCH: The node.js process is ${process.arch}, but your OS architecture is ${os.arch()}. ***\x1b[0;0m`);
57
console.error(`\x1b[1;31m*** This can greatly increase the build time of vs code. ***\x1b[0;0m`);
58
}
59
60
function hasSupportedVisualStudioVersion() {
61
// Translated over from
62
// https://source.chromium.org/chromium/chromium/src/+/master:build/vs_toolchain.py;l=140-175
63
const supportedVersions = ['2022', '2019'];
64
65
const availableVersions = [];
66
for (const version of supportedVersions) {
67
// Check environment variable first (explicit override)
68
let vsPath = process.env[`vs${version}_install`];
69
if (vsPath && fs.existsSync(vsPath)) {
70
availableVersions.push(version);
71
break;
72
}
73
74
// Check default installation paths
75
const programFiles86Path = process.env['ProgramFiles(x86)'];
76
const programFiles64Path = process.env['ProgramFiles'];
77
78
const vsTypes = ['Enterprise', 'Professional', 'Community', 'Preview', 'BuildTools', 'IntPreview'];
79
if (programFiles64Path) {
80
vsPath = `${programFiles64Path}/Microsoft Visual Studio/${version}`;
81
if (vsTypes.some(vsType => fs.existsSync(path.join(vsPath!, vsType)))) {
82
availableVersions.push(version);
83
break;
84
}
85
}
86
87
if (programFiles86Path) {
88
vsPath = `${programFiles86Path}/Microsoft Visual Studio/${version}`;
89
if (vsTypes.some(vsType => fs.existsSync(path.join(vsPath!, vsType)))) {
90
availableVersions.push(version);
91
break;
92
}
93
}
94
}
95
96
return availableVersions.length;
97
}
98
99
function installHeaders() {
100
const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
101
child_process.execSync(`${npm} ${process.env.npm_command || 'ci'}`, {
102
env: process.env,
103
cwd: path.join(import.meta.dirname, 'gyp'),
104
stdio: 'inherit'
105
});
106
107
// The node gyp package got installed using the above npm command using the gyp/package.json
108
// file checked into our repository. So from that point it is safe to construct the path
109
// to that executable
110
const node_gyp = process.platform === 'win32'
111
? path.join(import.meta.dirname, 'gyp', 'node_modules', '.bin', 'node-gyp.cmd')
112
: path.join(import.meta.dirname, 'gyp', 'node_modules', '.bin', 'node-gyp');
113
114
const local = getHeaderInfo(path.join(import.meta.dirname, '..', '..', '.npmrc'));
115
const remote = getHeaderInfo(path.join(import.meta.dirname, '..', '..', 'remote', '.npmrc'));
116
117
if (local !== undefined) {
118
// Both disturl and target come from a file checked into our repository
119
child_process.execFileSync(node_gyp, ['install', '--dist-url', local.disturl, local.target], { shell: true });
120
}
121
122
if (remote !== undefined) {
123
// Both disturl and target come from a file checked into our repository
124
child_process.execFileSync(node_gyp, ['install', '--dist-url', remote.disturl, remote.target], { shell: true });
125
}
126
127
// On Linux, apply a patch to the downloaded headers
128
// Remove dependency on std::source_location to avoid bumping the required GCC version to 11+
129
// Refs https://chromium-review.googlesource.com/c/v8/v8/+/6879784
130
if (process.platform === 'linux') {
131
const homedir = os.homedir();
132
const cachePath = process.env.XDG_CACHE_HOME || path.join(homedir, '.cache');
133
const nodeGypCache = path.join(cachePath, 'node-gyp');
134
const localHeaderPath = path.join(nodeGypCache, local!.target, 'include', 'node');
135
if (fs.existsSync(localHeaderPath)) {
136
console.log('Applying v8-source-location.patch to', localHeaderPath);
137
try {
138
child_process.execFileSync('patch', ['-p0', '-i', path.join(import.meta.dirname, 'gyp', 'custom-headers', 'v8-source-location.patch')], {
139
cwd: localHeaderPath
140
});
141
} catch (error) {
142
throw new Error(`Error applying v8-source-location.patch: ${(error as Error).message}`);
143
}
144
}
145
}
146
}
147
148
function getHeaderInfo(rcFile: string): { disturl: string; target: string } | undefined {
149
const lines = fs.readFileSync(rcFile, 'utf8').split(/\r\n|\n/g);
150
let disturl: string | undefined;
151
let target: string | undefined;
152
for (const line of lines) {
153
let match = line.match(/\s*disturl=*\"(.*)\"\s*$/);
154
if (match !== null && match.length >= 1) {
155
disturl = match[1];
156
}
157
match = line.match(/\s*target=*\"(.*)\"\s*$/);
158
if (match !== null && match.length >= 1) {
159
target = match[1];
160
}
161
}
162
return disturl !== undefined && target !== undefined
163
? { disturl, target }
164
: undefined;
165
}
166
167