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

Leverage PROJECT_CWD to set cwd in yarn 3 bin "proxies" #6199

Merged
merged 3 commits into from
Aug 11, 2022

Conversation

jtoar
Copy link
Contributor

@jtoar jtoar commented Aug 10, 2022

To upgrade RW projects to yarn 3, we had to make bin "proxy" files so that we could still access the bins of our dependencies. See #4444 for the full break down.

When you run a bin, yarn 3 sets the cwd to the workspace calling it. This revealed a hidden dependency in many of our CLI commands that expect the cwd to be the root, so we imported getPaths from @redwoodjs/cli (an export of the one in @redwoodjs/internal) in many of the bin proxy files to find the root and change to it.

This works and has worked for a long time, but turns out to be a not-so-great idea because it means pulling in a massive amount of @redwoodjs/internal for any CLI command, and possibly introducing some indeterminacy around the way env vars are loaded.

Instead of importing getPaths, this PR leverages the PROJECT_CWD env var provided by yarn 3. It's value is what you expect. See https://yarnpkg.com/advanced/lifecycle-scripts/#environment-variables.

Next step is to confirm whether or not this change fixes #5657.

Here's a visualization of the change using 0x to generate a flamegraph:

bin-proxies

The exact command for reproduction is

yarn dlx 0x -o ~/your-rw-project/node_modules/@redwoodjs/core/dist/bins/redwood.js --help

@nx-cloud
Copy link

nx-cloud bot commented Aug 10, 2022

☁️ Nx Cloud Report

CI is running/has finished running commands for commit c1478a7. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this branch


✅ Successfully ran 14 targets

Sent with 💌 from NxCloud.

@netlify
Copy link

netlify bot commented Aug 10, 2022

Deploy Preview for redwoodjs-docs canceled.

Name Link
🔨 Latest commit c1478a7
🔍 Latest deploy log https://app.netlify.com/sites/redwoodjs-docs/deploys/62f501faca835600080ebc59

@jtoar jtoar added topic/cli release:fix This PR is a fix labels Aug 10, 2022
@jtoar jtoar marked this pull request as ready for review August 10, 2022 14:04
@dac09
Copy link
Collaborator

dac09 commented Aug 10, 2022

Yarn master Dom at it again, nice one!

@@ -79,7 +79,7 @@ const loadDotEnvDefaultsMiddleware = () => {
config({
path: path.join(getPaths().base, '.env'),
defaults: path.join(getPaths().base, '.env.defaults'),
encoding: 'utf8',
Copy link
Contributor Author

@jtoar jtoar Aug 11, 2022

Choose a reason for hiding this comment

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

This is the default value

@@ -79,7 +79,7 @@ const loadDotEnvDefaultsMiddleware = () => {
config({
path: path.join(getPaths().base, '.env'),
defaults: path.join(getPaths().base, '.env.defaults'),
encoding: 'utf8',
multiline: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Partly fixes #5657

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you think we could add a test for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry I didn't see this comment; I'll revisit

Copy link
Collaborator

Choose a reason for hiding this comment

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

No worries! 👍👍👍

Comment on lines -32 to -35
// Ensure we run all commands from the base of the RW project
// even if you invoke from ./web or ./api
const rwProjectRoot = requireFromCli('./dist/lib/index.js').getPaths().base
process.chdir(rwProjectRoot)
Copy link
Contributor Author

@jtoar jtoar Aug 11, 2022

Choose a reason for hiding this comment

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

This one (core) is already at the root, so no need to change to it

@jtoar
Copy link
Contributor Author

jtoar commented Aug 11, 2022

I've tested this locally and it works so far, so I'm going to merge once checks past and test again in canary.

@jtoar jtoar enabled auto-merge (squash) August 11, 2022 13:21
@jtoar jtoar merged commit 0e087ad into main Aug 11, 2022
@jtoar jtoar deleted the ds-try-fixing-prisma-env-loading branch August 11, 2022 13:48
@redwoodjs-bot redwoodjs-bot bot added this to the next-release milestone Aug 11, 2022
dac09 added a commit to dac09/redwood that referenced this pull request Aug 15, 2022
…9/redwood into fix/cell-prerender-graphql-fallback

* 'fix/cell-prerender-graphql-fallback' of github.com:dac09/redwood:
  Stylistic suggestions
  Leverage PROJECT_CWD to set cwd in yarn 3 bin "proxies" (redwoodjs#6199)
  chore(cli): Add additional tests for sdl generator (redwoodjs#6200)
@jtoar jtoar modified the milestones: next-release, v3.0.0 Sep 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:fix This PR is a fix topic/cli
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Prisma loads .env, processes differently from RWJS
2 participants