Skip to content

Commit 307ca3e

Browse files
committed
feat: invalidate code bundle when installCommmand changes
Taking the installCommand into account when computing the code bundle hash will ensure we don't reuse the cache in case the installCommand changes.
1 parent 0203551 commit 307ca3e

3 files changed

Lines changed: 20 additions & 5 deletions

File tree

packages/cli/src/constructs/playwright-check.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,12 +396,12 @@ export class PlaywrightCheck extends RuntimeCheck {
396396
return `${testCommand} --config ${quotedPath}${projectArg}${tagArg}`
397397
}
398398

399-
static async bundleProject (playwrightConfigPath: string, include: string[]) {
399+
static async bundleProject (playwrightConfigPath: string, include: string[], installCommand?: string) {
400400
let dir = ''
401401
try {
402402
const {
403403
outputFile, browsers, relativePlaywrightConfigPath, cacheHash, playwrightVersion,
404-
} = await bundlePlayWrightProject(playwrightConfigPath, include)
404+
} = await bundlePlayWrightProject(playwrightConfigPath, include, installCommand)
405405
dir = outputFile
406406
const { data: { key } } = await PlaywrightCheck.uploadPlaywrightProject(dir)
407407
return { key, browsers, relativePlaywrightConfigPath, cacheHash, playwrightVersion }
@@ -432,7 +432,7 @@ export class PlaywrightCheck extends RuntimeCheck {
432432
cacheHash,
433433
playwrightVersion,
434434
relativePlaywrightConfigPath,
435-
} = await PlaywrightCheck.bundleProject(this.playwrightConfigPath, this.include ?? [])
435+
} = await PlaywrightCheck.bundleProject(this.playwrightConfigPath, this.include ?? [], this.installCommand)
436436

437437
const testCommand = PlaywrightCheck.buildTestCommand(
438438
this.testCommand,

packages/cli/src/services/__tests__/util.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,17 @@ describe('util', () => {
181181
await expect(fs.access(nodeModulesPath)).rejects.toThrow()
182182
}, 30000)
183183

184+
it('should produce different cacheHash when installCommand is provided', async () => {
185+
const fixtureDir = path.join(__dirname, 'fixtures', 'playwright-bundle-test')
186+
const playwrightConfigPath = path.join(fixtureDir, 'playwright.config.ts')
187+
Session.ignoreDirectoriesMatch = []
188+
189+
const resultWithout = await bundlePlayWrightProject(playwrightConfigPath, [])
190+
const resultWith = await bundlePlayWrightProject(playwrightConfigPath, [], 'npm ci')
191+
192+
expect(resultWith.cacheHash).not.toEqual(resultWithout.cacheHash)
193+
}, 30000)
194+
184195
it('should exclude node_modules with broad patterns despite include', async () => {
185196
const fixtureDir = path.join(__dirname, 'fixtures', 'playwright-bundle-test')
186197
const playwrightConfigPath = path.join(fixtureDir, 'playwright.config.ts')

packages/cli/src/services/util.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ export function normalizeVersion (v?: string | undefined): string | undefined {
191191
export async function bundlePlayWrightProject (
192192
playwrightConfig: string,
193193
include: string[],
194+
installCommand?: string,
194195
): Promise<{
195196
outputFile: string
196197
browsers: string[]
@@ -229,7 +230,7 @@ export async function bundlePlayWrightProject (
229230
const playwrightVersion = getPlaywrightVersionFromPackage(dir)
230231

231232
const [cacheHash] = await Promise.all([
232-
getCacheHash(lockfile),
233+
getCacheHash(lockfile, installCommand),
233234
loadPlaywrightProjectFiles(dir, pwConfigParsed, include, archive, lockfile),
234235
])
235236

@@ -251,10 +252,13 @@ export async function bundlePlayWrightProject (
251252
})
252253
}
253254

254-
export async function getCacheHash (lockFile: string): Promise<string> {
255+
async function getCacheHash (lockFile: string, installCommand: string): Promise<string> {
255256
const fileBuffer = await readFile(lockFile)
256257
const hash = createHash('sha256')
257258
hash.update(fileBuffer)
259+
if (installCommand) {
260+
hash.update(installCommand)
261+
}
258262
return hash.digest('hex')
259263
}
260264

0 commit comments

Comments
 (0)