nginx / unit

NGINX Unit - universal web app server - a lightweight and versatile open source server that simplifies the application stack by natively executing application code across eight different programming language runtimes.
https://unit.nginx.org
Apache License 2.0
5.29k stars 325 forks source link

Make dirs for nodejs bin #979

Closed ruspaul013 closed 8 months ago

ruspaul013 commented 9 months ago

Hello!

I was trying to configure locally nodejs and I kept bumping into this error

mv build/src//usr/bin/node/unit-http-g/unit-http-1.31.1.tgz build//usr/bin/node-unit-http-g.tar.gz
mv: cannot move 'build/src//usr/bin/node/unit-http-g/unit-http-1.31.1.tgz' to 'build//usr/bin/node-unit-http-g.tar.gz': No such file or directory
make: *** [build/Makefile:2061: build//usr/bin/node-unit-http-g.tar.gz] Error 1

I added to the configuration file to create these directories.

local build commands :

./configure --prefix=/usr/ --state=/var/lib/unit --control=unix:/var/run/control.unit.sock \
--pid=/var/run/unit.pid --log=/var/log/unit.log --tmp=/var/tmp --user=unit --group=unit \
--tests --openssl --modules=/usr/lib/unit/modules --libdir=/usr/lib/ 
./configure nodejs --node=/usr/bin/node --npm=/usr/bin/npm --node-gyp=/usr/bin/node-gyp

make
make install
ac000 commented 9 months ago

Hello!

I was trying to configure locally nodejs and I kept bumping into this error


mv build/src//usr/bin/node/unit-http-g/unit-http-1.31.1.tgz build//usr/bin/node-unit-http-g.tar.gz
mv: cannot move 'build/src//usr/bin/node/unit-http-g/unit-http-1.31.1.tgz' to 'build//usr/bin/node-unit-http-g.tar.gz': No such file or directory

This seems weird, not sure why all this is under usr/bin

But then I seem to have more severe issues... (I have managed to install it in the past, how I did it has currently escaped my mind...)

configure steps look like (This is on Fedora 38 and I have installed the nodejs & nodejs-devel packages and I found a node-gyp lying around...)

$ ./configure --prefix=/opt/unit \
            --modulesdir=/opt/unit/modules \
            --statedir=/opt/unit/state \
            --runstatedir=/opt/unit \
            --logdir=/opt/unit \
            --log=/opt/unit/unit.log \
            --pid=/opt/unit/unit.pid \
            --control=unix:/opt/unit/control.unit.sock
$ make -j
$ ./configure nodejs --node-gyp=/usr/lib/node_modules.rpmmoved/npm/bin/node-gyp-bin/node-gyp
configuring nodejs module
checking for node ... found
 + node version v18.18.2
checking for npm ... found
 + npm version 9.8.1
checking for node-gyp ... found
 + node-gyp version v9.1.0
$ make install

# Things seem to start off OK.

mv build/src/node/unit-http-g/unit-http-1.31.1.tgz build/node-unit-http-g.tar.gz

# Here we seem to have your mv command, but with a more sensible path.

# After building some stuff it quickly wanders off into the woods however...

npm install -g --unsafe-perm /home/andrew/src/unit/build/node-unit-http-g.tar.gz
npm ERR! code EACCES
npm ERR! syscall mkdir
npm ERR! path /usr/local/lib/node_modules
npm ERR! errno -13
npm ERR! Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules'
npm ERR!  [Error: EACCES: permission denied, mkdir '/usr/local/lib/node_modules'] {
npm ERR!   errno: -13,
npm ERR!   code: 'EACCES',
npm ERR!   syscall: 'mkdir',
npm ERR!   path: '/usr/local/lib/node_modules'
npm ERR! }

# It should not be touching system directories.

The immediate question in relation to your issue is why did node build the http package where it did?

Here's the preceding lines from my output before it does its thing with it

mkdir -p build/src/node/unit-http-g
cp -rp src/nodejs/unit-http/* build/src/node/unit-http-g/
cp -p build/src/node/version.h build/src/node/package.json build/src/node/unit-http-g/
mv build/src/node/unit-http-g/binding_pub.gyp build/src/node/unit-http-g/binding.gyp
cd build/src/node/unit-http-g && npm pack
# A bunch of npm output
unit-http-1.31.1.tgz
mv build/src/node/unit-http-g/unit-http-1.31.1.tgz build/node-unit-http-g.tar.gz
ruspaul013 commented 9 months ago

@ac000 Thanks for the reply!

I tried what you posted and everything works. I saw that if I add ./configure nodejs --node=/usr/bin/node, then unit-http will take /usr/bin prefix and the error above will appear.

Is this the expected behavior? Feel free to close it if everything is working as expected.

ruspaul013 commented 9 months ago

I was looking into the code and I found this https://github.com/nginx/unit/blob/master/auto/modules/nodejs#L124. can the directory be created here, in case someone uses the option --node in the configuration?

ac000 commented 9 months ago

Hi,

I was looking into the code and I found this https://github.com/nginx/unit/blob/master/auto/modules/nodejs#L124. can

Right, the problem is we're conflating the use of NXT_NODE. Its use is to inform the location of the node binary and its various sub-commands.

But then we also use it for a directory location, which is just wrong.

Even this seems wrong

116 if grep ^$NXT_NODE: $NXT_MAKEFILE 2>&1 > /dev/null; then                    
117     $echo                                                                   
118     $echo $0: error: duplicate \"$NXT_NODE\" package configured.            
119     $echo                                                                   
120     exit 1;                                                                 
121 fi 

As if you use --node=/usr/bin/node and then try it again you'll get

./confiogure: error: duplicate "/usr/bin/node" package configured. 

Which seems wrong to me... unless it's trying to cater for building multiple nodejs language modules of different versions... but then that's normally handled via a --module option.

the directory be created here, in case someone uses the option --node in the configuration?

I don't think we need to create any directories, we just need to fix the above.

You could try the following patch. It seems to work for both

./configure nodejs --node-gyp=/usr/lib/node_modules.rpmmoved/npm/bin/node-gyp-bin/node-gyp --local=/opt/unit/node

and

./configure nodejs --node=/usr/bin/node --node-gyp=/usr/lib/node_modules.rpmmoved/npm/bin/node-gyp-bin/node-gyp --local=/opt/unit/node
diff --git a/auto/modules/nodejs b/auto/modules/nodejs
index 968f3fdf..b9250e5a 100644
--- a/auto/modules/nodejs
+++ b/auto/modules/nodejs
@@ -1,6 +1,7 @@

 # Copyright (C) NGINX, Inc.

+NXT_NODE_MODULE=nodejs

 shift

@@ -115,18 +116,18 @@ fi

 if grep ^$NXT_NODE: $NXT_MAKEFILE 2>&1 > /dev/null; then
     $echo
-    $echo $0: error: duplicate \"$NXT_NODE\" package configured.
+    $echo $0: error: duplicate \"$NXT_NODE_MODULE\" package configured.
     $echo
     exit 1;
 fi

-NXT_NODE_TMP=${NXT_BUILD_DIR}/src/${NXT_NODE}/unit-http
-NXT_NODE_TMP_G=${NXT_BUILD_DIR}/src/${NXT_NODE}/unit-http-g
-NXT_NODE_TARBALL=${NXT_BUILD_DIR}/${NXT_NODE}-unit-http.tar.gz
-NXT_NODE_TARBALL_G=${NXT_BUILD_DIR}/${NXT_NODE}-unit-http-g.tar.gz
-NXT_NODE_VERSION_FILE=${NXT_BUILD_DIR}/src/${NXT_NODE}/version.h
-NXT_NODE_PACKAGE_FILE=${NXT_BUILD_DIR}/src/${NXT_NODE}/package.json
+NXT_NODE_TMP=${NXT_BUILD_DIR}/src/${NXT_NODE_MODULE}/unit-http
+NXT_NODE_TMP_G=${NXT_BUILD_DIR}/src/${NXT_NODE_MODULE}/unit-http-g
+NXT_NODE_TARBALL=${NXT_BUILD_DIR}/${NXT_NODE_MODULE}-unit-http.tar.gz
+NXT_NODE_TARBALL_G=${NXT_BUILD_DIR}/${NXT_NODE_MODULE}-unit-http-g.tar.gz
+NXT_NODE_VERSION_FILE=${NXT_BUILD_DIR}/src/${NXT_NODE_MODULE}/version.h
+NXT_NODE_PACKAGE_FILE=${NXT_BUILD_DIR}/src/${NXT_NODE_MODULE}/package.json
 NXT_NODE_EXPORTS="export UNIT_SRC_PATH=${PWD}/src \
     && export UNIT_BUILD_PATH=${PWD}/${NXT_BUILD_DIR} \
     && export UNIT_LIB_STATIC_PATH=${PWD}/${NXT_BUILD_DIR}/lib/libunit.a"
@@ -166,11 +167,11 @@ ${NXT_NODE}-copy-g: ${NXT_NODE_VERSION_FILE} ${NXT_NODE_PACKAGE_FILE}
        mv ${NXT_NODE_TMP_G}/binding_pub.gyp ${NXT_NODE_TMP_G}/binding.gyp

 ${NXT_NODE_VERSION_FILE}: ${NXT_VERSION_H}
-       mkdir -p ${NXT_BUILD_DIR}/src/${NXT_NODE}
+       mkdir -p ${NXT_BUILD_DIR}/src/${NXT_NODE_MODULE}
        $echo '#define NXT_NODE_VERNUM \$(NXT_VERNUM)' > $NXT_NODE_VERSION_FILE

 ${NXT_NODE_PACKAGE_FILE}: ${NXT_VERSION_H} src/nodejs/unit-http/package.json
-       mkdir -p ${NXT_BUILD_DIR}/src/${NXT_NODE}
+       mkdir -p ${NXT_BUILD_DIR}/src/${NXT_NODE_MODULE}
        sed -e "s|%%VERSION%%|\$(NXT_VERSION)|" \
                src/nodejs/unit-http/package.json > ${NXT_NODE_PACKAGE_FILE}

@@ -208,9 +209,9 @@ ${NXT_NODE}-local-install: ${NXT_NODE_TARBALL} ${NXT_NODE}-local-check \
        ${NXT_NPM} install ${PWD}/${NXT_NODE_TARBALL}

-${NXT_NODE}-build: ${NXT_NODE}
+${NXT_NODE}-build: ${NXT_NODE_MODULE}

-${NXT_NODE}-publish: ${NXT_NODE}
+${NXT_NODE}-publish: ${NXT_NODE_MODULE}
        cd ${NXT_NODE_TMP} && ${NXT_NPM} publish

 END
ruspaul013 commented 9 months ago

I tried it and it's working for my case.

ac000 commented 9 months ago

@ruspaul013

Thanks for testing and bringing this issue up.

I shall leave this PR open for the time being...

ac000 commented 8 months ago

Here's a more minimal fix, the whole thing looks a little wonkey but this is less likely to put the frighteners on people!

diff --git a/auto/modules/nodejs b/auto/modules/nodejs
index 968f3fdf..472fb3be 100644
--- a/auto/modules/nodejs
+++ b/auto/modules/nodejs
@@ -123,8 +123,8 @@ fi

 NXT_NODE_TMP=${NXT_BUILD_DIR}/src/${NXT_NODE}/unit-http
 NXT_NODE_TMP_G=${NXT_BUILD_DIR}/src/${NXT_NODE}/unit-http-g
-NXT_NODE_TARBALL=${NXT_BUILD_DIR}/${NXT_NODE}-unit-http.tar.gz
-NXT_NODE_TARBALL_G=${NXT_BUILD_DIR}/${NXT_NODE}-unit-http-g.tar.gz
+NXT_NODE_TARBALL=${NXT_BUILD_DIR}/src/${NXT_NODE}-unit-http.tar.gz
+NXT_NODE_TARBALL_G=${NXT_BUILD_DIR}/src/${NXT_NODE}-unit-http-g.tar.gz
 NXT_NODE_VERSION_FILE=${NXT_BUILD_DIR}/src/${NXT_NODE}/version.h
 NXT_NODE_PACKAGE_FILE=${NXT_BUILD_DIR}/src/${NXT_NODE}/package.json
 NXT_NODE_EXPORTS="export UNIT_SRC_PATH=${PWD}/src \
ruspaul013 commented 8 months ago

hello @ac000 , I made the changes you suggested.

ac000 commented 8 months ago

Hi @ruspaul013

I gather this change works you?