Skip to content

Commit 57d58c8

Browse files
committed
fix: isolate task node dependencies
1 parent bb6d436 commit 57d58c8

5 files changed

Lines changed: 48 additions & 14 deletions

File tree

docker/310.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ ENV PNPM_HOME=${QL_DIR}/data/dep_cache/node \
7373
HOME=/root
7474

7575
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PNPM_HOME}:${PYTHON_HOME}/bin:${HOME}/bin \
76-
NODE_PATH=/usr/local/bin:/usr/local/lib/node_modules:${PNPM_HOME}/global/5/node_modules \
76+
NODE_PATH=/usr/local/bin:/usr/local/lib/node_modules \
7777
PIP_CACHE_DIR=${PYTHON_HOME}/pip \
7878
PYTHONPATH=${PYTHON_HOME}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}/site-packages
7979

docker/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ ENV PNPM_HOME=${QL_DIR}/data/dep_cache/node \
7373
HOME=/root
7474

7575
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PNPM_HOME}:${PYTHON_HOME}/bin:${HOME}/bin \
76-
NODE_PATH=/usr/local/bin:/usr/local/lib/node_modules:${PNPM_HOME}/global/5/node_modules \
76+
NODE_PATH=/usr/local/bin:/usr/local/lib/node_modules \
7777
PIP_CACHE_DIR=${PYTHON_HOME}/pip \
7878
PYTHONPATH=${PYTHON_HOME}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}/site-packages
7979

shell/otask.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ clear_non_sh_env() {
8383
fi
8484
}
8585

86+
append_node_dependency_path() {
87+
export PREV_NODE_PATH="${NODE_PATH:=}"
88+
89+
local pnpm_global_path=$(pnpm root -g 2>/dev/null)
90+
if [[ -n "$pnpm_global_path" ]]; then
91+
export QL_NODE_GLOBAL_PATH="$pnpm_global_path"
92+
export NODE_PATH="${NODE_PATH:+${NODE_PATH}:}${pnpm_global_path}"
93+
fi
94+
}
95+
8696
enter_script_workdir() {
8797
local use_dot_prefix="$1"
8898

@@ -241,7 +251,7 @@ check_nounset() {
241251
}
242252

243253
main() {
244-
if [[ $1 == *.js ]] || [[ $1 == *.py ]] || [[ $1 == *.pyc ]] || [[ $1 == *.sh ]] || [[ $1 == *.ts ]]; then
254+
if [[ $1 == *.js ]] || [[ $1 == *.mjs ]] || [[ $1 == *.py ]] || [[ $1 == *.pyc ]] || [[ $1 == *.sh ]] || [[ $1 == *.ts ]]; then
245255
if [[ $1 == *.sh ]]; then
246256
timeoutCmd=""
247257
fi
@@ -277,6 +287,7 @@ main() {
277287

278288
handle_task_start "${task_shell_params[@]}"
279289
check_file "${task_shell_params[@]}"
290+
append_node_dependency_path
280291
if [[ $isJsOrPythonFile == 'false' ]]; then
281292
run_task_before "${task_shell_params[@]}"
282293
fi
@@ -286,6 +297,8 @@ main "${task_shell_params[@]}"
286297
if [[ "$set_u_on" == 'true' ]]; then
287298
set -u
288299
fi
300+
export NODE_PATH="${PREV_NODE_PATH}"
301+
unset QL_NODE_GLOBAL_PATH
289302
if [[ $isJsOrPythonFile == 'true' ]]; then
290303
export NODE_OPTIONS="${PREV_NODE_OPTIONS}"
291304
export PYTHONPATH="${PREV_PYTHONPATH}"

shell/preload/sitecustomize.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,36 @@
11
const { execSync } = require('child_process');
2+
const Module = require('module');
3+
const path = require('path');
24
const client = require('./client.js');
35
require(`./env.js`);
46

7+
function preferGlobalNodeModules() {
8+
const { QL_NODE_GLOBAL_PATH } = process.env;
9+
if (!QL_NODE_GLOBAL_PATH || Module._qlGlobalPathPatched) {
10+
return;
11+
}
12+
13+
const originalResolveFilename = Module._resolveFilename;
14+
Module._resolveFilename = function (request, parent, isMain, options) {
15+
if (
16+
!Module.builtinModules.includes(request) &&
17+
!request.startsWith('node:') &&
18+
!request.startsWith('.') &&
19+
!path.isAbsolute(request)
20+
) {
21+
try {
22+
return originalResolveFilename.call(this, request, parent, isMain, {
23+
...options,
24+
paths: [QL_NODE_GLOBAL_PATH],
25+
});
26+
} catch (error) {}
27+
}
28+
29+
return originalResolveFilename.call(this, request, parent, isMain, options);
30+
};
31+
Module._qlGlobalPathPatched = true;
32+
}
33+
534
function expandRange(rangeStr, max) {
635
const tempRangeStr = rangeStr
736
.trim()
@@ -113,6 +142,8 @@ try {
113142
return;
114143
}
115144

145+
preferGlobalNodeModules();
146+
116147
process.on('SIGTERM', (code) => {
117148
process.exit(15);
118149
});

shell/share.sh

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,7 @@ link_name=(
6565
)
6666

6767
init_env() {
68-
local pnpm_global_path=$(pnpm root -g 2>/dev/null)
69-
export NODE_PATH="/usr/local/bin:/usr/local/lib/node_modules${pnpm_global_path:+:${pnpm_global_path}}"
70-
71-
# 如果存在 pnpm 全局路径,创建软链接
72-
if [[ -n "$pnpm_global_path" ]]; then
73-
# 确保目标目录存在
74-
mkdir -p "${dir_root}/node_modules"
75-
# 链接全局模块到项目的 node_modules
76-
ln -sf "${pnpm_global_path}/"* "${dir_root}/node_modules/" 2>/dev/null || true
77-
fi
78-
68+
export NODE_PATH="/usr/local/bin:/usr/local/lib/node_modules"
7969
export PYTHONUNBUFFERED=1
8070
}
8171

0 commit comments

Comments
 (0)