Skip to content

fix: Include source url for parsing failures#109

Merged
timfish merged 3 commits intonodejs:mainfrom
timfish:fix/improve-error
Jun 29, 2024
Merged

fix: Include source url for parsing failures#109
timfish merged 3 commits intonodejs:mainfrom
timfish:fix/improve-error

Conversation

@timfish
Copy link
Copy Markdown
Contributor

@timfish timfish commented Jun 18, 2024

Since #106 might not get merged, I cherry picked the improvements to logged error messages when module wrapping fails due to parsing failures.

processModule may parse multiple files in the process of wrapping a module so it's important to log the exact file the fails parsing.

Error: 'import-in-the-middle' failed to wrap 'file:///Users/tim/Documents/Repositories/import-in-the-middle/test/fixtures/json-attributes.mjs'
    at load (/Users/tim/Documents/Repositories/import-in-the-middle/hook.js:306:21)
    at async nextLoad (node:internal/modules/esm/hooks:750:22)
    at async Hooks.load (node:internal/modules/esm/hooks:383:20)
    at async MessagePort.handleMessage (node:internal/modules/esm/worker:199:18) {
  cause: Error: Failed to parse 'file:///Users/tim/Documents/Repositories/import-in-the-middle/test/fixtures/json-attributes.mjs'
      at getExports (/Users/tim/Documents/Repositories/import-in-the-middle/lib/get-exports.js:115:17)
      at async processModule (/Users/tim/Documents/Repositories/import-in-the-middle/hook.js:131:23)
      at async load (/Users/tim/Documents/Repositories/import-in-the-middle/hook.js:269:25)
      at async nextLoad (node:internal/modules/esm/hooks:750:22)
      at async Hooks.load (node:internal/modules/esm/hooks:383:20)
      at async MessagePort.handleMessage (node:internal/modules/esm/worker:199:18) {
    cause: SyntaxError: Unexpected token (1:40)
        at pp$4.raise (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn/dist/acorn.js:3573:15)
        at pp$9.unexpected (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn/dist/acorn.js:772:10)
        at pp$9.semicolon (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn/dist/acorn.js:749:68)
        at Parser.parseImport (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn-import-attributes/lib/index.js:242:12)
        at pp$8.parseStatement (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn/dist/acorn.js:948:51)
        at pp$8.parseTopLevel (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn/dist/acorn.js:829:23)
        at Parser.parse (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn/dist/acorn.js:601:17)
        at Function.parse (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn/dist/acorn.js:651:37)
        at getEsmExports (/Users/tim/Documents/Repositories/import-in-the-middle/lib/get-esm-exports.js:37:23)
        at getExports (/Users/tim/Documents/Repositories/import-in-the-middle/lib/get-exports.js:97:14) {
      pos: 40,
      loc: [Position],
      raisedAt: 44
    }
  }
}

@timfish timfish changed the title Fix/improve error fix: Include source url for parsing failures Jun 18, 2024
AbhiPrasad

This comment was marked as outdated.

@timfish

This comment has been minimized.

@jsumners-nr
Copy link
Copy Markdown
Contributor

Do not use NODE_ENV. I provided feedback in #112 that we should use Node.js's native feature for issuing warnings.

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.

3 participants