-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
58e44ea
patch resolve package and use corret path for Yarn PnP module resolution
lmiller1990 a3e438e
add test
lmiller1990 c02d49d
fix logic
lmiller1990 18c6d0e
changelog
lmiller1990 35079a6
log
lmiller1990 8194140
Add link to pnp docs
mike-plummer 21762b2
Merge branch 'develop' into issue-25960
mike-plummer 01e94a7
Merge remote-tracking branch 'origin/develop' into issue-25960
lmiller1990 775314a
recursively search upwards for pnp.cjs
lmiller1990 47b41bd
use require.resolve no matter what
lmiller1990 948de1b
Merge branch 'develop' into issue-25960
lmiller1990 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import findUp from 'find-up' | ||
import path from 'path' | ||
import fs from 'fs-extra' | ||
import Debug from 'debug' | ||
const debug = Debug('cypress:scaffold-config:searchUtils') | ||
|
||
const ROOT_PATHS = [ | ||
'.git', | ||
|
||
// https://pnpm.io/workspaces | ||
'pnpm-workspace.yaml', | ||
|
||
// https://rushjs.io/pages/advanced/config_files/ | ||
'rush.json', | ||
|
||
// https://nx.dev/deprecated/workspace-json#workspace.json | ||
// https://nx.dev/reference/nx-json#nx.json | ||
'workspace.json', | ||
'nx.json', | ||
|
||
// https://lerna.js.org/docs/api-reference/configuration | ||
'lerna.json', | ||
] | ||
|
||
async function hasWorkspacePackageJson (directory: string) { | ||
try { | ||
const pkg = await fs.readJson(path.join(directory, 'package.json')) | ||
|
||
debug('package file for %s: %o', directory, pkg) | ||
|
||
return !!pkg.workspaces | ||
} catch (e) { | ||
debug('error reading package.json in %s. this is not the repository root', directory) | ||
|
||
return false | ||
} | ||
} | ||
|
||
export async function isRepositoryRoot (directory: string) { | ||
if (ROOT_PATHS.some((rootPath) => fs.existsSync(path.join(directory, rootPath)))) { | ||
return true | ||
} | ||
|
||
return hasWorkspacePackageJson(directory) | ||
} | ||
|
||
/** | ||
* Recursing search upwards from projectPath until the repository root looking for .pnp.cjs. | ||
* If `.pnp.cjs` is found, return it | ||
*/ | ||
export async function tryToFindPnpFile (projectPath: string): Promise<string | undefined> { | ||
return findUp(async (directory: string) => { | ||
const isCurrentRepositoryRoot = await isRepositoryRoot(directory) | ||
|
||
const file = path.join(directory, '.pnp.cjs') | ||
const hasPnpCjs = await fs.pathExists(file) | ||
|
||
if (hasPnpCjs) { | ||
return file | ||
} | ||
|
||
if (isCurrentRepositoryRoot) { | ||
debug('stopping search at %s because it is believed to be the repository root', directory) | ||
|
||
return findUp.stop | ||
} | ||
|
||
// Return undefined to keep searching | ||
return undefined | ||
}, { cwd: projectPath }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import fs from 'fs-extra' | ||
import path from 'path' | ||
import { expect } from 'chai' | ||
import os from 'os' | ||
import { isRepositoryRoot, tryToFindPnpFile } from '../../src/searchUtils' | ||
import dedent from 'dedent' | ||
|
||
const TEMP_DIR = path.join(os.tmpdir(), 'is-repository-root-tmp') | ||
|
||
beforeEach(async () => { | ||
await fs.mkdir(TEMP_DIR) | ||
}) | ||
|
||
afterEach(async () => { | ||
await fs.rm(TEMP_DIR, { recursive: true }) | ||
}) | ||
|
||
describe('isRepositoryRoot', () => { | ||
it('returns false if there is nothing in the directory', async () => { | ||
const isCurrentRepositoryRoot = await isRepositoryRoot(TEMP_DIR) | ||
|
||
expect(isCurrentRepositoryRoot).to.be.false | ||
}) | ||
|
||
it('returns true if there is a Git directory', async () => { | ||
await fs.mkdir(path.join(TEMP_DIR, '.git')) | ||
|
||
const isCurrentRepositoryRoot = await isRepositoryRoot(TEMP_DIR) | ||
|
||
expect(isCurrentRepositoryRoot).to.be.true | ||
}) | ||
|
||
it('returns false if there is a package.json without workspaces field', async () => { | ||
await fs.writeFile(path.join(TEMP_DIR, 'package.json'), `{ | ||
"name": "@packages/foo", | ||
"private": true, | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT" | ||
} | ||
`) | ||
|
||
const isCurrentRepositoryRoot = await isRepositoryRoot(TEMP_DIR) | ||
|
||
expect(isCurrentRepositoryRoot).to.be.false | ||
}) | ||
|
||
it('returns true if there is a package.json with workspaces field', async () => { | ||
await fs.writeFile(path.join(TEMP_DIR, 'package.json'), `{ | ||
"name": "monorepo-repo", | ||
"private": true, | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"workspaces": [ | ||
"packages/*" | ||
] | ||
} | ||
`) | ||
|
||
const isCurrentRepositoryRoot = await isRepositoryRoot(TEMP_DIR) | ||
|
||
expect(isCurrentRepositoryRoot).to.be.true | ||
}) | ||
}) | ||
|
||
describe('tryToFindPnpFile', () => { | ||
it('finds pnp.cjs at repo root', async () => { | ||
const projectPath = path.join(TEMP_DIR, 'packages', 'tests') | ||
const pnpcjs = path.join(TEMP_DIR, '.pnp.cjs') | ||
|
||
await Promise.all([ | ||
fs.ensureFile(path.join(projectPath, 'package.json')), | ||
fs.writeFile(pnpcjs, '/* pnp api */'), | ||
fs.writeFile(path.join(TEMP_DIR, 'package.json'), dedent` | ||
{ | ||
"workspaces": [ | ||
"packages/*" | ||
] | ||
} | ||
`), | ||
]) | ||
|
||
const pnpPath = await tryToFindPnpFile(projectPath) | ||
|
||
expect(pnpPath).to.eq(pnpcjs) | ||
}) | ||
|
||
it('does not find pnp.cjs at repo root', async () => { | ||
const projectPath = path.join(TEMP_DIR, 'packages', 'tests') | ||
|
||
await fs.ensureFile(path.join(projectPath, 'package.json')) | ||
await fs.writeFile(path.join(TEMP_DIR, 'package.json'), dedent` | ||
{ | ||
"workspaces": [ | ||
"packages/*" | ||
] | ||
} | ||
`) | ||
|
||
const pnpPath = await tryToFindPnpFile(projectPath) | ||
|
||
expect(pnpPath).to.eq(undefined) | ||
}) | ||
}) |
Oops, something went wrong.
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.
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.
omg, never would have figured this one out. Very nice!