Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Extract the correct SDK version during build #1530

Merged
merged 3 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fuzzy-doors-know.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trigger.dev": patch
---

Fix SDK version in build manifest for out-of-sync detection
2 changes: 2 additions & 0 deletions apps/webapp/app/presenters/v3/DeploymentPresenter.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export class DeploymentPresenter {
},
},
sdkVersion: true,
cliVersion: true,
},
},
triggeredBy: {
Expand Down Expand Up @@ -145,6 +146,7 @@ export class DeploymentPresenter {
},
deployedBy: deployment.triggeredBy,
sdkVersion: deployment.worker?.sdkVersion,
cliVersion: deployment.worker?.cliVersion,
imageReference: deployment.imageReference,
externalBuildData:
externalBuildData && externalBuildData.success ? externalBuildData.data : undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ export default function Page() {
<Property.Label>SDK Version</Property.Label>
<Property.Value>{deployment.sdkVersion ? deployment.sdkVersion : "–"}</Property.Value>
</Property.Item>
<Property.Item>
<Property.Label>CLI Version</Property.Label>
<Property.Value>{deployment.cliVersion ? deployment.cliVersion : "–"}</Property.Value>
</Property.Item>
<Property.Item>
<Property.Label>Started at</Property.Label>
<Property.Value>
Expand Down
7 changes: 5 additions & 2 deletions packages/cli-v3/src/build/buildWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { writeJSONFile } from "../utilities/fileSystem.js";
import { isWindows } from "std-env";
import { pathToFileURL } from "node:url";
import { logger } from "../utilities/logger.js";
import { SdkVersionExtractor } from "./plugins.js";

export type BuildWorkerEventListener = {
onBundleStart?: () => void;
Expand Down Expand Up @@ -61,6 +62,8 @@ export async function buildWorker(options: BuildWorkerOptions) {
await notifyExtensionOnBuildStart(buildContext);
const pluginsFromExtensions = resolvePluginsForContext(buildContext);

const sdkVersionExtractor = new SdkVersionExtractor();

options.listener?.onBundleStart?.();

const bundleResult = await bundleWorker({
Expand All @@ -69,7 +72,7 @@ export async function buildWorker(options: BuildWorkerOptions) {
destination: options.destination,
watch: false,
resolvedConfig,
plugins: [...pluginsFromExtensions],
plugins: [sdkVersionExtractor.plugin, ...pluginsFromExtensions],
jsxFactory: resolvedConfig.build.jsx.factory,
jsxFragment: resolvedConfig.build.jsx.fragment,
jsxAutomatic: resolvedConfig.build.jsx.automatic,
Expand All @@ -81,7 +84,7 @@ export async function buildWorker(options: BuildWorkerOptions) {
contentHash: bundleResult.contentHash,
runtime: resolvedConfig.runtime ?? DEFAULT_RUNTIME,
environment: options.environment,
packageVersion: CORE_VERSION,
packageVersion: sdkVersionExtractor.sdkVersion ?? CORE_VERSION,
cliPackageVersion: VERSION,
target: "deploy",
files: bundleResult.files,
Expand Down
95 changes: 95 additions & 0 deletions packages/cli-v3/src/build/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { ResolvedConfig } from "@trigger.dev/core/v3/build";
import { configPlugin } from "../config.js";
import { logger } from "../utilities/logger.js";
import { bunPlugin } from "../runtimes/bun.js";
import { resolvePathSync as esmResolveSync } from "mlly";
import { readPackageJSON, resolvePackageJSON } from "pkg-types";
import { dirname } from "node:path";
import { readJSONFile } from "../utilities/fileSystem.js";

export async function buildPlugins(
target: BuildTarget,
Expand Down Expand Up @@ -87,3 +91,94 @@ export function polyshedPlugin(): esbuild.Plugin {
},
};
}

export class SdkVersionExtractor {
private _sdkVersion: string | undefined;
private _ranOnce = false;

get sdkVersion() {
return this._sdkVersion;
}

get plugin(): esbuild.Plugin {
return {
name: "sdk-version",
setup: (build) => {
build.onResolve({ filter: /^@trigger\.dev\/sdk\// }, async (args) => {
if (this._ranOnce) {
return undefined;
} else {
this._ranOnce = true;
}

logger.debug("[SdkVersionExtractor] Extracting SDK version", { args });

try {
const resolvedPath = esmResolveSync(args.path, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need to dynamically require mlly or else the CLI won't build

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually scratch that, it's not true. Leaving this here for self-shame reasons.

url: args.resolveDir,
});

logger.debug("[SdkVersionExtractor] Resolved SDK module path", { resolvedPath });

const packageJsonPath = await resolvePackageJSON(dirname(resolvedPath), {
test: async (filePath) => {
try {
const candidate = await readJSONFile(filePath);

// Exclude esm type markers
return Object.keys(candidate).length > 1 || !candidate.type;
} catch (error) {
logger.debug("[SdkVersionExtractor] Error during package.json test", {
error: error instanceof Error ? error.message : error,
});

return false;
}
},
});

if (!packageJsonPath) {
return undefined;
}

logger.debug("[SdkVersionExtractor] Found package.json", { packageJsonPath });

const packageJson = await readPackageJSON(packageJsonPath);

if (!packageJson.name || packageJson.name !== "@trigger.dev/sdk") {
logger.debug("[SdkVersionExtractor] No match for SDK package name", {
packageJsonPath,
packageJson,
});

return undefined;
}

if (!packageJson.version) {
logger.debug("[SdkVersionExtractor] No version found in package.json", {
packageJsonPath,
packageJson,
});

return undefined;
}

this._sdkVersion = packageJson.version;

logger.debug("[SdkVersionExtractor] Found SDK version", {
args,
packageJsonPath,
sdkVersion: this._sdkVersion,
});

return undefined;
} catch (error) {
logger.debug("[SdkVersionExtractor] Failed to extract SDK version", { error });
}

return undefined;
});
},
};
}
}