Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Preferentially Execute Local
wp-env
#50980Preferentially Execute Local
wp-env
#50980Changes from 5 commits
ccef1f7
f44e63a
a06252f
9dbf5e2
32890b1
b1baba3
c359fd4
File filter
Filter by extension
Conversations
Jump to
There are no files selected for viewing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we use
require.resolve( '@wordpress/env' )
instead? I think this will throw an error if the module can't be found, in which case we could revert to../lib/cli.js
. In my testing from a nested directory in a project, this is the result:(And then we can change env to cli.)
require.resolve should handle more cases because it's using the Node module resolution algorithm, whereas the current approach probably won't fork in child directories
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
path.resolve()
in this case will just resolve an absolute path relative toprocess.cwd()
. Since all we're doing is creating a path string, thefs.existsSync( localPath )
check below will protect us from trying to do something with a module that doesn't exist.An interesting point though is that perhaps it might make sense be more explicit about what we're doing and use
path.join( process.cwd(), 'node_modules/@wordpress/env/lib/cli.js
? I'm going to make that change.I would worry that
require.resolve
is too heavy-handed. Node's module resolution algorithm includes searching the current directory and every parent directory for a module in anode_modules
directory. Imagine that a package has a.wp-env.json
built with a global version of X in mind, but, it's in a directory with a project at a higher-level that (for whatever reason) has a differentwp-env
version.Since
wp-env
commands only care about the current working directory, I think it makes sense for this override to only apply in the current one as well. For instance, even thoughnpx wp-env start
will find the root instance if ran in a subdirectory, the command won't actually work because there's no package there.Ultimately the question here is whether or not the user should expect it to find the current directory's package, or, if it should find the nearest one (traversing to the global install if it can't find one). Given that this is the typical behavior of NPM commands though, you might be right.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, you make a good point about wp-env being directory based. However, this makes me think of another scenario: monorepos! For example, I have a plugin in a monorepo -- the
.wp-env.json
file is under a path like./apps/this-plugin
. But node modules for that plugin would be stored in./node_modules
(since yarn will put all monorepo deps in the same place.) While I need to run.wp-env
from the apps/plugin directory, this new feature wouldn't work unless we used require.resolve.Also a good point, but it's important to have explicit dependencies if you really need to depend on a specific version!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I agree with your last line, that the expected behavior is probably to resolve the current project's wp-env copy from node_modules, even if it's in a different directory. (And while it would be unexpected to resolve from outside the project in a parent directory, that doesn't seem like a common use case)