Skip to content

issue-245: fix CommonJS interoperability in tsc-multi and shim files #258

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default tseslint.config(
'examples/realtime-demo/**',
'examples/nextjs/**',
'integration-tests//**',
'tsc-multi.json',
]),
eslint.configs.recommended,
tseslint.configs.recommended,
Expand Down
17 changes: 12 additions & 5 deletions packages/agents-core/src/shims/shims-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,18 @@ declare global {
// circular dependency resolution issues caused by other exports in '@openai/agents-core/_shims'
export function loadEnv(): Record<string, string | undefined> {
if (typeof process === 'undefined' || typeof process.env === 'undefined') {
if (
typeof import.meta === 'object' &&
typeof import.meta.env === 'object'
) {
return import.meta.env as unknown as Record<string, string | undefined>;
// In CommonJS builds, import.meta is not available, so we return empty object
try {
// Use eval to avoid TypeScript compilation errors in CommonJS builds
const importMeta = (0, eval)('import.meta');
if (
typeof importMeta === 'object' &&
typeof importMeta.env === 'object'
) {
return importMeta.env as unknown as Record<string, string | undefined>;
}
} catch {
// import.meta not available (CommonJS build)
}
return {};
}
Expand Down
17 changes: 12 additions & 5 deletions packages/agents-core/src/shims/shims-workerd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ declare global {
// circular dependency resolution issues caused by other exports in '@openai/agents-core/_shims'
export function loadEnv(): Record<string, string | undefined> {
if (typeof process === 'undefined' || typeof process.env === 'undefined') {
if (
typeof import.meta === 'object' &&
typeof import.meta.env === 'object'
) {
return import.meta.env as unknown as Record<string, string | undefined>;
// In CommonJS builds, import.meta is not available, so we return empty object
try {
// Use eval to avoid TypeScript compilation errors in CommonJS builds
const importMeta = (0, eval)('import.meta');
if (
typeof importMeta === 'object' &&
typeof importMeta.env === 'object'
) {
return importMeta.env as unknown as Record<string, string | undefined>;
}
} catch {
// import.meta not available (CommonJS build)
}
return {};
}
Expand Down
2 changes: 1 addition & 1 deletion tsc-multi.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"targets": [
{ "extname": ".js", "module": "es2022", "moduleResolution": "node" },
{ "extname": ".js", "module": "commonjs", "moduleResolution": "node" },
Copy link
Member

@seratch seratch Jul 28, 2025

Choose a reason for hiding this comment

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

Thanks for changing this. This change looks reasonable, but we need to verify these before merging the change:

I will spend time on this later this week soon.

{ "extname": ".mjs", "module": "esnext" }
],
"projects": [
Expand Down