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

C3: Fix usage with yarn #4311

Merged
merged 5 commits into from
Nov 7, 2023
Merged

C3: Fix usage with yarn #4311

merged 5 commits into from
Nov 7, 2023

Conversation

jculvey
Copy link
Contributor

@jculvey jculvey commented Oct 31, 2023

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't yarn create some-framework@some-particular-version. This change modifies the runFrameworkGenerator helper to use npx and spoof the npm user agent so framework creators treat it like a yarn 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:

  • Tests
    • Included
    • Not necessary because:
  • Changeset (Changeset guidelines)
    • Included
    • Not necessary because:
  • Associated docs
    • Issue(s)/PR(s):
    • Not necessary because:

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.

@jculvey jculvey requested review from a team as code owners October 31, 2023 04:07
@changeset-bot
Copy link

changeset-bot bot commented Oct 31, 2023

🦋 Changeset detected

Latest commit: ece618d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
create-cloudflare Patch

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

@jculvey jculvey changed the title C3 fix yarn create C3: Fix usage with yarn Oct 31, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Oct 31, 2023

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 with this latest build directly:

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.


wrangler@3.15.0 includes the following runtime dependencies:

Package Constraint Resolved
miniflare workspace:* 3.20231025.1
workerd 1.20231030.0 1.20231030.0
workerd --version 1.20231030.0 2023-10-30

|

Please ensure constraints are pinned, and miniflare/workerd minor versions match.

@codecov
Copy link

codecov bot commented Oct 31, 2023

Codecov Report

Merging #4311 (ece618d) into main (6e4ad7e) will increase coverage by 0.03%.
The diff coverage is n/a.

Impacted file tree graph

@@            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     

see 3 files with indirect coverage changes

packages/create-cloudflare/src/helpers/command.ts Outdated Show resolved Hide resolved
// 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" } : {};
Copy link
Member

Choose a reason for hiding this comment

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

why is this needed?

Copy link
Contributor Author

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.

Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

exactly!

Copy link
Contributor

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?

@yusukebe
Copy link
Contributor

Thanks for handling the issue!

@@ -254,18 +263,34 @@ export const npmInstall = async () => {
});
};

const needsReset = (ctx: PagesGeneratorContext) => {
Copy link
Contributor

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

Copy link
Contributor

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.

"create-cloudflare": patch
---

Fixes c3 usage with yarn
Copy link
Contributor

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;
Copy link
Contributor

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.

Copy link
Member

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):
Screenshot 2023-11-02 at 12 49 19

I'm totally on board adding the rule in a followup PR if devprod's on board with it 🙂

Copy link
Contributor

@petebacondarwin petebacondarwin left a 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

@jculvey jculvey merged commit 35be459 into main Nov 7, 2023
24 of 27 checks passed
@jculvey jculvey deleted the c3-fix-yarn-create branch November 7, 2023 22:29
@workers-devprod workers-devprod mentioned this pull request Nov 7, 2023
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 this pull request may close these issues.

🐛 BUG: C3 fails with yarn
5 participants