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

Fix npm_execpath for bundled release #2843

Merged
merged 1 commit into from
Mar 7, 2017
Merged

Fix npm_execpath for bundled release #2843

merged 1 commit into from
Mar 7, 2017

Conversation

kouhin
Copy link
Contributor

@kouhin kouhin commented Mar 6, 2017

Summary

Docker is using a bundled version of yarn, and the yarn.js is moved to /usr/local/bin/yarn.
However yarn always generates npm_execpath by the following code:

env.npm_execpath = path.join(__dirname, '..', '..', 'bin', 'yarn.js');

Then the generated npm_execpath with docker pre-installed yarn will be /bin/yarn.js which does not exist.

Test plan

Test 1: Bundled version:

Run:

docker run --rm -it node:7.7 /bin/bash -c 'echo "{\"scripts\": {\"test\": \"echo \$npm_execpath\"}}" > package.json && yarn test'

Output:

/usr/local/bin/yarn

Test 2: Common installed version:

docker run --rm -it node:7.7 /bin/bash -c 'curl -o- -L https://yarnpkg.com/install.sh | bash && echo "{\"scripts\": {\"test\": \"echo \$npm_execpath\"}}" > package.json && yarn test'

Output:

/root/.config/yarn/global/node_modules/yarn/bin/yarn.js

#fix #2345

@kouhin kouhin changed the title Use process.mainModule.filename as npm_execpath Fix npm_execpath for bundled release Mar 7, 2017
@bestander bestander merged commit 2f49dcf into yarnpkg:master Mar 7, 2017
@bestander
Copy link
Member

Awesome, another issue with bundled build fixed!
How about the other places where __dirname__ is used?

@kouhin
Copy link
Contributor Author

kouhin commented Mar 8, 2017

@bestander Thank you!
I'm trying to solve the __dirname problem of node-gyp (https://github.com/kouhin/yarn/blob/9349e01a8a2ddd44d363d2d2f92175d706a590a6/src/util/execute-lifecycle-script.js#L120). It's a little complex, but wish I could solve it.

@bestander
Copy link
Member

The trick with node-gyp is that we don't bundle it with the yarn.js build.
So it must be installed separately.

See discussions and workarounds:
#2266
nodejs/docker-node#346
#2828

The workaround to make Yarn install node-gyp and then yarn.js be able to access it.

skevy pushed a commit to expo/yarn that referenced this pull request Mar 8, 2017
@kouhin
Copy link
Contributor Author

kouhin commented Mar 9, 2017

@bestander Yes, I see. yarn global add node-gyp is a simple workaround, but what about this:

docker run --rm -it node:7.7 /bin/bash -c "yarn global add yarn && yarn add webworker-threads"

It will cause an error:

error /root/node_modules/webworker-threads: Command failed.
Exit code: 1
Command: sh
Arguments: -c node-gyp rebuild
Directory: /root/node_modules/webworker-threads
Output:
module.js:472
    throw err;
    ^

Error: Cannot find module '/root/.config/yarn/global/node_modules/yarn/node_modules/node-gyp/bin/node-gyp.js'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:425:7)
    at startup (bootstrap_node.js:146:9)
    at bootstrap_node.js:540:3

yarn global add yarn will install a common version of yarn with node-gyp,
yarn is installed in /root/.config/yarn/global/node_modules/yarn, however node-gyp is installed in /root/.config/yarn/global/node_modules/node-gyp.

Maybe this code causes the error or I'm wrong?
https://github.com/yarnpkg/yarn/blob/master/src/util/execute-lifecycle-script.js#L120

  // add node-gyp
  pathParts.unshift(path.join(__dirname, '..', '..', 'bin', 'node-gyp-bin'));

@kouhin
Copy link
Contributor Author

kouhin commented Mar 9, 2017

I may made a mistake.
This one works:

docker run --rm -it node:7.7 /bin/bash -c "yarn global add yarn && echo \"env\" >> /root/.config/yarn/global/node_modules/yarn/bin/node-gyp-bin/node-gyp && yarn add webworker-threads"

Why?

@bestander
Copy link
Member

bestander commented Mar 9, 2017 via email

@kouhin
Copy link
Contributor Author

kouhin commented Mar 9, 2017

Yes, yarn global add yarn
A symlink will be created at /usr/local/bin and override the bundled one.

/usr/local/bin/yarn -> ../../../root/.config/yarn/global/node_modules/yarn/bin/yarn.js

@bestander
Copy link
Member

I haven't followed the log through yet but I would advise against yarn global add yarn, it confuses what yarn binary is used.
What if you do yarn global add yarn 2 or 3 times in a row?

@kouhin
Copy link
Contributor Author

kouhin commented Mar 10, 2017

@bestander It's easy to confirm what yarn binary is used.

❯ docker run --rm -it node:7.7 /bin/bash
root@2ce437d7a73d:/# which yarn
/usr/local/bin/yarn
root@2ce437d7a73d:/# cd /usr/local/bin
root@2ce437d7a73d:/usr/local/bin# ls -al
total 37932
drwxrwxr-x  2  500   500     4096 Mar  2 19:40 .
drwxrwsr-x 18 root staff     4096 Mar  2 19:40 ..
-rwxrwxr-x  1  500   500 35393839 Mar  2 00:23 node
lrwxrwxrwx  1 root root        19 Mar  2 19:40 nodejs -> /usr/local/bin/node
lrwxrwxrwx  1  500   500       38 Mar  2 00:23 npm -> ../lib/node_modules/npm/bin/npm-cli.js
-rwxr-xr-x  1 root root   3432504 Mar  2 19:40 yarn

The default yarn is a bundled yarn binary, and then

root@2ce437d7a73d:/usr/local/bin# yarn global add yarn
yarn global v0.21.3
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Installed "yarn@0.21.3" with binaries:
      - yarn
      - yarnpkg
Done in 12.15s.
root@2ce437d7a73d:/usr/local/bin# which yarn
/usr/local/bin/yarn
root@2ce437d7a73d:/usr/local/bin# ls -al
total 34584
drwxrwxr-x  2  500   500     4096 Mar 10 14:59 .
drwxrwsr-x 19 root staff     4096 Mar 10 14:59 ..
-rwxrwxr-x  1  500   500 35393839 Mar  2 00:23 node
lrwxrwxrwx  1 root root        19 Mar  2 19:40 nodejs -> /usr/local/bin/node
lrwxrwxrwx  1  500   500       38 Mar  2 00:23 npm -> ../lib/node_modules/npm/bin/npm-cli.js
lrwxrwxrwx  1 root root        63 Mar 10 14:59 yarn -> ../../../root/.config/yarn/global/node_modules/yarn/bin/yarn.js
lrwxrwxrwx  1 root root        63 Mar 10 14:59 yarnpkg -> ../../../root/.config/yarn/global/node_modules/yarn/bin/yarn.js

Common yarn overrides pre-installed version.

We can also confirm the version, and install webworker-threads again.

❯ docker run --rm -it node:7.7.0 /bin/bash -c "echo '==BEFORE==' && yarn -v && echo '==== INSTALL YARN ====' && yarn global add yarn@0.22.0 && echo '==AFTER==' && yarn -v && yarn add webworker-threads"
==BEFORE==
yarn install v0.21.3
info No lockfile found.
[1/4] Resolving packages...
success Nothing to install.
success Saved lockfile.
Done in 0.14s.
==== INSTALL YARN ====
yarn global v0.21.3
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Installed "yarn@0.22.0" with binaries:
      - yarn
      - yarnpkg
Done in 9.92s.
==AFTER==
yarn install v0.22.0
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.10s.
yarn add v0.22.0
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
[1/1] ⠂ webworker-threads
[-/1] ⠂ waiting...
error /node_modules/webworker-threads: Command failed.
Exit code: 1
Command: sh
Arguments: -c node-gyp rebuild
Directory: /node_modules/webworker-threads
Output:
module.js:472
    throw err;
    ^

Error: Cannot find module '/root/.config/yarn/global/node_modules/yarn/node_modules/node-gyp/bin/node-gyp.js'
    at Function.Module._resolveFilename (module.js:470:15)
    at Function.Module._load (module.js:418:25)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:425:7)
    at startup (bootstrap_node.js:146:9)
    at bootstrap_node.js:540:3
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

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.

$npm_execpath value is incorrect in the .js bundle release
2 participants