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: correctly resolve dependencies for CT onboarding when using Yarn Plug n Play #26452

Merged
merged 11 commits into from
Apr 11, 2023
12 changes: 12 additions & 0 deletions packages/launchpad/cypress/e2e/project-setup.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,18 @@ describe('Launchpad: Setup Project', () => {
cy.findByDisplayValue('pnpm install -D react-scripts react-dom react')
})

it('works with Yarn 3 Plug n Play', () => {
scaffoldAndOpenProject('yarn-v3.1.1-pnp')

cy.visitLaunchpad()

cy.get('[data-cy-testingtype="component"]').click()
cy.get('button').should('be.visible').contains('Vue.js 3(detected)')
cy.get('button').should('be.visible').contains('Vite(detected)')
cy.findByText('Next step').click()
cy.findByTestId('alert').contains(`You've successfully installed all required dependencies.`)
})

it('makes the right command for npm', () => {
scaffoldAndOpenProject('pristine-npm')

Expand Down
36 changes: 34 additions & 2 deletions packages/scaffold-config/src/frameworks.ts
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')

Expand All @@ -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
Copy link
Contributor Author

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.

return require(require.resolve(packageFilePath))
}

return await fs.readJson(packageFilePath)
Copy link
Contributor

Choose a reason for hiding this comment

The 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 require(require.resolve(packageFilePath)) logic in both cases? PnP handles if the module is zipped, and if we aren't in PnP it will be a flat file that should be normally require-able

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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'))
Copy link
Contributor Author

@lmiller1990 lmiller1990 Apr 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.pnp.cjs is regenerated by Yarn every time you install a module. We only want to require it once, since the trick Yarn PnP uses is to dynamically monkeypatch module and do some other weird hacks to facilitate the various features of PnP. That is what setup() does.

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 .pnp.cjs if you are doing it manually). Yarn 3 does run really fast, though, because of this... although Yarn 1 -> Yarn 2.x + PnP is a heck of a lift for a lot of code bases and projects, which is why many people are stuck on v1 (including FB, the (original) authors of Yarn).

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 require calls anyway, but this is cleaner. We do need to execute it once for each new project (think global mode - you can switch projects, each having a different .pnp.cjs).

Copy link
Contributor

Choose a reason for hiding this comment

The 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 .pnp.cjs we encounter?

Theres a method on the module module that seems to do this for us in a PnP context (find .pnp.cjs, load, and return pnpapi (which we can ignore))

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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, isRepositoryRoot.


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')
Copy link
Contributor

Choose a reason for hiding this comment

The 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) {
Expand All @@ -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)

Expand Down
14 changes: 14 additions & 0 deletions patches/resolve-package-path+4.0.3.patch
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sent the author a patch stefanpenner/resolve-package-path#62

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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);
Loading