-
Notifications
You must be signed in to change notification settings - Fork 702
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
C3: Fix usage with yarn #4311
C3: Fix usage with yarn #4311
Conversation
🦋 Changeset detectedLatest commit: ece618d The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
A wrangler prerelease is available for testing. You can install this latest build in your project with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/6789015440/npm-package-wrangler-4311 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/prs/6789015440/npm-package-wrangler-4311 Or you can use npx https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/6789015440/npm-package-wrangler-4311 dev path/to/script.js Additional artifacts:npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/6789015440/npm-package-miniflare-4311 npm install https://prerelease-registry.devprod.cloudflare.dev/workers-sdk/runs/6789015440/npm-package-cloudflare-pages-shared-4311 Note that these links will no longer work once the GitHub Actions artifact expires.
| Please ensure constraints are pinned, and |
Codecov Report
@@ Coverage Diff @@
## main #4311 +/- ##
==========================================
+ Coverage 75.28% 75.31% +0.03%
==========================================
Files 223 223
Lines 12357 12357
Branches 3198 3198
==========================================
+ Hits 9303 9307 +4
+ Misses 3054 3050 -4 |
// So to retain the ability to lock verisons we run it with `npx` and spoof | ||
// the user agent so scaffolding tools treat the invocation like yarn | ||
let cmd = `${npm === "yarn" ? "npx" : dlx} ${cli} ${argString}`; | ||
const env = npm === "yarn" ? { npm_config_user_agent: "yarn" } : {}; |
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.
why is this needed?
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.
So that the scaffolding tools think that they were invoked via yarn create my-framework
. This means that they'll use yarn
when doing any npm installs, which in turn means that we don't need to resetPackageManager
.
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.
ah ok, so here is basically when we run npx
instead of yarn and we add this user agent to trick the underlying CLIs to think that we're actually using yarn?
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.
exactly!
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 assume the code comments were added after this discussion?
Thanks for handling the issue! |
@@ -254,18 +263,34 @@ export const npmInstall = async () => { | |||
}); | |||
}; | |||
|
|||
const needsReset = (ctx: PagesGeneratorContext) => { |
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'm not sure needsReset is named adequately in a file called command.ts
If this is the prerequisite check before calling resetPackageManager, maybe name is packageManagerNeedsReset
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.
Since it is local to this file and only used in one place I don't think this name is too problematic.
.changeset/dirty-bobcats-eat.md
Outdated
"create-cloudflare": patch | ||
--- | ||
|
||
Fixes c3 usage with yarn |
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.
Can we have a bit more explanation here? This will appear in the changelog for the package. We should write from the point of view of someone trying to find out what has changed in a release.
// Resets the package manager context for a project by clearing out existing dependencies | ||
// and lock files then re-installing. | ||
export const resetPackageManager = async (ctx: PagesGeneratorContext) => { | ||
const { npm } = detectPackageManager(); | ||
|
||
// Only do this when using pnpm or yarn | ||
if (npm === "npm") return; | ||
if (!needsReset(ctx)) return; |
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.
Surely we have a lint rule that stops if blocks not in curlies?
I know these are only single line ifs, and I'm being a bit pedantic, but this is a very common cause of errors.
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 agree it's does feel a bit unsafe (although I understand the convenience)
I don't think we have the eslint rule set and actually we do this sort of thing in a bunch of different places (C3 not being the biggest offender):
I'm totally on board adding the rule in a followup PR if devprod's on board with it 🙂
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.
A few minor nits
1266aca
to
0f8b002
Compare
0f8b002
to
ece618d
Compare
Fixes #4230
What this PR solves / how to test:
This fixes a regression that was causing c3 to fail with web frameworks when using yarn.
Being able to version the framework creators we use is important for c3, but unfortunately
yarn
can'tyarn create some-framework@some-particular-version
. This change modifies therunFrameworkGenerator
helper to usenpx
and spoof the npm user agent so framework creators treat it like ayarn create
invocation.It also patches the package manager reset logic to make it only run when truly needed (i.e. when there's a mismatch between installed node modules and the type of lockfile used by the desired package manager).
This also adds e2e coverage for yarn to hopefully prevent future regressions.
Author has addressed the following:
Note for PR author:
We want to celebrate and highlight awesome PR review! If you think this PR received a particularly high-caliber review, please assign it the label
highlight pr review
so future reviewers can take inspiration and learn from it.