-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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: correctly resolve dependencies for CT onboarding when using Yarn Plug n Play #26452
Changes from 2 commits
58e44ea
a3e438e
c02d49d
18c6d0e
35079a6
8194140
21762b2
01e94a7
775314a
47b41bd
948de1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import fs from 'fs-extra' | ||
import path from 'path' | ||
import * as dependencies from './dependencies' | ||
import componentIndexHtmlGenerator from './component-index-template' | ||
import debugLib from 'debug' | ||
import semver from 'semver' | ||
import { isThirdPartyDefinition } from './ct-detect-third-party' | ||
import resolvePackagePath from 'resolve-package-path' | ||
|
||
const debug = debugLib('cypress:scaffold-config:frameworks') | ||
|
||
|
@@ -14,10 +14,42 @@ export type WizardBundler = typeof dependencies.WIZARD_BUNDLERS[number] | |
|
||
export type CodeGenFramework = Cypress.ResolvedComponentFrameworkDefinition['codeGenFramework'] | ||
|
||
let yarnPnpRegistrationPath: string | undefined | ||
|
||
async function readPackageJson (packageFilePath: string): Promise<PkgJson> { | ||
if (yarnPnpRegistrationPath) { | ||
// using Yarn PnP. You cannot `fs.readJson`, since the module is zipped. | ||
// use require.resolve - The PnP runtime (.pnp.cjs) automatically patches Node's | ||
// fs module to add support for accessing files inside Zip archives. | ||
// @see https://yarnpkg.com/features/pnp#packages-are-stored-inside-zip-archives-how-can-i-access-their-files | ||
return require(require.resolve(packageFilePath)) | ||
} | ||
|
||
return await fs.readJson(packageFilePath) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe I'm missing an edge case, but couldn't we just use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there was an edge case where this doesn't work, let me double check. |
||
} | ||
|
||
export async function isDependencyInstalled (dependency: Cypress.CypressComponentDependency, projectPath: string): Promise<Cypress.DependencyToInstall> { | ||
try { | ||
debug('detecting %s in %s', dependency.package, projectPath) | ||
|
||
// we only need to register this once, when the project check dependencies for the first time. | ||
if (yarnPnpRegistrationPath !== projectPath) { | ||
try { | ||
const pnpapi = require(path.join(projectPath, '.pnp.cjs')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's a bit of a hack (Yarn 3, I mean, not what I'm doing here - this is how they recommend you load the It wouldn't matter if we executed this block it every time - it would just be a bit of pointless overhead. It would be next to negligible, since Node.js caches There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about a scenario where a Cypress project is nested into a subdirectory of a yarn project? Do we need to traverse upwards to find the first Theres a method on the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, forgot the nested edge case, I will verify what happens and address using this if required. Yarn has a ton of APIs for this, as you pointed out, we can probably use one. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Painful, but for this API to exist you need to be in a PnP context... which you get by requiring the file in the first place 🤦 I think we need to manually traverse up, lucky we've already got some code that does that, |
||
|
||
pnpapi.setup() | ||
yarnPnpRegistrationPath = projectPath | ||
} catch (e) { | ||
// not using Yarn PnP | ||
yarnPnpRegistrationPath = undefined | ||
} | ||
} | ||
|
||
// NOTE: this *must* be required **after** the call to `pnpapi.setup()` | ||
// or the pnpapi module that is added at runtime by Yarn PnP will not be correctly used | ||
// for module resolution. | ||
const resolvePackagePath = require('resolve-package-path') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. omg, never would have figured this one out. Very nice! |
||
|
||
const packageFilePath = resolvePackagePath(dependency.package, projectPath) | ||
|
||
if (!packageFilePath) { | ||
|
@@ -30,7 +62,7 @@ export async function isDependencyInstalled (dependency: Cypress.CypressComponen | |
} | ||
} | ||
|
||
const pkg = await fs.readJson(packageFilePath) as PkgJson | ||
const pkg = await readPackageJson(packageFilePath) | ||
|
||
debug('found package.json %o', pkg) | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
diff --git a/node_modules/resolve-package-path/lib/index.js b/node_modules/resolve-package-path/lib/index.js | ||
index a1463f7..a3b5d3c 100644 | ||
--- a/node_modules/resolve-package-path/lib/index.js | ||
+++ b/node_modules/resolve-package-path/lib/index.js | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sent the author a patch stefanpenner/resolve-package-path#62 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Problem is the module is installed in the same directory as the project, which in our case it isn't - the module is in the binary. |
||
@@ -89,6 +89,9 @@ function resolvePackagePath(target, baseDir, _cache) { | ||
// the custom `pnp` code here can be removed when yarn 1.13 is the | ||
// current release. This is due to Yarn 1.13 and resolve interoperating | ||
// together seamlessly. | ||
+ if (!pnp) { | ||
+ pnp = require(require.resolve('pnpapi', { paths: [baseDir] })) | ||
+ } | ||
pkgPath = pnp | ||
? pnp.resolveToUnqualified(target + '/package.json', baseDir) | ||
: (0, resolve_package_path_1.default)(cache, target, baseDir); |
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.
Might be worth reading https://yarnpkg.com/features/pnp for more context.