Skip to content

Conversation

@luhc228
Copy link
Collaborator

@luhc228 luhc228 commented May 26, 2021

背景

切换版本后返回管理页面,node 版本还是旧的

原因

调用 shell.which('node') 的时候,会以 process.env.PATH 作为环境变量,而当前的 process.env.PATH 中没有新安装的 node 的 PATH

@luhc228 luhc228 requested a review from alvinhui May 26, 2021 12:56
if (status === 'done') {
killChannelChildProcess(childProcessMap, installChannel);
} else if (status === 'success' && result && result.nodePath) {
process.env.PATH = `${result.nodePath.replace('/bin/node', '/bin')}${path.delimiter}${process.env.PATH}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 result.nodePath 返回的值有示例吗?是什么?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我添加了注释了

@alvinhui
Copy link
Contributor

alvinhui commented May 27, 2021

如果确实是这个原因的话,那么解决该问题的根治方法是不是每次调用 shell.which 的时候要保持 process.env 为最新?

@luhc228
Copy link
Collaborator Author

luhc228 commented May 27, 2021

如果确实是这个原因的话,那么解决该问题的根治方法是不是每次调用 shell.which 的时候要保持 process.env 为最新?

看了一下 node_modules/.bin/electron 的实现:

#!/usr/bin/env node

const electron = require('./');

const proc = require('child_process');

const child = proc.spawn(electron, process.argv.slice(2), { stdio: 'inherit', windowsHide: false });
child.on('close', function (code, signal) {
  if (code === null) {
    console.error(electron, 'exited with signal', signal);
    process.exit(1);
  }
  process.exit(code);
});

const handleTerminationSignal = function (signal) {
  process.on(signal, function signalHandler () {
    if (!child.killed) {
      child.kill(signal);
    }
  });
};

handleTerminationSignal('SIGINT');
handleTerminationSignal('SIGTERM');

这里使用 child_process.spawn() 启动一个子进程来启动应用,其中这个子进程的 process.env 是默认的 process.env,因为这里没有显式指定 env(具体详见 child_process 文档)。除非这里可以传入一直变化着的 env ?

@alvinhui alvinhui merged commit 3f3e8f2 into release-next May 28, 2021
@alvinhui alvinhui deleted the fix/node-version-not-update branch May 28, 2021 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants