mise-toolchain-env.mjs 1.1 KB

1234567891011121314151617181920212223242526
  1. import { delimiter, dirname } from "node:path";
  2. /**
  3. * 让所有子进程使用和启动器相同的 Node runtime。
  4. *
  5. * `mise run` 通过 shell 执行 TOML task;当另一套 Node 出现在
  6. * 子 shell 的 PATH 前面时,pnpm 会用错误的 runtime 启动 package script,
  7. * 即使 task 本身已经由 mise 选中了正确版本。把启动器的 Node 目录置首,
  8. * 可以在不依赖用户 home 目录布局的前提下固定整条子进程链路。
  9. */
  10. export function withPinnedNodePath(env, nodeExecutablePath) {
  11. const nodeDirectory = dirname(nodeExecutablePath);
  12. // Windows 的 Node 环境对象通常使用 `Path`,只读取大写 `PATH` 会把 pnpm.cmd
  13. // 所在目录从子进程环境中丢掉,导致 dev:desktop 的内部 pnpm 调用失败。
  14. const pathKey = typeof env.PATH === "string" ? "PATH" : "Path";
  15. const existingPath = typeof env[pathKey] === "string" ? env[pathKey] : "";
  16. const pathEntries = existingPath
  17. .split(delimiter)
  18. .filter(Boolean)
  19. .filter((entry) => entry !== nodeDirectory);
  20. return {
  21. ...env,
  22. [pathKey]: [nodeDirectory, ...pathEntries].join(delimiter),
  23. };
  24. }