Skip to content

Commit 152cd3d

Browse files
committed
fix(nodeenv): link nodejs.exe without mklink
mklink ran through cmd with an unquoted command line, so env paths with forward slashes or spaces broke it, and it needs elevation or Developer Mode. Its output was also printed twice and reported at ERROR level, although the environment was fine. Use os.symlink, fall back to a hard link, and only warn if both fail. Fixes ekalinin#303
1 parent d163302 commit 152cd3d

2 files changed

Lines changed: 69 additions & 5 deletions

File tree

‎nodeenv.py‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,10 +1355,19 @@ def install_activate(env_dir, args):
13551355

13561356
if not os.path.exists(shim_nodejs):
13571357
if is_WIN:
1358+
# a symlink needs elevation or Developer Mode, a hard link
1359+
# needs neither
1360+
# https://github.com/ekalinin/nodeenv/issues/303
13581361
try:
1359-
callit(['mklink', shim_nodejs, 'node.exe'], True, True)
1362+
os.symlink('node.exe', shim_nodejs)
13601363
except OSError:
1361-
logger.error('Error: Failed to create nodejs.exe link')
1364+
try:
1365+
os.link(join(bin_dir, 'node.exe'), shim_nodejs)
1366+
except OSError:
1367+
logger.warning(
1368+
'Could not create the nodejs.exe link; this is '
1369+
'harmless unless something on your system invokes '
1370+
'node as "nodejs"')
13621371
else:
13631372
os.symlink("node", shim_nodejs)
13641373

‎tests/test_install_activate.py‎

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,11 @@ def test_isolate_npm_shim_content(tmpdir):
423423
@pytest.fixture
424424
def fake_win():
425425
"""
426-
Pretend the host is Windows. install_activate() links nodejs.exe with
427-
mklink there, which exists on Windows only, so callit() is stubbed too.
426+
Pretend the host is Windows. The nodejs.exe link is stubbed out, so
427+
Scripts/ holds only the generated scripts.
428428
"""
429429
with mock.patch.object(nodeenv, 'is_WIN', True):
430-
with mock.patch.object(nodeenv, 'callit'):
430+
with mock.patch.object(os, 'symlink'):
431431
yield
432432

433433

@@ -581,3 +581,58 @@ def test_isolate_npm_node_system_shim_exports(tmpdir):
581581
env_dir = str(tmpdir)
582582
assert out.decode('utf-8').strip() == '|'.join((
583583
env_dir + '/.npm', env_dir + '/.npmrc', env_dir + '/.npm-init.js'))
584+
585+
586+
# nodejs.exe is linked with os.symlink/os.link, not with `mklink` through
587+
# cmd, which needed quoting and elevation
588+
# https://github.com/ekalinin/nodeenv/issues/303
589+
#
590+
# is_WIN is faked, so these run on every platform.
591+
592+
593+
def _install_win_node(tmpdir):
594+
bin_dir = tmpdir.join('Scripts')
595+
bin_dir.mkdir()
596+
bin_dir.join('node.exe').write('node')
597+
598+
with mock.patch.object(nodeenv, 'is_WIN', True), \
599+
mock.patch.object(sys, 'argv', ['nodeenv', str(tmpdir)]):
600+
opts = nodeenv.parse_args()
601+
nodeenv.install_activate(str(tmpdir), opts)
602+
return bin_dir
603+
604+
605+
@pytest.mark.skipif(nodeenv.is_WIN, reason='symlinks need privileges on win32')
606+
def test_win_nodejs_symlink_created(tmpdir):
607+
# forward slashes and a space broke the unquoted mklink command line
608+
env_dir = tmpdir.join('with space')
609+
env_dir.mkdir()
610+
611+
bin_dir = _install_win_node(env_dir)
612+
613+
nodejs_file = bin_dir.join('nodejs.exe')
614+
assert os.path.islink(str(nodejs_file))
615+
assert os.readlink(str(nodejs_file)) == 'node.exe'
616+
617+
618+
def test_win_nodejs_hardlink_when_symlink_fails(tmpdir):
619+
# no elevation and no Developer Mode: a hard link needs neither
620+
with mock.patch.object(os, 'symlink', side_effect=OSError):
621+
bin_dir = _install_win_node(tmpdir)
622+
623+
nodejs_file = bin_dir.join('nodejs.exe')
624+
assert not os.path.islink(str(nodejs_file))
625+
assert os.path.samefile(str(nodejs_file), str(bin_dir.join('node.exe')))
626+
627+
628+
def test_win_nodejs_link_failure_is_a_warning(tmpdir):
629+
with mock.patch.object(os, 'symlink', side_effect=OSError), \
630+
mock.patch.object(os, 'link', side_effect=OSError), \
631+
mock.patch.object(nodeenv.logger, 'warning') as m_warning, \
632+
mock.patch.object(nodeenv.logger, 'error') as m_error:
633+
bin_dir = _install_win_node(tmpdir)
634+
635+
assert not bin_dir.join('nodejs.exe').check()
636+
m_error.assert_not_called()
637+
m_warning.assert_called_once()
638+
assert 'nodejs.exe' in m_warning.call_args[0][0]

0 commit comments

Comments
 (0)