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

Finding a way to determine whether a package was run with yarn create or npx #5063

Closed
eliperelman opened this issue Dec 8, 2017 · 6 comments

Comments

@eliperelman
Copy link
Contributor

Do you want to request a feature or report a bug?

Feature

What is the current behavior?

When a user is running a package via yarn create or npx, the package is unable to determine whether it was yarn that launched the package. This is useful for running additional yarn commands in the repo over npm-based ones.

If the current behavior is a bug, please provide the steps to reproduce.

yarn create @neutrinojs/project scaffolds the project using npm commands because it doesn't know the user is using yarn to run it.

What is the expected behavior?

Should have a way to sniff that this is yarn-based, probably through an environment variable.

Please mention your node.js, yarn and operating system version.

❯ node --version
v9.2.0

❯ yarn --version
1.3.2

❯ uname -a
Darwin pro.local 17.2.0 Darwin Kernel Version 17.2.0: Fri Sep 29 18:27:05 PDT 2017; root:xnu-4570.20.62~3/RELEASE_X86_64 x86_64 # macOS High Sierra
@rally25rs
Copy link
Contributor

The npm_execpath env var will give you the path to the package manager that is executing:

~/Projects/yarn-test 🐒   yarn run env | grep npm_execpath
  "npm_execpath": "/Users/jvalore/.yarn/bin/yarn.js",

~/Projects/yarn-test 🐒   npm run env | grep npm_execpath
npm_execpath=/Users/jvalore/.nvm/versions/node/v8.5.0/lib/node_modules/npm/bin/npm-cli.js

which is useful if you need to run additional package manager commands. (node $npm_execpath install for example)


You could potentially also regex check the user agent string:

~/Projects/yarn-test 🐒   yarn run env | grep npm_config_user_agent
  "npm_config_user_agent": "yarn/1.3.2 npm/? node/v8.5.0 darwin x64",

~/Projects/yarn-test 🐒   npm run env | grep npm_config_user_agent
npm_config_user_agent=npm/5.5.1 node/v8.5.0 darwin x64

@rally25rs rally25rs self-assigned this Jan 2, 2018
@eliperelman
Copy link
Contributor Author

This post is about the create command, not the run scripts. The create command spawns a new node process and from what I could tell doesn't pass anything identifiable in the spawned env.

@eliperelman
Copy link
Contributor Author

@rally25rs
Copy link
Contributor

Ah yes, you're right. Sorry about that. It looks like we don't build up all the env vars when we spawn a yarn create shell.

I'm guessing at least some, if not all, of the normal npm_* env vars should be set during yarn create just like yarn run and other lifecycle scripts.

@rally25rs
Copy link
Contributor

rally25rs commented Jan 2, 2018

@arcanis it looks like you wrote yarn create originally. Was it intentional that there are no npm_* env vars? Do you think it would be a problem if we added them?

Or perhaps instead we just add some other env var just to indicate that Yarn ran this?

@eliperelman
Copy link
Contributor Author

@rally25rs @arcanis I have opened #7127 to at least get some environment parity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants