fix: isolate task node dependencies

This commit is contained in:
whyour 2026-05-24 01:40:07 +08:00
parent bb6d436c19
commit 57d58c871e
5 changed files with 48 additions and 14 deletions

View File

@ -73,7 +73,7 @@ ENV PNPM_HOME=${QL_DIR}/data/dep_cache/node \
HOME=/root HOME=/root
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PNPM_HOME}:${PYTHON_HOME}/bin:${HOME}/bin \ ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PNPM_HOME}:${PYTHON_HOME}/bin:${HOME}/bin \
NODE_PATH=/usr/local/bin:/usr/local/lib/node_modules:${PNPM_HOME}/global/5/node_modules \ NODE_PATH=/usr/local/bin:/usr/local/lib/node_modules \
PIP_CACHE_DIR=${PYTHON_HOME}/pip \ PIP_CACHE_DIR=${PYTHON_HOME}/pip \
PYTHONPATH=${PYTHON_HOME}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}/site-packages PYTHONPATH=${PYTHON_HOME}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}/site-packages

View File

@ -73,7 +73,7 @@ ENV PNPM_HOME=${QL_DIR}/data/dep_cache/node \
HOME=/root HOME=/root
ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PNPM_HOME}:${PYTHON_HOME}/bin:${HOME}/bin \ ENV PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:${PNPM_HOME}:${PYTHON_HOME}/bin:${HOME}/bin \
NODE_PATH=/usr/local/bin:/usr/local/lib/node_modules:${PNPM_HOME}/global/5/node_modules \ NODE_PATH=/usr/local/bin:/usr/local/lib/node_modules \
PIP_CACHE_DIR=${PYTHON_HOME}/pip \ PIP_CACHE_DIR=${PYTHON_HOME}/pip \
PYTHONPATH=${PYTHON_HOME}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}/site-packages PYTHONPATH=${PYTHON_HOME}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}:${PYTHON_HOME}/lib/python${PYTHON_SHORT_VERSION}/site-packages

View File

@ -83,6 +83,16 @@ clear_non_sh_env() {
fi fi
} }
append_node_dependency_path() {
export PREV_NODE_PATH="${NODE_PATH:=}"
local pnpm_global_path=$(pnpm root -g 2>/dev/null)
if [[ -n "$pnpm_global_path" ]]; then
export QL_NODE_GLOBAL_PATH="$pnpm_global_path"
export NODE_PATH="${NODE_PATH:+${NODE_PATH}:}${pnpm_global_path}"
fi
}
enter_script_workdir() { enter_script_workdir() {
local use_dot_prefix="$1" local use_dot_prefix="$1"
@ -241,7 +251,7 @@ check_nounset() {
} }
main() { main() {
if [[ $1 == *.js ]] || [[ $1 == *.py ]] || [[ $1 == *.pyc ]] || [[ $1 == *.sh ]] || [[ $1 == *.ts ]]; then if [[ $1 == *.js ]] || [[ $1 == *.mjs ]] || [[ $1 == *.py ]] || [[ $1 == *.pyc ]] || [[ $1 == *.sh ]] || [[ $1 == *.ts ]]; then
if [[ $1 == *.sh ]]; then if [[ $1 == *.sh ]]; then
timeoutCmd="" timeoutCmd=""
fi fi
@ -277,6 +287,7 @@ main() {
handle_task_start "${task_shell_params[@]}" handle_task_start "${task_shell_params[@]}"
check_file "${task_shell_params[@]}" check_file "${task_shell_params[@]}"
append_node_dependency_path
if [[ $isJsOrPythonFile == 'false' ]]; then if [[ $isJsOrPythonFile == 'false' ]]; then
run_task_before "${task_shell_params[@]}" run_task_before "${task_shell_params[@]}"
fi fi
@ -286,6 +297,8 @@ main "${task_shell_params[@]}"
if [[ "$set_u_on" == 'true' ]]; then if [[ "$set_u_on" == 'true' ]]; then
set -u set -u
fi fi
export NODE_PATH="${PREV_NODE_PATH}"
unset QL_NODE_GLOBAL_PATH
if [[ $isJsOrPythonFile == 'true' ]]; then if [[ $isJsOrPythonFile == 'true' ]]; then
export NODE_OPTIONS="${PREV_NODE_OPTIONS}" export NODE_OPTIONS="${PREV_NODE_OPTIONS}"
export PYTHONPATH="${PREV_PYTHONPATH}" export PYTHONPATH="${PREV_PYTHONPATH}"

View File

@ -1,7 +1,36 @@
const { execSync } = require('child_process'); const { execSync } = require('child_process');
const Module = require('module');
const path = require('path');
const client = require('./client.js'); const client = require('./client.js');
require(`./env.js`); require(`./env.js`);
function preferGlobalNodeModules() {
const { QL_NODE_GLOBAL_PATH } = process.env;
if (!QL_NODE_GLOBAL_PATH || Module._qlGlobalPathPatched) {
return;
}
const originalResolveFilename = Module._resolveFilename;
Module._resolveFilename = function (request, parent, isMain, options) {
if (
!Module.builtinModules.includes(request) &&
!request.startsWith('node:') &&
!request.startsWith('.') &&
!path.isAbsolute(request)
) {
try {
return originalResolveFilename.call(this, request, parent, isMain, {
...options,
paths: [QL_NODE_GLOBAL_PATH],
});
} catch (error) {}
}
return originalResolveFilename.call(this, request, parent, isMain, options);
};
Module._qlGlobalPathPatched = true;
}
function expandRange(rangeStr, max) { function expandRange(rangeStr, max) {
const tempRangeStr = rangeStr const tempRangeStr = rangeStr
.trim() .trim()
@ -113,6 +142,8 @@ try {
return; return;
} }
preferGlobalNodeModules();
process.on('SIGTERM', (code) => { process.on('SIGTERM', (code) => {
process.exit(15); process.exit(15);
}); });

View File

@ -65,17 +65,7 @@ link_name=(
) )
init_env() { init_env() {
local pnpm_global_path=$(pnpm root -g 2>/dev/null) export NODE_PATH="/usr/local/bin:/usr/local/lib/node_modules"
export NODE_PATH="/usr/local/bin:/usr/local/lib/node_modules${pnpm_global_path:+:${pnpm_global_path}}"
# 如果存在 pnpm 全局路径,创建软链接
if [[ -n "$pnpm_global_path" ]]; then
# 确保目标目录存在
mkdir -p "${dir_root}/node_modules"
# 链接全局模块到项目的 node_modules
ln -sf "${pnpm_global_path}/"* "${dir_root}/node_modules/" 2>/dev/null || true
fi
export PYTHONUNBUFFERED=1 export PYTHONUNBUFFERED=1
} }