Skip to content

v3: pnpm support #1000

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

Merged
merged 6 commits into from
Apr 4, 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/spicy-lamps-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"trigger.dev": patch
---

Fixing an issue with bundling @trigger.dev/core/v3 in dev when using pnpm
1 change: 0 additions & 1 deletion packages/cli-v3/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@
"simple-git": "^3.19.0",
"socket.io-client": "^4.7.4",
"source-map-support": "^0.5.21",
"supports-color": "^9.4.0",
"terminal-link": "^3.0.0",
"tiny-invariant": "^1.2.0",
"tsconfig-paths": "^4.2.0",
Expand Down
47 changes: 43 additions & 4 deletions packages/cli-v3/src/commands/dev.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import { z } from "zod";
import * as packageJson from "../../package.json";
import { CliApiClient } from "../apiClient";
import { CommonCommandOptions, commonOptions, wrapCommandAction } from "../cli/common.js";
import { bundleDependenciesPlugin, workerSetupImportConfigPlugin } from "../utilities/build";
import {
bundleDependenciesPlugin,
bundleTriggerDevCore,
workerSetupImportConfigPlugin,
} from "../utilities/build";
import { chalkError, chalkGrey, chalkPurple, chalkTask, chalkWorker } from "../utilities/cliOutput";
import { readConfig } from "../utilities/configFiles";
import { readJSONFile } from "../utilities/fileSystem";
Expand All @@ -47,6 +51,7 @@ import {
parseBuildErrorStack,
parseNpmInstallError,
} from "../utilities/deployErrors";
import { findUp, pathExists } from "find-up";

let apiClient: CliApiClient | undefined;

Expand Down Expand Up @@ -384,6 +389,7 @@ function useDev({
__PROJECT_CONFIG__: JSON.stringify(config),
},
plugins: [
bundleTriggerDevCore("workerFacade", config.tsconfigPath),
bundleDependenciesPlugin(
"workerFacade",
(config.dependenciesToBundle ?? []).concat([/^@trigger.dev/]),
Expand Down Expand Up @@ -460,7 +466,7 @@ function useDev({
const environmentVariablesResponse =
await environmentClient.getEnvironmentVariables(config.project);

const processEnv = gatherProcessEnv();
const processEnv = await gatherProcessEnv();

const backgroundWorker = new BackgroundWorker(fullPath, {
projectConfig: config,
Expand Down Expand Up @@ -770,7 +776,7 @@ function createDuplicateTaskIdOutputErrorMessage(
return `Duplicate ${chalkTask("task id")} detected:${duplicateTable}`;
}

function gatherProcessEnv() {
async function gatherProcessEnv() {
const env = {
NODE_ENV: process.env.NODE_ENV ?? "development",
PATH: process.env.PATH,
Expand All @@ -781,11 +787,44 @@ function gatherProcessEnv() {
NVM_BIN: process.env.NVM_BIN,
LANG: process.env.LANG,
TERM: process.env.TERM,
NODE_PATH: process.env.NODE_PATH,
NODE_PATH: await amendNodePathWithPnpmNodeModules(process.env.NODE_PATH),
HOME: process.env.HOME,
BUN_INSTALL: process.env.BUN_INSTALL,
};

// Filter out undefined values
return Object.fromEntries(Object.entries(env).filter(([key, value]) => value !== undefined));
}

async function amendNodePathWithPnpmNodeModules(nodePath?: string): Promise<string | undefined> {
const pnpmModulesPath = await findPnpmNodeModulesPath();

if (!pnpmModulesPath) {
return nodePath;
}

if (nodePath) {
if (nodePath.includes(pnpmModulesPath)) {
return nodePath;
}

return `${nodePath}:${pnpmModulesPath}`;
}

return pnpmModulesPath;
}

async function findPnpmNodeModulesPath(): Promise<string | undefined> {
return await findUp(
async (directory) => {
const pnpmModules = join(directory, "node_modules", ".pnpm", "node_modules");

const hasPnpmNodeModules = await pathExists(pnpmModules);

if (hasPnpmNodeModules) {
return pnpmModules;
}
},
{ type: "directory" }
);
}
38 changes: 38 additions & 0 deletions packages/cli-v3/src/utilities/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,44 @@ import { extname, isAbsolute } from "node:path";
import tsConfigPaths from "tsconfig-paths";
import { logger } from "./logger";

export function bundleTriggerDevCore(buildIdentifier: string, tsconfigPath?: string): Plugin {
return {
name: "trigger-bundle-core",
setup(build) {
build.onResolve({ filter: /.*/ }, (args) => {
if (args.path !== "@trigger.dev/core/v3") {
return undefined;
}

const triggerSdkPath = require.resolve("@trigger.dev/sdk/v3", { paths: [process.cwd()] });

logger.debug(`[${buildIdentifier}][trigger-bundle-core] Resolved @trigger.dev/sdk/v3`, {
...args,
triggerSdkPath,
});

const resolvedPath = require.resolve("@trigger.dev/core/v3", {
paths: [triggerSdkPath],
});

logger.debug(
`[${buildIdentifier}][trigger-bundle-core] Externalizing @trigger.dev/core/v3`,
{
...args,
triggerSdkPath,
resolvedPath,
}
);

return {
path: resolvedPath,
external: false,
};
});
},
};
}

export function workerSetupImportConfigPlugin(configPath?: string): Plugin {
return {
name: "trigger-worker-setup",
Expand Down
3 changes: 1 addition & 2 deletions packages/cli-v3/src/utilities/initialBanner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { spinner } from "@clack/prompts";
import chalk from "chalk";
import supportsColor from "supports-color";
import type { Result } from "update-check";
import checkForUpdate from "update-check";
import pkg from "../../package.json";
Expand Down Expand Up @@ -52,7 +51,7 @@ export async function printStandloneInitialBanner(performUpdateCheck = true) {
}
}

logger.log(text + "\n" + (supportsColor.stdout ? chalkGrey("-".repeat(54)) : "-".repeat(54)));
logger.log(text + "\n" + chalkGrey("-".repeat(54)));
}

export function printDevBanner() {
Expand Down
20 changes: 15 additions & 5 deletions packages/cli-v3/src/workers/dev/backgroundWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,20 @@ export class BackgroundWorker {

let resolved = false;

const cwd = dirname(this.path);

const fullEnv = {
...this.params.env,
...this.#readEnvVars(),
};

logger.debug("Initializing worker", { path: this.path, cwd, fullEnv });

this.tasks = await new Promise<Array<TaskMetadataWithFilePath>>((resolve, reject) => {
const child = fork(this.path, {
stdio: [/*stdin*/ "ignore", /*stdout*/ "pipe", /*stderr*/ "pipe", "ipc"],
env: {
...this.params.env,
...this.#readEnvVars(),
},
cwd,
env: fullEnv,
});

// Set a timeout to kill the child process if it doesn't respond
Expand Down Expand Up @@ -580,14 +587,17 @@ class TaskRunProcess {
...(this.worker.debugOtel ? { OTEL_LOG_LEVEL: "debug" } : {}),
};

const cwd = dirname(this.path);

logger.debug(`[${this.execution.run.id}] initializing task run process`, {
env: fullEnv,
path: this.path,
cwd,
});

this._child = fork(this.path, {
stdio: [/*stdin*/ "ignore", /*stdout*/ "pipe", /*stderr*/ "pipe", "ipc"],
cwd: dirname(this.path),
cwd,
env: fullEnv,
execArgv: this.worker.debuggerOn
? ["--inspect-brk", "--trace-uncaught", "--no-warnings=ExperimentalWarning"]
Expand Down
Loading