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

package-utils cannot resolve external commands #1815

Closed
ylemkimon opened this issue Sep 21, 2020 · 1 comment · Fixed by #1945
Closed

package-utils cannot resolve external commands #1815

ylemkimon opened this issue Sep 21, 2020 · 1 comment · Fixed by #1945

Comments

@ylemkimon
Copy link
Contributor

ylemkimon commented Sep 21, 2020

Describe the bug
packageExists in @webpack-cli/package-utils is used to resolve external commands:

export function packageExists(packageName: string): boolean {
try {
require(packageName);
return true;
} catch (err) {
return false;
}
}

However, since external command packages, such as @webpack-cli/info, aren't declared as a dependency, @webpack-cli/package-utils shouldn't be able to require them.

This is especially an issue for Yarn 2 PnP as it requires all dependencies to be explicitly declared to link them (Yarn rulebook).

If Yarn cannot resolve the dependency, it falls back to the top level if pnpFallbackMode is depenencies-only (default) or all. So it's possible to resolve external commands, but not default commands included in the webpack-cli.

For instance, if the dependency tree of a project is:

project
├─D> @webpack-cli/xxx
└─D> webpack-cli
     ├─D> @webpack-cli/package-utils
     └─D> @webpack-cli/info

webpack xxx works but webpack info doesn't work because @webpack-cli/package-utils cannot resolve @webpack-cli/info. webpack xxx doesn't work if pnpFallbackMode: none.

To summarize:

  1. @webpack-cli/package-utils cannot resolve default commands (fix: declare default commands as optional peer dependencies #1816)
  2. @webpack-cli/package-utils cannot resolve external commands with pnpFallbackMode: none

What is the current behavior?

$ webpack info
Error: @webpack-cli/package-utils tried to access @webpack-cli/info, but it isn't declared in its dependencies; this makes the require call ambiguous and unsound.

Required package: @webpack-cli/info (via "@webpack-cli/info")
Required by: @webpack-cli/package-utils@npm:1.0.1-alpha.4 (via @webpack-cli-package-utils/node_modules/@webpack-cli/package-utils/lib/)

Require stack:
- @webpack-cli-package-utils/node_modules/@webpack-cli/package-utils/lib/packageUtils.js
- @webpack-cli-package-utils/node_modules/@webpack-cli/package-utils/lib/index.js
- webpack-cli/lib/commands/ExternalCommand.js
- webpack-cli/lib/utils/arg-parser.js
- webpack-cli/lib/webpack-cli.js
- webpack-cli/lib/bootstrap.js
- webpack-cli/bin/cli.js
- webpack/bin/webpack.js
    at internalTools_makeError (.pnp.js:21227:34)
    at resolveToUnqualified (.pnp.js:22185:23)
    at resolveRequest (.pnp.js:22277:29)
    at Object.resolveRequest (.pnp.js:22343:26)
    at Function.external_module_.Module._resolveFilename (.pnp.js:21460:34)
    at Function.external_module_.Module._load (.pnp.js:21325:48)
    at Module.require (internal/modules/cjs/loader.js:1025:19)
    at require (v8-compile-cache/v8-compile-cache.js:161:20)
    at packageExists (@webpack-cli/package-utils/lib/packageUtils.js:58:9)
    at Function.run (webpack-cli/lib/commands/ExternalCommand.js:27:22)
[webpack-cli] The command moved into a separate package: @webpack-cli/info

? Would you like to install @webpack-cli/info? (That will run yarn add -D @webpack-cli/info)

To Reproduce

yarn set version berry
yarn init
echo -e 'packageExtensions:\n  "@webpack-cli/package-utils@*":\n    dependencies:\n      "cross-spawn": "^7.0.0"' >> .yarnrc.yml # Workaround for issue already fixed in master
yarn add -D webpack@^5.0.0-rc.0 webpack-cli@^4.0.0-beta.9
yarn webpack info

Expected behavior
webpack info and webpack xxx (xxx is an external command) to run

@ylemkimon ylemkimon changed the title @webpack-cli/package-utils cannot resolve default commands @webpack-cli/package-utils cannot resolve external commands Sep 21, 2020
@ylemkimon ylemkimon changed the title @webpack-cli/package-utils cannot resolve external commands package-utils cannot resolve external commands Sep 21, 2020
@ylemkimon
Copy link
Contributor Author

Fixed via #1822.

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 a pull request may close this issue.

1 participant