diff --git a/packages/eslint-plugin-turbo/lib/rules/no-undeclared-env-vars.ts b/packages/eslint-plugin-turbo/lib/rules/no-undeclared-env-vars.ts index 677ba8aa1e6ff..f490776f04f43 100644 --- a/packages/eslint-plugin-turbo/lib/rules/no-undeclared-env-vars.ts +++ b/packages/eslint-plugin-turbo/lib/rules/no-undeclared-env-vars.ts @@ -34,6 +34,22 @@ const meta: Rule.RuleMetaData = { ], }; +/** + * Normalize the value of the cwd + * Extracted from eslint + * SPDX-License-Identifier: MIT + */ +function normalizeCwd(cwd: string | undefined): string | undefined { + if (cwd) { + return cwd; + } + if (typeof process === "object") { + return process.cwd(); + } + + return undefined; +} + function create(context: Rule.RuleContext): Rule.RuleListener { const { options } = context; const allowList: Array = options?.[0]?.allowList || []; @@ -46,9 +62,11 @@ function create(context: Rule.RuleContext): Rule.RuleListener { console.error(`Unable to convert "${allowed}" to regex`); } }); + + const cwd = normalizeCwd(context.getCwd ? context.getCwd() : undefined); const turboConfig = options?.[0]?.turboConfig; const turboVars = getEnvVarDependencies({ - cwd: context.getCwd(), + cwd, turboConfig, }); diff --git a/packages/eslint-plugin-turbo/lib/utils/getEnvVarDependencies.ts b/packages/eslint-plugin-turbo/lib/utils/getEnvVarDependencies.ts index 2e877fd6b7e3a..86f583b126e4a 100644 --- a/packages/eslint-plugin-turbo/lib/utils/getEnvVarDependencies.ts +++ b/packages/eslint-plugin-turbo/lib/utils/getEnvVarDependencies.ts @@ -23,7 +23,7 @@ function getEnvVarDependencies({ cwd, turboConfig, }: { - cwd: string; + cwd: string | undefined; turboConfig?: Schema; }): Set | null { const turboJsonContent = turboConfig || findTurboConfig({ cwd });