Skip to content

Commit b9ddcd5

Browse files
authored
Add Python symlink to path (for non-Windows OSes only) (#2362)
* lib: create a Python symlink and add it to PATH Helps to ensure a version of Python validated by lib/find-python.js is used to run various Python scripts generated by gyp. Known to affect gyp-mac-tool, probably affects gyp-flock-tool as well. These Python scripts (such as `gyp-mac-tool`) are invoked directly, via the generated Makefile, so their shebang lines determine which Python binary is used to run them. The shebang lines of these scripts are all `#!/usr/bin/env python3`, so the first `python3` on the user's PATH will be used. By adding a symlink to the Python binary validated by find-python.js, and putting this symlink first on the PATH, we can ensure we use a compatible version of Python to run these scripts. (Only on Unix/Unix-like OSes. Symlinks are tricky on Windows, and Python isn't used at build-time anyhow on Windows, so this intervention isn't useful or necessary on Windows. A similar technique for Windows, no symlinks required, would be to make batch scripts which execute the target binary, much like what Node does for its bundled copy of npm on Windows.) * test: update mocked graceful-fs for configure test Add missing functions "unlink()" and "symlink()" to mocked module. * lib: log any errors when creating Python symlink Warn users about errors, but continue on in case the user does happen to have new enough Python on their PATH. (The symlinks are only meant to fix an issue in a corner case, where the user told `node-gyp` where new enough Python is, but it's not the first `python3` on their PATH. We should not introduce a new potential failure mode to all users when fixing this bug. So no hard errors during the symlink process.) * lib: improve error formatting for Python symlink Logging the entire error object shows the stack twice, and all the other information is contained in the stack. It also messes with the order of what is logged. Rather than logging a bunch of redundant information in a messy way, we can log only the stack. Logging it in a separate log.warn() also gets rid of an extra space character at the beginning of the line. * lib: restore err.errno to logs for symlink errors This info (err.errno) is the only piece of information in the error object that is not redundant to err.stack. * lib: use log.verbose, not log.warn These messages aren't important enough to be `log.warn`s. Log as verbose only; they will also appear in full error output.
1 parent 6f74c76 commit b9ddcd5

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

lib/build.js

+7
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ function build (gyp, argv, callback) {
185185
}
186186
}
187187

188+
if (!win) {
189+
// Add build-time dependency symlinks (such as Python) to PATH
190+
const buildBinsDir = path.resolve('build', 'node_gyp_bins')
191+
process.env.PATH = `${buildBinsDir}:${process.env.PATH}`
192+
log.verbose('bin symlinks', `adding symlinks (such as Python), at "${buildBinsDir}", to PATH`)
193+
}
194+
188195
var proc = gyp.spawn(command, argv)
189196
proc.on('exit', onExit)
190197
}

lib/configure.js

+24-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ if (win) {
1717
function configure (gyp, argv, callback) {
1818
var python
1919
var buildDir = path.resolve('build')
20+
var buildBinsDir = path.join(buildDir, 'node_gyp_bins')
2021
var configNames = ['config.gypi', 'common.gypi']
2122
var configs = []
2223
var nodeDir
@@ -73,7 +74,9 @@ function configure (gyp, argv, callback) {
7374

7475
function createBuildDir () {
7576
log.verbose('build dir', 'attempting to create "build" dir: %s', buildDir)
76-
fs.mkdir(buildDir, { recursive: true }, function (err, isNew) {
77+
78+
const deepestBuildDirSubdirectory = win ? buildDir : buildBinsDir
79+
fs.mkdir(deepestBuildDirSubdirectory, { recursive: true }, function (err, isNew) {
7780
if (err) {
7881
return callback(err)
7982
}
@@ -84,11 +87,31 @@ function configure (gyp, argv, callback) {
8487
findVisualStudio(release.semver, gyp.opts.msvs_version,
8588
createConfigFile)
8689
} else {
90+
createPythonSymlink()
8791
createConfigFile()
8892
}
8993
})
9094
}
9195

96+
function createPythonSymlink () {
97+
const symlinkDestination = path.join(buildBinsDir, 'python3')
98+
99+
log.verbose('python symlink', `creating symlink to "${python}" at "${symlinkDestination}"`)
100+
101+
fs.unlink(symlinkDestination, function (err) {
102+
if (err && err.code !== 'ENOENT') {
103+
log.verbose('python symlink', 'error when attempting to remove existing symlink')
104+
log.verbose('python symlink', err.stack, 'errno: ' + err.errno)
105+
}
106+
fs.symlink(python, symlinkDestination, function (err) {
107+
if (err) {
108+
log.verbose('python symlink', 'error when attempting to create Python symlink')
109+
log.verbose('python symlink', err.stack, 'errno: ' + err.errno)
110+
}
111+
})
112+
})
113+
}
114+
92115
function createConfigFile (err, vsInfo) {
93116
if (err) {
94117
return callback(err)

test/test-configure-python.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ const configure = requireInject('../lib/configure', {
1414
mkdir: function (dir, options, cb) { cb() },
1515
promises: {
1616
writeFile: function (file, data) { return Promise.resolve(null) }
17-
}
17+
},
18+
unlink: function (path, cb) { cb() },
19+
symlink: function (target, path, cb) { cb() }
1820
}
1921
})
2022

0 commit comments

Comments
 (0)