Skip to content

Commit ac48c83

Browse files
authored
fix: add dry-run feature flag for edge-function tarball generation (#6712)
1 parent a8a607a commit ac48c83

File tree

4 files changed

+109
-15
lines changed

4 files changed

+109
-15
lines changed

packages/edge-bundler/node/bridge.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ export class DenoBridge {
7171
this.useGlobal = options.useGlobal ?? true
7272

7373
const useNextDeno =
74-
options.featureFlags?.edge_bundler_generate_tarball || options.featureFlags?.edge_bundler_deno_v2
74+
options.featureFlags?.edge_bundler_dry_run_generate_tarball ||
75+
options.featureFlags?.edge_bundler_generate_tarball ||
76+
options.featureFlags?.edge_bundler_deno_v2
7577

7678
this.versionRange = options.versionRange ?? (useNextDeno ? NEXT_DENO_VERSION_RANGE : DENO_VERSION_RANGE)
7779
}

packages/edge-bundler/node/bundler.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,82 @@ describe.skipIf(lt(denoVersion, '2.4.3'))(
828828
await cleanup()
829829
await rm(vendorDirectory.path, { force: true, recursive: true })
830830
})
831+
832+
describe('Dry-run tarball generation flag enabled', () => {
833+
test('Logs success message when tarball generation succeeded', async () => {
834+
const systemLogger = vi.fn()
835+
const { basePath, cleanup, distPath } = await useFixture('imports_node_builtin', { copyDirectory: true })
836+
const declarations: Declaration[] = [
837+
{
838+
function: 'func1',
839+
path: '/func1',
840+
},
841+
]
842+
843+
await bundle([join(basePath, 'netlify/edge-functions')], distPath, declarations, {
844+
basePath,
845+
configPath: join(basePath, '.netlify/edge-functions/config.json'),
846+
featureFlags: {
847+
edge_bundler_dry_run_generate_tarball: true,
848+
edge_bundler_generate_tarball: false,
849+
},
850+
systemLogger,
851+
})
852+
853+
expect(systemLogger).toHaveBeenCalledWith('Dry run: Tarball bundle generated successfully.')
854+
855+
const manifestFile = await readFile(resolve(distPath, 'manifest.json'), 'utf8')
856+
const manifest = JSON.parse(manifestFile)
857+
858+
expect(manifest.bundles.length).toBe(1)
859+
expect(manifest.bundles[0].format).toBe('eszip2')
860+
861+
await cleanup()
862+
})
863+
864+
test('Logs error message when tarball generation failed and does not fail the overall build', async () => {
865+
const systemLogger = vi.fn()
866+
vi.resetModules()
867+
vi.doMock('./formats/tarball.js', () => ({
868+
bundle: vi.fn().mockRejectedValue(new Error('Simulated tarball bundling failure')),
869+
}))
870+
871+
const { bundle: bundleUnderTest } = await import('./bundler.js')
872+
873+
const { basePath, cleanup, distPath } = await useFixture('imports_node_builtin', { copyDirectory: true })
874+
const sourceDirectory = join(basePath, 'functions')
875+
const declarations: Declaration[] = [
876+
{
877+
function: 'func1',
878+
path: '/func1',
879+
},
880+
]
881+
882+
await expect(
883+
bundleUnderTest([sourceDirectory], distPath, declarations, {
884+
basePath,
885+
configPath: join(sourceDirectory, 'config.json'),
886+
featureFlags: {
887+
edge_bundler_dry_run_generate_tarball: true,
888+
edge_bundler_generate_tarball: false,
889+
},
890+
systemLogger,
891+
}),
892+
).resolves.toBeDefined()
893+
894+
expect(systemLogger).toHaveBeenCalledWith(
895+
`Dry run: Tarball bundle generation failed: Simulated tarball bundling failure`,
896+
)
897+
898+
const manifestFile = await readFile(resolve(distPath, 'manifest.json'), 'utf8')
899+
const manifest = JSON.parse(manifestFile)
900+
expect(manifest.bundles.length).toBe(1)
901+
expect(manifest.bundles[0].format).toBe('eszip2')
902+
903+
await cleanup()
904+
vi.resetModules()
905+
})
906+
})
831907
},
832908
10_000,
833909
)

packages/edge-bundler/node/bundler.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,35 @@ export const bundle = async (
119119

120120
const bundles: Bundle[] = []
121121

122-
if (featureFlags.edge_bundler_generate_tarball) {
123-
bundles.push(
124-
await bundleTarball({
125-
basePath,
126-
buildID,
127-
debug,
128-
deno,
129-
distDirectory,
130-
functions,
131-
featureFlags,
132-
importMap: importMap.clone(),
133-
vendorDirectory: vendor?.directory,
134-
}),
135-
)
122+
if (featureFlags.edge_bundler_generate_tarball || featureFlags.edge_bundler_dry_run_generate_tarball) {
123+
const tarballPromise = bundleTarball({
124+
basePath,
125+
buildID,
126+
debug,
127+
deno,
128+
distDirectory,
129+
functions,
130+
featureFlags,
131+
importMap: importMap.clone(),
132+
vendorDirectory: vendor?.directory,
133+
})
134+
135+
if (featureFlags.edge_bundler_dry_run_generate_tarball) {
136+
try {
137+
await tarballPromise
138+
logger.system('Dry run: Tarball bundle generated successfully.')
139+
} catch (error: unknown) {
140+
if (error instanceof Error) {
141+
logger.system(`Dry run: Tarball bundle generation failed: ${error.message}`)
142+
} else {
143+
logger.system(`Dry run: Tarball bundle generation failed: ${String(error)}`)
144+
}
145+
}
146+
}
147+
148+
if (featureFlags.edge_bundler_generate_tarball) {
149+
bundles.push(await tarballPromise)
150+
}
136151
}
137152

138153
if (vendor) {

packages/edge-bundler/node/feature_flags.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const defaultFlags = {
22
edge_bundler_generate_tarball: false,
3+
edge_bundler_dry_run_generate_tarball: false,
34
edge_bundler_deno_v2: false,
45
}
56

0 commit comments

Comments
 (0)