v3: Improved ESM module require error detection logic

This commit is contained in:
Eric Allam
2024-06-25 09:08:43 +01:00
parent 8a5076aacf
commit 77ad4127cb
9 changed files with 123 additions and 17 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"trigger.dev": patch
---
Improved ESM module require error detection logic
+4
View File
@@ -640,6 +640,10 @@ function useDev({
backgroundWorker
);
} catch (e) {
logger.debug("Error starting background worker", {
error: e,
});
if (e instanceof TaskMetadataParseError) {
logTaskMetadataParseError(e.zodIssues, e.tasks);
return;
+35 -10
View File
@@ -28,17 +28,10 @@ export function parseBuildErrorStack(error: unknown): BuildError | undefined {
if (errorIsErrorLike(error)) {
if (typeof error.stack === "string") {
const isErrRequireEsm = error.stack.includes("ERR_REQUIRE_ESM");
let moduleName = null;
if (isErrRequireEsm) {
// Regular expression to match the module path
const moduleRegex = /node_modules\/(@[^\/]+\/[^\/]+|[^\/]+)\/[^\/]+\s/;
const match = moduleRegex.exec(error.stack);
if (match) {
moduleName = match[1] as string; // Capture the module name
if (error.stack.includes("ERR_REQUIRE_ESM")) {
const moduleName = getPackageNameFromEsmRequireError(error.stack);
if (moduleName) {
return {
type: "esm-require-error",
moduleName,
@@ -51,6 +44,38 @@ export function parseBuildErrorStack(error: unknown): BuildError | undefined {
}
}
function getPackageNameFromEsmRequireError(stack: string): string | undefined {
const pathRegex = /require\(\) of ES Module (.*) from/;
const pathMatch = pathRegex.exec(stack);
if (!pathMatch) {
return;
}
const filePath = pathMatch[1];
if (!filePath) {
return;
}
const lastPart = filePath.split("node_modules/").pop();
if (!lastPart) {
return;
}
// regular expression to match the package name
const moduleRegex = /(@[^\/]+\/[^\/]+|[^\/]+)/;
const match = moduleRegex.exec(lastPart);
if (!match) {
return;
}
return match[1];
}
export function logESMRequireError(parsedError: ESMRequireError, resolvedConfig: ReadConfigResult) {
logger.log(
`\n${chalkError("X Error:")} The ${chalkPurple(
+33
View File
@@ -3093,6 +3093,9 @@ importers:
'@sindresorhus/slugify':
specifier: ^2.2.1
version: 2.2.1
'@t3-oss/env-nextjs':
specifier: ^0.10.1
version: 0.10.1(typescript@5.3.3)(zod@3.22.3)
'@traceloop/instrumentation-openai':
specifier: ^0.3.9
version: 0.3.9(@opentelemetry/api@1.4.1)
@@ -3138,6 +3141,9 @@ importers:
yt-dlp-wrap:
specifier: ^2.3.12
version: 2.3.12
zod:
specifier: 3.22.3
version: 3.22.3
devDependencies:
'@opentelemetry/core':
specifier: ^1.22.0
@@ -15297,6 +15303,33 @@ packages:
defer-to-connect: 2.0.1
dev: false
/@t3-oss/env-core@0.10.1(typescript@5.3.3)(zod@3.22.3):
resolution: {integrity: sha512-GcKZiCfWks5CTxhezn9k5zWX3sMDIYf6Kaxy2Gx9YEQftFcz8hDRN56hcbylyAO3t4jQnQ5ifLawINsNgCDpOg==}
peerDependencies:
typescript: '>=5.0.0'
zod: ^3.0.0
peerDependenciesMeta:
typescript:
optional: true
dependencies:
typescript: 5.3.3
zod: 3.22.3
dev: false
/@t3-oss/env-nextjs@0.10.1(typescript@5.3.3)(zod@3.22.3):
resolution: {integrity: sha512-iy2qqJLnFh1RjEWno2ZeyTu0ufomkXruUsOZludzDIroUabVvHsrSjtkHqwHp1/pgPUzN3yBRHMILW162X7x2Q==}
peerDependencies:
typescript: '>=5.0.0'
zod: ^3.0.0
peerDependenciesMeta:
typescript:
optional: true
dependencies:
'@t3-oss/env-core': 0.10.1(typescript@5.3.3)(zod@3.22.3)
typescript: 5.3.3
zod: 3.22.3
dev: false
/@tabler/icons-react@2.40.0(react@18.2.0):
resolution: {integrity: sha512-C+dDOZowFbwI3LGQP0fdua+hOPkGkW7XeMcRXTSdEKc5fD75W6zRO5nXnWivIMRKsi/Y26EDmnQo15N8JX378w==}
peerDependencies:
+6 -4
View File
@@ -17,6 +17,7 @@
"@react-email/components": "^0.0.17",
"@react-email/render": "^0.0.7",
"@sindresorhus/slugify": "^2.2.1",
"@t3-oss/env-nextjs": "^0.10.1",
"@traceloop/instrumentation-openai": "^0.3.9",
"@trigger.dev/core": "workspace:^3.0.0-beta.0",
"@trigger.dev/sdk": "workspace:^3.0.0-beta.0",
@@ -31,7 +32,8 @@
"server-only": "^0.0.1",
"stripe": "^12.14.0",
"typeorm": "^0.3.20",
"yt-dlp-wrap": "^2.3.12"
"yt-dlp-wrap": "^2.3.12",
"zod": "3.22.3"
},
"devDependencies": {
"@opentelemetry/api": "^1.8.0",
@@ -52,11 +54,11 @@
"@trigger.dev/tsconfig": "workspace:*",
"@types/node": "20.4.2",
"@types/react": "^18.3.1",
"esbuild": "^0.19.11",
"trigger.dev": "workspace:*",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.3.0",
"esbuild": "^0.19.11",
"tsup": "^8.0.1"
"tsup": "^8.0.1",
"typescript": "^5.3.0"
}
}
+32
View File
@@ -0,0 +1,32 @@
import { createEnv } from "@t3-oss/env-nextjs";
import { z } from "zod";
export const env = createEnv({
/*
* Serverside Environment variables, not available on the client.
* Will throw if you access these variables on the client.
*/
server: {
TRIGGER_SECRET_KEY: z.string(),
OPENAI_API_KEY: z.string().min(1),
},
/*
* Environment variables available on the client (and server).
*
* 💡 You'll get type errors if these are not prefixed with NEXT_PUBLIC_.
*/
client: {
NEXT_PUBLIC_TEST: z.string().min(1).optional(),
},
/*
* Due to how Next.js bundles environment variables on Edge and Client,
* we need to manually destructure them to make sure all are included in bundle.
*
* 💡 You'll get type errors if not all variables from `server` & `client` are included here.
*/
runtimeEnv: {
TRIGGER_SECRET_KEY: process.env.TRIGGER_SECRET_KEY,
OPENAI_API_KEY: process.env.OPENAI_API_KEY,
NEXT_PUBLIC_TEST: process.env.NEXT_PUBLIC_TEST,
},
});
@@ -1,12 +1,14 @@
import { logger, task, wait } from "@trigger.dev/sdk/v3";
import { env } from "../env";
export const oneAtATime = task({
id: "on-at-a-time",
queue: {
concurrencyLimit: 1,
},
run: async (payload: { message: string }) => {
logger.info("One at a time task payload", { payload });
logger.info("One at a time task payload", { payload, env });
await wait.for({ seconds: 10 });
+1 -1
View File
@@ -48,7 +48,7 @@ export const config: TriggerConfig = {
enableConsoleLogging: false,
additionalPackages: ["wrangler@3.35.0", "pg@8.11.5"],
additionalFiles: ["./wrangler/wrangler.toml"],
dependenciesToBundle: [/@sindresorhus/, "escape-string-regexp"],
dependenciesToBundle: [/@sindresorhus/, "escape-string-regexp", /@t3-oss/],
instrumentations: [new OpenAIInstrumentation()],
logLevel: "info",
onStart: async (payload, { ctx }) => {
+4 -1
View File
@@ -13,6 +13,9 @@
"@trigger.dev/sdk/v3/*": ["../../packages/trigger-sdk/src/v3/*"]
},
"emitDecoratorMetadata": true,
"experimentalDecorators": true
"experimentalDecorators": true,
"allowJs": true,
"moduleResolution": "Bundler",
"module": "ES2015"
}
}