Skip to content

Conversation

@Devesh36
Copy link

@Devesh36 Devesh36 commented Dec 9, 2025

/fix #1167

Overview

Changed the transpile command to throw an error instead of warning when package.json main or exports fields are not properly configured to point to the dist/ directory.


Behavior Changes

1. Main Field Points Outside dist/

❌ BEFORE

$ cat package.json
{
  "name": "my-package",
  "main": "index.tsx"
}

$ tsci transpile index.tsx
Screenshot 2025-12-09 at 10 20 28 AM

Output:


When using transpilation, your package's "main" field should point inside the `dist/*` directory, usually to "dist/index.js"
Transpiling entry file...
Building ESM bundle...
[... transpilation continues ...]
Transpile complete!

Exit Code: 0 (SUCCESS - continues despite misconfiguration)


✅ AFTER

$ cat package.json
{
  "name": "my-package",
  "main": "index.tsx"
}

$ tsci transpile index.tsx

Output:

When using transpilation, your package's "main" field must point inside the `dist/*` directory, usually to "dist/index.js"

Exit Code: 1 (ERROR - stops immediately)


2. Missing Both main and exports Fields

❌ BEFORE

$ cat package.json
{
  "name": "my-package",
  "version": "1.0.0"
}

$ tsci transpile index.tsx

Output:

Transpiling entry file...
Building ESM bundle...
[... transpilation continues despite missing fields ...]

Exit Code: 0 (SUCCESS - no validation for missing fields)


✅ AFTER

$ cat package.json
{
  "name": "my-package",
  "version": "1.0.0"
}

$ tsci transpile index.tsx
Screenshot 2025-12-09 at 10 22 00 AM

Output:

When using transpilation, your package.json must have either a "main" or "exports" field pointing to the output in the `dist/*` directory

Exit Code: 1 (ERROR - stops immediately)


3. Invalid exports Field (NEW - Only In AFTER)

❌ BEFORE

$ cat package.json
{
  "exports": {
    ".": "./lib/index.js",
    "./utils": "./utils.js"
  }
}

$ tsci transpile index.tsx

Output:

Transpiling entry file...
[... transpilation continues with invalid exports ...]

Exit Code: 0 (SUCCESS - no exports validation)


✅ AFTER

$ cat package.json
{
  "exports": {
    ".": "./lib/index.js",
    "./utils": "./utils.js"
  }
}

$ tsci transpile index.tsx

Output:

When using transpilation, your package's "exports" field must point to outputs in the `dist/*` directory

Exit Code: 1 (ERROR - validates all export paths)


Error Messages Summary

Scenario BEFORE AFTER
main outside dist ⚠️ Warning: "should point" ❌ Error: "must point"
Missing main & exports ✅ No validation ❌ Error: "must have either"
Invalid exports paths ✅ No validation ❌ Error: "must point to outputs"
Valid main/exports ✅ Proceeds ✅ Proceeds

Implementation Details

Files Modified

  1. cli/utils/validate-main-in-dist.ts

    • Changed from console.warn() to throw new Error()
    • Added validation for missing main and exports fields
    • Added new validateExports() helper function
    • Recursive validation for nested export structures
  2. tests/cli/transpile/transpile.test.ts

    • Test renamed: "warns when main is outside dist" → "throws error when main is outside dist"
    • Error message updated: "should point" → "must point"
    • New test: "throws error when main and exports are not set"
  3. tests/cli/build/build-transpile.test.ts

    • Test renamed: "warns when main is outside dist" → "throws error when main is outside dist"
    • Error message updated: "should point" → "must point"
    • New test: "throws error when main and exports are not set"

Error Handling

Both cli/transpile/register.ts and cli/build/register.ts already have try-catch blocks that catch and properly handle the thrown errors:

try {
  validateMainInDist(projectDir, distDir)
  // ... transpile logic ...
} catch (error) {
  const message = error instanceof Error ? error.message : String(error)
  console.error(message)
  process.exit(1)
}

Test Results

Transpile Tests

$ bun test tests/cli/transpile/transpile.test.ts
(pass) transpile throws error when main is outside dist [656.39ms]
(pass) transpile throws error when main and exports are not set [728.92ms]

Build Tests

$ bun test tests/cli/build/build-transpile.test.ts
(pass) build with --transpile throws error when main is outside dist [1465.01ms]
(pass) build with --transpile throws error when main and exports are not set [1549.87ms]
  1. Early Error Detection: Catches misconfiguration before spending time on transpilation
  2. Clearer Intent: "must point" is stronger and clearer than "should point"
  3. Comprehensive Validation: Now validates both main and exports fields
  4. Nested Export Support: Recursively validates complex export structures
  5. Prevents Package Issues: Ensures published packages will have correct entry points

@Devesh36
Copy link
Author

Devesh36 commented Dec 9, 2025

i think the check is failing because of it's a Windows-only test running on macOS

Copy link

@rushabhcodes rushabhcodes left a comment

Choose a reason for hiding this comment

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

The test does not run on you device, i think there is a problem in the test or you introduced one

@Devesh36
Copy link
Author

Devesh36 commented Dec 9, 2025

The test does not run on you device, i think there is a problem in the test or you introduced one

yaa fixed it ! Did a obvious mistake ;)

@Devesh36 Devesh36 requested a review from rushabhcodes December 9, 2025 09:00
Copy link
Member

@imrishabh18 imrishabh18 left a comment

Choose a reason for hiding this comment

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

You should not be throwing an error for this, it should be a warning how it was before.

The issue lacked context when that was created

@Devesh36
Copy link
Author

Devesh36 commented Dec 9, 2025

You should not be throwing an error for this, it should be a warning how it was before.

The issue lacked context when that was created

ok then ! should i close this commit ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Throw an error on transpile if the package.json main or exports is not set properly

3 participants