Skip to content

Commit e73e9a5

Browse files
committed
Fix theme package trying to use default environment
The package command does not support an environment flag and would fail if a default environment was present in a users `shopify.theme.toml` because we try to load it automatically. This adds an early return if we're using the default environment but the command doesn't support it.
1 parent 63c5aa5 commit e73e9a5

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

.changeset/twelve-crabs-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/cli-kit': patch
3+
---
4+
5+
Fix an issue with running `theme package` with a default environment present

packages/cli-kit/src/public/node/base-command.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,32 @@ class MockCommandWithRequiredFlagInNonTTY extends MockCommand {
6969
}
7070
}
7171

72+
class MockCommandWithoutEnvironmentFlag extends Command {
73+
/* eslint-disable @shopify/cli/command-flags-with-env */
74+
static flags = {
75+
...globalFlags,
76+
path: Flags.string({
77+
parse: async (input) => resolvePath(input),
78+
default: async () => cwd(),
79+
}),
80+
someString: Flags.string({}),
81+
}
82+
/* eslint-enable @shopify/cli/command-flags-with-env */
83+
84+
async run(): Promise<void> {
85+
const {flags} = await this.parse(MockCommandWithoutEnvironmentFlag)
86+
testResult = flags
87+
}
88+
89+
async catch(error: Error): Promise<void> {
90+
testError = error
91+
}
92+
93+
environmentsFilename(): string {
94+
return 'shopify.environments.toml'
95+
}
96+
}
97+
7298
const validEnvironment = {
7399
someString: 'stringy',
74100
someBoolean: true,
@@ -233,6 +259,23 @@ describe('applying environments', async () => {
233259
`)
234260
})
235261

262+
describe('when the command does not support the environment flag', () => {
263+
runTestInTmpDir('does not apply default environment', async (tmpDir: string) => {
264+
// Given
265+
const outputMock = mockAndCaptureOutput()
266+
outputMock.clear()
267+
268+
// When
269+
await MockCommandWithoutEnvironmentFlag.run(['--path', tmpDir])
270+
271+
// Then
272+
expect(testResult).toMatchObject({
273+
path: resolvePath(tmpDir),
274+
})
275+
expect(testResult.environment).toBeUndefined()
276+
})
277+
})
278+
236279
runTestInTmpDir(
237280
'base Command does not apply environment specified via environment variable',
238281
async (tmpDir: string) => {

packages/cli-kit/src/public/node/base-command.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ This flag is required in non-interactive terminal environments, such as a CI env
166166
)
167167

168168
if (!environment) return originalResult
169+
if (isDefaultEnvironment && !commandSupportsFlag(options?.flags, 'environment')) return originalResult
169170

170171
// Parse using noDefaultsOptions to derive a list of flags specified as
171172
// command-line arguments.
@@ -307,7 +308,7 @@ function argsFromEnvironment<TFlags extends FlagOutput, TGlobalFlags extends Fla
307308
): string[] {
308309
const args: string[] = []
309310
for (const [label, value] of Object.entries(environment)) {
310-
const flagIsRelevantToCommand = options?.flags && Object.prototype.hasOwnProperty.call(options.flags, label)
311+
const flagIsRelevantToCommand = commandSupportsFlag(options?.flags, label)
311312
const userSpecifiedThisFlag =
312313
noDefaultsResult.flags && Object.prototype.hasOwnProperty.call(noDefaultsResult.flags, label)
313314
if (flagIsRelevantToCommand && !userSpecifiedThisFlag) {
@@ -331,4 +332,8 @@ function argsFromEnvironment<TFlags extends FlagOutput, TGlobalFlags extends Fla
331332
return args
332333
}
333334

335+
function commandSupportsFlag(flags: FlagInput | undefined, flagName: string): boolean {
336+
return Boolean(flags) && Object.prototype.hasOwnProperty.call(flags, flagName)
337+
}
338+
334339
export default BaseCommand

0 commit comments

Comments
 (0)