Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix: spree upgrade installs an out-of-sync bundle instead of failing (#14948)

Damian Legawiec317c01af7775
diff --git a/packages/cli/src/commands/upgrade.ts b/packages/cli/src/commands/upgrade.ts
index ac45150a81..221942454f 100644
--- a/packages/cli/src/commands/upgrade.ts
+++ b/packages/cli/src/commands/upgrade.ts
@@ -181,29 +181,40 @@ async function runBundleUpdate(projectDir: string, flags: UpgradeFlags): Promise
return true
}
+// A bundle out of sync with Gemfile.lock (a pulled lockfile change, a cold
+// bundle_cache volume, an un-checked-out git source) makes `bundle list` fail.
+// `bundle install` is the fix every time, so run it and list again rather than
+// sending the operator off to run it by hand.
export async function detectSpreeGems(projectDir: string): Promise<string[]> {
try {
- // Filter in JS rather than piping through grep: `bundle list` exits 0 even
- // with zero spree gems, so a nonzero exit unambiguously means bundler
- // itself errored (out-of-sync lockfile, un-checked-out git source) — no
- // exit-code disambiguation against grep's "no match" needed.
- const stdout = await dockerComposeCapture(['bundle', 'list', '--name-only'], projectDir)
- return stdout
- .split('\n')
- .map((line) => line.trim())
- .filter((line) => line.startsWith('spree'))
+ return await listSpreeGems(projectDir)
+ } catch {
+ p.log.step(pc.bold('bundle install') + pc.dim(' — the bundle is out of sync with Gemfile.lock'))
+ await dockerComposeExecOrRun(['bundle', 'install'], projectDir)
+ }
+
+ try {
+ return await listSpreeGems(projectDir)
} catch (err) {
const e = err as { stderr?: string }
- // Surface bundler's error + the real next step instead of laundering it
- // into a misleading "No Spree gems detected".
throw new Error(
- 'Could not list gems in the web container — the bundle looks out of sync.\n' +
- 'Run `spree bundle install` first, then re-run `spree upgrade`.' +
+ 'Could not list gems in the web container, even after `bundle install`.' +
(e.stderr?.trim() ? `\n\n${e.stderr.trim()}` : ''),
)
}
}
+// Filter in JS rather than piping through grep: `bundle list` exits 0 even
+// with zero spree gems, so a nonzero exit unambiguously means bundler itself
+// errored.
+async function listSpreeGems(projectDir: string): Promise<string[]> {
+ const stdout = await dockerComposeCapture(['bundle', 'list', '--name-only'], projectDir)
+ return stdout
+ .split('\n')
+ .map((line) => line.trim())
+ .filter((line) => line.startsWith('spree'))
+}
+
async function runMigrate(projectDir: string, flags: UpgradeFlags): Promise<void> {
if (!(await confirmStep('Install + run pending migrations?', flags))) {
p.log.info('Skipping migrations.')
diff --git a/packages/cli/tests/upgrade.test.ts b/packages/cli/tests/upgrade.test.ts
index 7cc4005dc4..2bb1520c0c 100644
--- a/packages/cli/tests/upgrade.test.ts
+++ b/packages/cli/tests/upgrade.test.ts
@@ -123,25 +123,36 @@ describe('detectSpreeGems', () => {
await expect(detectSpreeGems('/proj')).resolves.toEqual([])
})
- it('throws a bundle-install hint when bundler itself errors', async () => {
- routeBundleList(async () => {
- throw Object.assign(new Error('x'), {
- exitCode: 1,
- stderr:
- 'The git source https://github.com/spree/spree.git is not yet checked out. Please run bundle install',
- })
- })
- await expect(detectSpreeGems('/proj')).rejects.toThrow(/spree bundle install/)
- await expect(detectSpreeGems('/proj')).rejects.toThrow(/not yet checked out/)
+ it('runs bundle install and lists again when the bundle is out of sync', async () => {
+ let listCalls = 0
+ const calls: string[] = []
+ mockExeca.mockImplementation((async (_cmd: string, args: string[]) => {
+ if (args.includes('ps')) return { stdout: 'running' }
+ if (args.includes('list')) {
+ listCalls += 1
+ if (listCalls === 1)
+ throw Object.assign(new Error('x'), {
+ exitCode: 1,
+ stderr: 'The git source https://github.com/spree/spree.git is not yet checked out.',
+ })
+ return { stdout: 'spree\nspree_core\n' }
+ }
+ calls.push(args.join(' '))
+ return { stdout: '' }
+ }) as never)
+
+ await expect(detectSpreeGems('/proj')).resolves.toEqual(['spree', 'spree_core'])
+ expect(calls).toEqual(['compose exec web bundle install'])
})
- it('wraps daemon-level failures (exitCode 255) with the bundle hint', async () => {
- routeBundleList(async () => {
- throw Object.assign(new Error('x'), { exitCode: 255, stderr: 'no such service: web' })
- })
- await expect(detectSpreeGems('/proj')).rejects.toThrow(
- /bundle looks out of sync|no such service/,
- )
+ it('throws with the bundler error when listing still fails after bundle install', async () => {
+ mockExeca.mockImplementation((async (_cmd: string, args: string[]) => {
+ if (args.includes('ps')) return { stdout: 'running' }
+ if (args.includes('install')) return { stdout: '' }
+ throw Object.assign(new Error('x'), { exitCode: 1, stderr: 'Could not find gem spree_core' })
+ }) as never)
+ await expect(detectSpreeGems('/proj')).rejects.toThrow(/even after `bundle install`/)
+ await expect(detectSpreeGems('/proj')).rejects.toThrow(/Could not find gem spree_core/)
})
})