Skip to content
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

Error compiling a sync .ts file - SyntaxError: Unexpected token '', "��z��" is not valid JSON #2312

Closed
bburns opened this issue Jun 12, 2024 · 5 comments
Labels

Comments

@bburns
Copy link
Contributor

bburns commented Jun 12, 2024

I'm getting this same error - am on Windows -

> nango dev --debug

Debug mode enabled
.env file detected and loaded
Is run via npx: false. Is locally installed: false
Version 0.40.0 of nango is installed.
Latest version of nango is 0.40.0.
Current full working directory is read as: C:\Users\bburns\Workspace\neomem\nango\nango-integrations
Current stripped directory is read as: nango-integrations
Found nango.yaml file successfully.
Watching ./nango.yaml
Watching ./**/*.ts, ./nango.yaml

Error compiling google-mail\syncs\emails.ts:
SyntaxError: Unexpected token '', "��z��" is not valid JSON
    at JSON.parse (<anonymous>)
    at new AnyMap (C:\Users\bburns\AppData\Roaming\npm\node_modules\nango\node_modules\@cspotcode\source-map-support\node_modules\@jridgewell\src\any-map.ts:20:37)
    at mapSourcePosition (C:\Users\bburns\AppData\Roaming\npm\node_modules\nango\node_modules\@cspotcode\source-map-support\source-map-support.js:387:14)
    at wrapCallSite (C:\Users\bburns\AppData\Roaming\npm\node_modules\nango\node_modules\@cspotcode\source-map-support\source-map-support.js:592:20)
    at Function.prepareStackTrace (C:\Users\bburns\AppData\Roaming\npm\node_modules\nango\node_modules\@cspotcode\source-map-support\source-map-support.js:671:41)
    at maybeOverridePrepareStackTrace (node:internal/errors:140:29)
    at prepareStackTrace (node:internal/errors:114:5)
    at getStackString (node:internal/util/inspect:1240:16)
    at formatError (node:internal/util/inspect:1369:15)
    at formatRaw (node:internal/util/inspect:985:14)

emails.ts:

import type { NangoSync, GmailEmail } from '../../models'

export default async function fetchData(nango: NangoSync) {
  const backfillPeriod = new Date(Date.now() - 24 * 60 * 60 * 1000) // 24 hours ago.
  const { lastSyncDate } = nango
  const syncDate = lastSyncDate || backfillPeriod

  const pageSize = 100
  let nextPageToken: string | undefined = ''

  do {
    const response: any = await nango.proxy({
      method: 'GET',
      endpoint: '/gmail/v1/users/me/messages',
      params: {
        maxResults: `${pageSize}`,
        q: `after:${Math.floor(syncDate.getTime() / 1000)}`,
        pageToken: nextPageToken,
      },
    })

    const messageList = response.data.messages || []
    const emails: GmailEmail[] = []

    for (const message of messageList) {
      const messageDetail = await nango.proxy({
        method: 'GET',
        endpoint: `/gmail/v1/users/me/messages/${message.id}`,
      })

      const headers = messageDetail.data.payload.headers.reduce(
        (acc: any, current: any) => {
          acc[current.name] = current.value
          return acc
        },
        {}
      )

      emails.push(mapEmail(messageDetail, headers))
    }

    await nango.batchSave(emails, 'GmailEmail')

    nextPageToken = response.data.nextPageToken
  } while (nextPageToken)
}

function mapEmail(messageDetail: any, headers: any): GmailEmail {
  const parts = messageDetail.data.payload.parts || []
  let body = ''
  for (const part of parts) {
    if (part.mimeType === 'text/plain') {
      body = Buffer.from(part.body.data, 'base64').toString('utf8')
      break
    }
  }
  return {
    id: messageDetail.data.id,
    sender: headers.From,
    recipients: headers.To,
    date: new Date(parseInt(messageDetail.data.internalDate)),
    subject: headers.Subject,
    body: body,
    threadId: messageDetail.data.threadId,
  }
}

I tried looking through the compiler code but couldn't see where the problem might be.

I have this error when running node ../nango/packages/cli/dist/index.js compile

Error compiling "/Users/samuelbodin/code/nango-integrations/unauthenticated/syncs/trackDeletes.ts":
SyntaxError: Unexpected token '', "��z��" is not valid JSON
    at JSON.parse (<anonymous>)
    at new AnyMap (/Users/samuelbodin/code/nango/node_modules/@jridgewell/src/any-map.ts:20:37)
    at mapSourcePosition (/Users/samuelbodin/code/nango/node_modules/@cspotcode/source-map-support/source-map-support.js:387:14)
    at wrapCallSite (/Users/samuelbodin/code/nango/node_modules/@cspotcode/source-map-support/source-map-support.js:592:20)
    at Function.prepareStackTrace (/Users/samuelbodin/code/nango/node_modules/@cspotcode/source-map-support/source-map-support.js:671:41)
    at prepareStackTraceCallback (node:internal/errors:145:29)
    at getStackString (node:internal/util/inspect:1240:16)
    at formatError (node:internal/util/inspect:1369:15)
    at formatRaw (node:internal/util/inspect:985:14)
    at formatValue (node:internal/util/inspect:840:10)

Can you try this again? Should be resolved.

Originally posted by @khaliqgant in #2273 (comment)

@bburns
Copy link
Contributor Author

bburns commented Jun 13, 2024

This seems to be an issue with backslashes in the path - will work on a PR.

chokidar.watch(paths, [options])
Note: globs must not contain windows separators (), because that's how they work by the standard — you'll need to replace them with forward slashes (/).

@khaliqgant
Copy link
Member

khaliqgant commented Jun 13, 2024

Got it, thanks for digging!

Does nango compile throw the same error? I would think not since we don't use chokidar since that isn't a watch task but rather an one off command

@linear linear bot added the GitHub label Jun 14, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 23, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 23, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 23, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 23, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 25, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 25, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 26, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 26, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 26, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 26, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 26, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 26, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 26, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 26, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 27, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 27, 2024
bburns added a commit to bburns/nango that referenced this issue Jun 27, 2024
bburns added a commit to bburns/nango that referenced this issue Jul 5, 2024
bburns added a commit to bburns/nango that referenced this issue Jul 5, 2024
bburns added a commit to bburns/nango that referenced this issue Jul 5, 2024
bburns added a commit to bburns/nango that referenced this issue Jul 10, 2024
bburns added a commit to bburns/nango that referenced this issue Jul 10, 2024
bburns added a commit to bburns/nango that referenced this issue Jul 10, 2024
bburns added a commit to bburns/nango that referenced this issue Jul 10, 2024
bburns added a commit to bburns/nango that referenced this issue Jul 10, 2024
bburns added a commit to bburns/nango that referenced this issue Jul 10, 2024
@bburns
Copy link
Contributor Author

bburns commented Jul 10, 2024

Ah, sorry for the noise - I didn't realize my commits would show up here. It took some back and forth to get it all working.

The nango dev crash was due to the filepath passed to tsup.build, which needs forward slashes on Windows - not sure why it gives such a cryptic error message.

And nango compile was crashing, as node-glob needs forward slashes also.

Sending the PR...

bburns added a commit to bburns/nango that referenced this issue Jul 10, 2024
This commit uses forward slashes on Windows for the calls
to glob.sync and tsup.build - those are where
`nango compile` and `nango dev` were failing.

I copied the slash function from https://github.com/sindresorhus/slash,
as it just converts \ to /, and didn't want to deal with
package-lock.json changes.

It also uses path.join or path.resolve instead of
`${foo}/pok/bar.ts`, os.tmpdir() instead of '/tmp', etc.

getNangoRootPath() now always returns a string, since
getPackagePath() will throw an error if it can't find the package.

And I expanded the regex in packages/node-client/lib/utils.unit.test.ts
to handle "nango-node-client/0.41.0 (linux/5.15.153.1-microsoft-standard-WSL2; node.js/20.12.2)".

I ran the tests on Windows with git bash and Command Prompt,
and with WSL2 Ubuntu -

    npm i
    npm run ts-build
    npm run test:cli
    npm run test:unit

and in a test nango project, these worked -

    $ node /c/users/bburns/Workspace/forks/nango/packages/cli/dist/index.js generate --debug
    $ node /c/users/bburns/Workspace/forks/nango/packages/cli/dist/index.js compile --debug
    $ node /c/users/bburns/Workspace/forks/nango/packages/cli/dist/index.js dev --debug

Also added a workflow to run test:cli and test:unit on
Windows - .github/workflows/tests-windows.yaml.
bburns added a commit to bburns/nango that referenced this issue Jul 13, 2024
This commit uses forward slashes on Windows for the calls
to glob.sync and tsup.build - those are where
`nango compile` and `nango dev` were failing.

I copied the slash function from https://github.com/sindresorhus/slash,
as it just converts \ to /, and didn't want to deal with
package-lock.json changes.

It also uses path.join or path.resolve instead of
`${foo}/pok/bar.ts`, os.tmpdir() instead of '/tmp', etc.

getNangoRootPath() now always returns a string, since
getPackagePath() will throw an error if it can't find the package.

And I expanded the regex in packages/node-client/lib/utils.unit.test.ts
to handle "nango-node-client/0.41.0 (linux/5.15.153.1-microsoft-standard-WSL2; node.js/20.12.2)".

I ran the tests on Windows with git bash and Command Prompt,
and with WSL2 Ubuntu -

    npm i
    npm run ts-build
    npm run test:cli
    npm run test:unit

and in a test nango project, these worked -

    $ node /c/users/bburns/Workspace/forks/nango/packages/cli/dist/index.js generate --debug
    $ node /c/users/bburns/Workspace/forks/nango/packages/cli/dist/index.js compile --debug
    $ node /c/users/bburns/Workspace/forks/nango/packages/cli/dist/index.js dev --debug

Also added a workflow to run test:cli and test:unit on
Windows - .github/workflows/tests-windows.yaml.
bodinsamuel added a commit that referenced this issue Jul 15, 2024
This commit uses forward slashes on Windows for the calls to glob.sync
and tsup.build - those are where `nango compile` and `nango dev` were
failing.

I copied the slash function from https://github.com/sindresorhus/slash,
as it just converts \ to /, and didn't want to deal with
package-lock.json changes.

It also uses path.join or path.resolve instead of `${foo}/pok/bar.ts`,
os.tmpdir() instead of '/tmp', etc.

getNangoRootPath() now always returns a string, since getPackagePath()
will throw an error if it can't find the package.

And I expanded the regex in packages/node-client/lib/utils.unit.test.ts
to handle "nango-node-client/0.41.0
(linux/5.15.153.1-microsoft-standard-WSL2; node.js/20.12.2)".

I ran the tests on Windows with git bash and Command Prompt, and with
WSL2 Ubuntu -

    npm i
    npm run ts-build
    npm run test:cli
    npm run test:unit

and in a test nango project, these worked -

$ node /c/users/bburns/Workspace/forks/nango/packages/cli/dist/index.js
generate --debug
$ node /c/users/bburns/Workspace/forks/nango/packages/cli/dist/index.js
compile --debug
$ node /c/users/bburns/Workspace/forks/nango/packages/cli/dist/index.js
dev --debug

Also added a workflow to run test:cli and test:unit on Windows -
.github/workflows/tests-windows.yaml.

## Issue ticket number and link

#2312

## Checklist before requesting a review (skip if just adding/editing
APIs & templates)
- [x] I added tests, otherwise the reason is: 
- [ ] I added observability, otherwise the reason is:
- [ ] I added analytics, otherwise the reason is:

---------

Co-authored-by: Samuel Bodin <1637651+bodinsamuel@users.noreply.github.com>
@bburns
Copy link
Contributor Author

bburns commented Jul 19, 2024

Fixed with pr #2496

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants