Skip to content

Symlink node_modules.asar to node_modules in lib/vscode #2482

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

Merged
merged 3 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ release-packages/
release-gcp/
release-images/
node_modules
/lib/vscode/node_modules.asar
node-*
/plugins
/lib/coder-cloud-agent
Expand Down
4 changes: 4 additions & 0 deletions ci/build/build-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ EOF
# yarn to fetch node_modules if necessary without build scripts running.
# We cannot use --no-scripts because we still want dependent package scripts to run.
jq 'del(.scripts)' < "$VSCODE_SRC_PATH/package.json" > "$VSCODE_OUT_PATH/package.json"

pushd "$VSCODE_OUT_PATH"
symlink_asar
popd
}

main "$@"
10 changes: 10 additions & 0 deletions ci/build/npm-postinstall.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ main() {
vscode_yarn() {
cd lib/vscode
yarn --production --frozen-lockfile

# This is a copy of symlink_asar in ../lib.sh. Look there for details.
if [ ! -e node_modules.asar ]; then
if [ "${WINDIR-}" ]; then
mklink /J node_modules.asar node_modules
else
ln -s node_modules node_modules.asar
fi
fi

cd extensions
yarn --production --frozen-lockfile
for ext in */; do
Expand Down
14 changes: 14 additions & 0 deletions ci/dev/postinstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail

main() {
cd "$(dirname "$0")/../.."
source ./ci/lib.sh

cd lib/vscode
yarn ${CI+--frozen-lockfile}

symlink_asar
}

main "$@"
20 changes: 20 additions & 0 deletions ci/lib.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
set -euo pipefail

pushd() {
builtin pushd "$@" > /dev/null
Expand Down Expand Up @@ -93,3 +94,22 @@ export OS
# RELEASE_PATH is the destination directory for the release from the root.
# Defaults to release
RELEASE_PATH="${RELEASE_PATH-release}"

# VS Code bundles some modules into an asar which is an archive format that
# works like tar. It then seems to get unpacked into node_modules.asar.
#
# I don't know why they do this but all the dependencies they bundle already
# exist in node_modules so just symlink it. We have to do this since not only VS
# Code itself but also extensions will look specifically in this directory for
# files (like the ripgrep binary or the oniguruma wasm).
symlink_asar() {
if [ ! -e node_modules.asar ]; then
if [ "${WINDIR-}" ]; then
# mklink takes the link name first.
mklink /J node_modules.asar node_modules
else
# ln takes the link name second.
ln -s node_modules node_modules.asar
fi
fi
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,12 @@ const args = minimist(process.argv.slice(2), {
const Module = require.__$__nodeRequire('module') as any;
const originalLoad = Module._load;

Module._load = function (request: string, parent: object, isMain: boolean) {
Module._load = function (request: string) {
if (request === 'natives') {
throw new Error('Either the extension or a NPM dependency is using the "natives" node module which is unsupported as it can cause a crash of the extension host. Click [here](https://go.microsoft.com/fwlink/?linkid=871887) to find out more');
}

// NOTE@coder: Map node_module.asar requests to regular node_modules.
return originalLoad.apply(this, [request.replace(/node_modules\.asar(\.unpacked)?/, 'node_modules'), parent, isMain]);
return originalLoad.apply(this, arguments);
};
})();

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"release:github-assets": "./ci/build/release-github-assets.sh",
"test:standalone-release": "./ci/build/test-standalone-release.sh",
"package": "./ci/build/build-packages.sh",
"postinstall": "cd lib/vscode && yarn ${CI+--frozen-lockfile}",
"postinstall": "./ci/dev/postinstall.sh",
"_____": "",
"fmt": "./ci/dev/fmt.sh",
"lint": "./ci/dev/lint.sh",
Expand Down