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
+14 -1
View File
@@ -83,6 +83,16 @@ clear_non_sh_env() {
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() {
local use_dot_prefix="$1"
@@ -241,7 +251,7 @@ check_nounset() {
}
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
timeoutCmd=""
fi
@@ -277,6 +287,7 @@ main() {
handle_task_start "${task_shell_params[@]}"
check_file "${task_shell_params[@]}"
append_node_dependency_path
if [[ $isJsOrPythonFile == 'false' ]]; then
run_task_before "${task_shell_params[@]}"
fi
@@ -286,6 +297,8 @@ main "${task_shell_params[@]}"
if [[ "$set_u_on" == 'true' ]]; then
set -u
fi
export NODE_PATH="${PREV_NODE_PATH}"
unset QL_NODE_GLOBAL_PATH
if [[ $isJsOrPythonFile == 'true' ]]; then
export NODE_OPTIONS="${PREV_NODE_OPTIONS}"
export PYTHONPATH="${PREV_PYTHONPATH}"
+31
View File
@@ -1,7 +1,36 @@
const { execSync } = require('child_process');
const Module = require('module');
const path = require('path');
const client = require('./client.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) {
const tempRangeStr = rangeStr
.trim()
@@ -113,6 +142,8 @@ try {
return;
}
preferGlobalNodeModules();
process.on('SIGTERM', (code) => {
process.exit(15);
});
+1 -11
View File
@@ -65,17 +65,7 @@ link_name=(
)
init_env() {
local pnpm_global_path=$(pnpm root -g 2>/dev/null)
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 NODE_PATH="/usr/local/bin:/usr/local/lib/node_modules"
export PYTHONUNBUFFERED=1
}