Skip to content

Commit d1c51b0

Browse files
authored
Merge pull request #5256 from Shopify/ah.fix-shopify-function-version-check
Allow multiple compatible `@shopify/shopify_function` major versions
2 parents d4601ad + 6484553 commit d1c51b0

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

packages/app/src/cli/services/build/extension.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,13 @@ export async function buildFunctionExtension(
188188
}
189189
// eslint-disable-next-line @typescript-eslint/no-explicit-any
190190
} catch (error: any) {
191-
// We capture and rethrow as AbortError to avoid random user-code errors being reported as CLI bugs.
192-
// At the same time, we need to keep the ESBuild details for the logs. (the `errors` array)
191+
// To avoid random user-code errors being reported as CLI bugs, we capture and rethrow them as AbortError.
192+
// In this case, we need to keep the ESBuild details for the logs. (the `errors` array).
193+
// If the error is already an AbortError, we can just rethrow it.
194+
if (error instanceof AbortError) {
195+
throw error
196+
}
197+
193198
const errorMessage = (error as Error).message ?? 'Unknown error occurred'
194199
// eslint-disable-next-line @typescript-eslint/no-explicit-any
195200
const newError: any = new AbortError('Failed to build function.', errorMessage)

packages/app/src/cli/services/function/build.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ describe('bundleExtension', () => {
142142
test('errors if shopify library is not on a compatible version', async () => {
143143
await inTemporaryDirectory(async (tmpDir) => {
144144
// Given
145-
const incompatibleVersion = '0.0.1'
145+
const incompatibleVersion = '999.0.0'
146146
const ourFunction = await testFunctionExtension({dir: tmpDir})
147147
ourFunction.entrySourceFilePath = joinPath(tmpDir, 'src/index.ts')
148148
await installShopifyLibrary(tmpDir)

packages/app/src/cli/services/function/build.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ import {runWithTimer} from '@shopify/cli-kit/node/metadata'
1717
import {AbortError} from '@shopify/cli-kit/node/error'
1818
import {Writable} from 'stream'
1919

20-
export const SHOPIFY_FUNCTION_NPM_PACKAGE_MAJOR_VERSION = '1'
20+
const ALLOWED_FUNCTION_NPM_PACKAGE_MAJOR_VERSIONS = ['0', '1']
21+
export const PREFERRED_FUNCTION_NPM_PACKAGE_MAJOR_VERSION = '1'
2122

2223
class InvalidShopifyFunctionPackageError extends AbortError {
2324
constructor(message: string) {
@@ -28,7 +29,7 @@ class InvalidShopifyFunctionPackageError extends AbortError {
2829
)} library installed.`,
2930
[
3031
outputContent`Add ${outputToken.green(
31-
`"@shopify/shopify_function": "~${SHOPIFY_FUNCTION_NPM_PACKAGE_MAJOR_VERSION}.0.0"`,
32+
`"@shopify/shopify_function": "~${PREFERRED_FUNCTION_NPM_PACKAGE_MAJOR_VERSION}.0.0"`,
3233
)} to the dependencies section of the package.json file in your function's directory, if not already present.`
3334
.value,
3435
`Run your package manager's install command to update dependencies.`,
@@ -160,7 +161,7 @@ async function validateShopifyFunctionPackageVersion(fun: ExtensionInstance<Func
160161
const packageJson = JSON.parse(await readFile(packageJsonPath))
161162
const majorVersion = packageJson.version.split('.')[0]
162163

163-
if (majorVersion !== SHOPIFY_FUNCTION_NPM_PACKAGE_MAJOR_VERSION) {
164+
if (!ALLOWED_FUNCTION_NPM_PACKAGE_MAJOR_VERSIONS.includes(majorVersion)) {
164165
throw new InvalidShopifyFunctionPackageError(
165166
'The installed version of the Shopify Functions JavaScript library is not compatible with this version of Shopify CLI.',
166167
)

packages/app/src/cli/services/generate/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {configurationFileNames, versions} from '../../constants.js'
22
import {AppInterface} from '../../models/app/app.js'
3-
import {buildGraphqlTypes, SHOPIFY_FUNCTION_NPM_PACKAGE_MAJOR_VERSION} from '../function/build.js'
3+
import {buildGraphqlTypes, PREFERRED_FUNCTION_NPM_PACKAGE_MAJOR_VERSION} from '../function/build.js'
44
import {GenerateExtensionContentOutput} from '../../prompts/generate/extension.js'
55
import {ExtensionFlavor, ExtensionTemplate} from '../../models/app/template.js'
66
import {ensureDownloadedExtensionFlavorExists, ensureExtensionDirectoryExists} from '../extensions/common.js'
@@ -301,7 +301,7 @@ export function getFunctionRuntimeDependencies(templateLanguage: string): Depend
301301
if (templateLanguage === 'javascript') {
302302
dependencies.push({
303303
name: '@shopify/shopify_function',
304-
version: `~${SHOPIFY_FUNCTION_NPM_PACKAGE_MAJOR_VERSION}.0.0`,
304+
version: `~${PREFERRED_FUNCTION_NPM_PACKAGE_MAJOR_VERSION}.0.0`,
305305
})
306306
}
307307
return dependencies

0 commit comments

Comments
 (0)