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

Merged
merged 3 commits into from
Jan 13, 2025

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.

Comment on lines +26 to +34
outputContent`Make sure you have a compatible version of the ${outputToken.yellow(
'@shopify/shopify_function',
)} library installed.`,
[
outputContent`Add ${outputToken.green(
`"@shopify/shopify_function": "~${SHOPIFY_FUNCTION_NPM_PACKAGE_MAJOR_VERSION}.0.0"`,
)} to the dependencies section of the package.json file in your function's directory, if not already present.`
.value,
`Run your package manager's install command to update dependencies.`,
Copy link
Contributor

Choose a reason for hiding this comment

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

Should I be able to see these messages in the console when I encounter this error?

I do see the passed in message: Could not find the Shopify Functions JavaScript library.

I know in this PR you refactored things into an AbortError subclass so this is just a mov, but still got me thinking... where does all this outputContent end up? Even running the build command with --verbose doesn't show it.

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 noticed that too when testing. It appears that the error is caught and rethrown here. I didn't want to tackle that as part of this PR though, since it's more related to the general error handling.

Copy link
Contributor

Choose a reason for hiding this comment

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

Are you going to fix that in a follow-up PR? there's no reason to have a custom Error if doesn't work as expected :)

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 can take a look. I'm not actually sure why we rethrow the error like this 😅 It looks like you added it about a month ago. Do you have context on it?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I added that try/catch because we were leaking some errors, and we want to properly Abort if there is an issue in any step of the build process.

But the rethrow keeps the message, why isn't it preserved in this case?

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.

@andrewhassan andrewhassan added this pull request to the merge queue Jan 13, 2025
Merged via the queue into main with commit 3b4a49f Jan 13, 2025
27 checks passed
@andrewhassan andrewhassan deleted the ah.validate-shopify-function-package-version branch January 13, 2025 13:59
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