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

Validate @shopify/shopify_function version on build #5153

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

andrewhassan
Copy link
Contributor

@andrewhassan andrewhassan commented Jan 3, 2025

WHY are these changes introduced?

Closes https://github.com/Shopify/shopify-functions/issues/519

The @shopify/shopify_function NPM package for JavaScript Functions is implicitly tied to the version of Javy used by the CLI. However, we don't validate that the NPM package is compatible today, which means it's possible to use an older version of the NPM package that isn't compatible with a newer version of Javy.

WHAT is this pull request doing?

To guarantee compatibility, this PR validates that the installed @shopify/shopify_function package's major version is what the CLI expects.

This PR also ensures that we're using the latest patch version in our recommendations and when generating a function.

How to test your changes?

  • Run app function build for a JS function using shopify_function version 1.x.y.
  • Run app function build for a JS function using shopify_function version 0.x.y.
  • Run app dev for a JS function (both scenarios above)
  • Run app function build for a Rust function (should not be affected by these changes)

Measuring impact

How do we know this change was effective? Please choose one:

  • n/a - this doesn't need measurement, e.g. a linting rule or a bug-fix
  • Existing analytics will cater for this addition
  • PR includes analytics changes to measure impact

Checklist

  • I've considered possible cross-platform impacts (Mac, Linux, Windows)
  • I've considered possible documentation changes

@andrewhassan andrewhassan force-pushed the ah.validate-shopify-function-package-version branch from 10e6428 to e121a59 Compare January 3, 2025 21:46
@andrewhassan andrewhassan changed the title Validate shopify_function version on build Validate @shopify/shopify_function version on build Jan 3, 2025
@andrewhassan andrewhassan force-pushed the ah.validate-shopify-function-package-version branch 3 times, most recently from c980c84 to e57adca Compare January 3, 2025 22:04
@andrewhassan andrewhassan marked this pull request as ready for review January 3, 2025 22:11
@andrewhassan andrewhassan requested a review from a team as a code owner January 3, 2025 22:11

This comment has been minimized.

@andrewhassan andrewhassan requested a review from a team as a code owner January 6, 2025 15:55
@andrewhassan andrewhassan force-pushed the ah.validate-shopify-function-package-version branch from 7cc70c4 to f4c7577 Compare January 6, 2025 16:24
Copy link
Contributor

github-actions bot commented Jan 6, 2025

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements
75.33% (+0.02% 🔼)
8852/11751
🟡 Branches
70.58% (+0.02% 🔼)
4294/6084
🟡 Functions
75.23% (+0.02% 🔼)
2317/3080
🟡 Lines
75.87% (+0.03% 🔼)
8369/11030
Show files with reduced coverage 🔻
St.
File Statements Branches Functions Lines
🟢
... / app-event-watcher.ts
93.83% (-1.23% 🔻)
86.49% (-2.7% 🔻)
90.48% 98.61%

Test suite run success

1995 tests passing in 902 suites.

Report generated by 🧪jest coverage report action from f4c7577

@andrewhassan andrewhassan requested review from a team, lopert and saga-dasgupta and removed request for a team January 6, 2025 21:41
@@ -139,11 +147,32 @@
return entryPoint
}

async function validateShopifyFunctionPackageVersion(fun: ExtensionInstance<FunctionConfigType>) {
const packageJsonPath = await findPathUp('node_modules/@shopify/shopify_function/package.json', {
Copy link
Contributor

Choose a reason for hiding this comment

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

have you tried this path with different package managers? I know that pnpm sometimes uses a different folder structure for modules.

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 have not, but I was following the implementation here and assumed they validated this.

Copy link
Contributor

@lopert lopert left a comment

Choose a reason for hiding this comment

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

LGTM, spent a bit of time messing with the various package files and node_modules folders to 🎩 this.

})

if (!packageJsonPath) {
throw new InvalidShopifyFunctionPackageError('Could not find the Shopify Functions JavaScript library.')
Copy link
Contributor

Choose a reason for hiding this comment

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

I could not get this to trigger in a way that made sense to me.

If I removed the dependency, deleted the node_modules folder, ran npm install in my extension, then tried the build command, I'd get an unrelated (?) error.

Command failed with exit code 1: npm exec -- graphql-code-generator --config package.json

But if I went in and manually removed just the node_modules/@shopify/shopify_function/package.json file, I got this message 👍

What kind of config / steps would lead to a user actually getting into this position?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In practice, I think this could happen if the user removed the @shopify/shopify_function dependency so that it wasn't present at all in their node_modules folder.

Copy link
Contributor

@saga-dasgupta saga-dasgupta left a comment

Choose a reason for hiding this comment

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

This change makes sense to me and I was able to verify the expected error message is displayed for function build and app dev for a function with shopify-functions version 0.1.0.

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.

5 participants