Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python symlink to path (for non-Windows OSes only) #2362

Merged
merged 6 commits into from
Jun 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ function build (gyp, argv, callback) {
}
}

if (!win) {
// Add build-time dependency symlinks (such as Python) to PATH
const buildBinsDir = path.resolve('build', 'node_gyp_bins')
process.env.PATH = `${buildBinsDir}:${process.env.PATH}`
log.verbose('bin symlinks', `adding symlinks (such as Python), at "${buildBinsDir}", to PATH`)
cclauss marked this conversation as resolved.
Show resolved Hide resolved
}

var proc = gyp.spawn(command, argv)
proc.on('exit', onExit)
}
Expand Down
25 changes: 24 additions & 1 deletion lib/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ if (win) {
function configure (gyp, argv, callback) {
var python
var buildDir = path.resolve('build')
var buildBinsDir = path.join(buildDir, 'node_gyp_bins')
var configNames = ['config.gypi', 'common.gypi']
var configs = []
var nodeDir
Expand Down Expand Up @@ -73,7 +74,9 @@ function configure (gyp, argv, callback) {

function createBuildDir () {
log.verbose('build dir', 'attempting to create "build" dir: %s', buildDir)
fs.mkdir(buildDir, { recursive: true }, function (err, isNew) {

const deepestBuildDirSubdirectory = win ? buildDir : buildBinsDir
fs.mkdir(deepestBuildDirSubdirectory, { recursive: true }, function (err, isNew) {
if (err) {
return callback(err)
}
Expand All @@ -84,11 +87,31 @@ function configure (gyp, argv, callback) {
findVisualStudio(release.semver, gyp.opts.msvs_version,
createConfigFile)
} else {
createPythonSymlink()
createConfigFile()
}
})
}

function createPythonSymlink () {
const symlinkDestination = path.join(buildBinsDir, 'python3')

log.verbose('python symlink', `creating symlink to "${python}" at "${symlinkDestination}"`)
cclauss marked this conversation as resolved.
Show resolved Hide resolved

fs.unlink(symlinkDestination, function (err) {
if (err && err.code !== 'ENOENT') {
log.verbose('python symlink', 'error when attempting to remove existing symlink')
log.verbose('python symlink', err.stack, 'errno: ' + err.errno)
}
fs.symlink(python, symlinkDestination, function (err) {
if (err) {
log.verbose('python symlink', 'error when attempting to create Python symlink')
log.verbose('python symlink', err.stack, 'errno: ' + err.errno)
}
})
})
}

function createConfigFile (err, vsInfo) {
if (err) {
return callback(err)
Expand Down
4 changes: 3 additions & 1 deletion test/test-configure-python.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const configure = requireInject('../lib/configure', {
mkdir: function (dir, options, cb) { cb() },
promises: {
writeFile: function (file, data) { return Promise.resolve(null) }
}
},
unlink: function (path, cb) { cb() },
symlink: function (target, path, cb) { cb() }
}
})

Expand Down