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

import error #2

Closed
seeden opened this issue Jul 10, 2017 · 46 comments
Closed

import error #2

seeden opened this issue Jul 10, 2017 · 46 comments

Comments

@seeden
Copy link

seeden commented Jul 10, 2017

When I use next import I can see error and my build will fail

import quiz from './queries/quiz.graphql';
import quizzes from './queries/quizzes.graphql';
/Users/seeden/Documents/git/quizana-ui/node_modules/babel-core/lib/transformation/file/index.js:590
      throw err;
      ^

TypeError: /src/graphql/quiz/typeDefs.js: Cannot read property 'type' of undefined
    at Whitespace.getNewlinesAfter (/node_modules/babel-generator/lib/whitespace.js:50:19)
    at Generator._printNewline (/node_modules/babel-generator/lib/printer.js:459:34)
    at Generator.printJoin (/node_modules/babel-generator/lib/printer.js:376:32)
    at Generator.printList (/node_modules/babel-generator/lib/printer.js:430:17)
    at Generator.ObjectExpression (/node_modules/babel-generator/lib/generators/types.js:57:10)
    at /node_modules/babel-generator/lib/printer.js:298:23
    at Buffer.withSource (/node_modules/babel-generator/lib/buffer.js:168:5)
    at Generator.withSource (/node_modules/babel-generator/lib/printer.js:189:15)
    at Generator.print (/node_modules/babel-generator/lib/printer.js:297:10)
    at Generator.ArrayExpression (/node_modules/babel-generator/lib/generators/types.js:106:12)

But when I change quiz => quizQuery evrything is fine.

import quizQuery from './queries/quiz.graphql';
import quizzes from './queries/quizzes.graphql';

I have really no idea what is wrong with names. I have same error across whole project.

@bs1180
Copy link

bs1180 commented Jul 21, 2017

I'm also seeing exactly this error, but it's extremely hard to pin down and I can't even reliably repeat it. I did notice adding any statement resolves the problem - could it be some sort of race condition?

Throws error:

import query from "./SimplexListContainer.gql";
import { graphql } from 'react-apollo'

export default graphql(query)(SimplexList)

Doesn't throw:

import query from "./SimplexListContainer.gql";
import { graphql } from 'react-apollo'

console.log('wut')

export default graphql(query)(SimplexList)

I'm using next.js, and my .babelrc is very simple:

{
  "plugins": [
    "babel-plugin-inline-import-graphql-ast"
  ],
  "env": {
    "development": {
      "presets": [
        "next/babel",
        "flow"
      ],
      "plugins": ["inline-dotenv"]
    }
}

Thanks for any help.

@detrohutt
Copy link
Owner

detrohutt commented Jul 21, 2017

Unfortunately I have no idea how to fix this. This project was my first attempt at working with babel plugins so I'm not very familiar with the concepts.

I will say someone else reported another issue with the same error message to me. In his case, it happened when he added a blank line before one of his import statements. I was unable to reproduce that error on my end though.

Anyone more knowledgeable feel free to make a pull request or point me in the right direction.

Thanks @seeden and @bs1180 for your help in working around the bug. When I have more time I'll try to do some research into what might be causing this.

@detrohutt
Copy link
Owner

detrohutt commented Jul 22, 2017

If you guys want to try upgrading to v2.0.0, it may fix the problem. I didn't do anything to purposely fix it but I pretty much completely rewrote the plugin so I may have inadvertently fixed the bug.

@real34
Copy link
Contributor

real34 commented Jul 28, 2017

I am still facing the same issue with the latest release. It does not seem to be fixed. I will try to investigate more too an see if I can bring more information.

@TAQ2
Copy link

TAQ2 commented Jul 28, 2017

i was having a similar problem and i think i have fixed it. You basically need a env set for production in your babel rc. Take a look at mine:

{
  "env": {
    "development": {
      "presets": ["next/babel"],
      "plugins": [
        "babel-plugin-inline-import-graphql-ast",
        "transform-class-properties"
      ]
    },
    "production": {
      "presets": ["next/babel"],
      "plugins": [
        "babel-plugin-inline-import-graphql-ast",
        "transform-class-properties"
      ]
    },
    "test": {
      "presets": ["es2015", "next/babel"],
      "plugins": [
        "babel-plugin-inline-import-graphql-ast",
        "transform-class-properties"
      ]
    }
  }
}

not sure if you are experiencing the same issue but let me know if this works for you :)

@bs1180
Copy link

bs1180 commented Jul 31, 2017

Unfortunately it's still reoccurring with the new version, and the suggestion by TAQ2 hasn't worked either. Happy to experiment with other possibilities, if there are any suggestions?

@detrohutt
Copy link
Owner

detrohutt commented Aug 4, 2017

@bs1180 I'm still not sure why this is happening, but I've got some time to investigate now. I'm considering adding a runtime: true option for runtime parsing(more detail here) which may help. If it's because of a race condition as you mentioned, it probably won't help since there will still need to be synchronous file reads to resolve fragments to build out the query.

My first step is to recreate the issue on my end so I've got a few questions:

  • What does the simplest query that causes the error(SimplexListContainer.gql in your above example) look like? Does it throw an error even with a self-contained query with no fragment imports?
  • What version of Next are you using?
  • Is your project based off of the with-apollo example in the Next.js repo or are you doing something different?

Thanks for your patience with this issue btw.

P.S. If you have time to condense your project into a simple reproduction repo of the issue that you can share, that would be extremely helpful. If not, I'll do my best to recreate your scenario from your description.

@detrohutt
Copy link
Owner

@seeden If you can provide a reproduction repo of your error, I'll be glad to take a look and try to fix it. Sorry for the long wait.

@detrohutt
Copy link
Owner

@seeden @bs1180 I've created an example project where I attempted to recreate the errors you guys were getting, but I'm not getting an error in either case.

I named my query import the same as the file name(seeden's case), and tried importing and immediately exporting(bs1180's case)

The first case(master branch) is closer to what I would normally do, but I made a "separate-component" branch to more closely imitate what bs1180 appeared to be doing. Both branches work fine for me.

I'm closing this for now, but if either of you are able to reproduce the error in a repo(or fork of mine) I'll reopen this issue. Hope my example helps!

@seeden
Copy link
Author

seeden commented Aug 7, 2017

Hi @detrohutt

I tried your latest version 2.0.0 and I can see same error.

// models
import Leaderboard from './models/Leaderboard.graphql';

// payloads
import LeaderboardTypePayload from './queries/LeaderboardTypePayload.graphql';

// queries
import leaderboards from './queries/leaderboards.graphql';
import leaderboardType from './queries/leaderboardType.graphql';

export default [
  // models
  Leaderboard,

  // payloads
  LeaderboardTypePayload,

  // queries
  leaderboards,
  leaderboardType,
];

Next works

// models
import LeaderboardModel from './models/Leaderboard.graphql';

// payloads
import LeaderboardTypePayload from './queries/LeaderboardTypePayload.graphql';

// queries
import leaderboards from './queries/leaderboards.graphql';
import leaderboardType from './queries/leaderboardType.graphql';

export default [
  // models
  LeaderboardModel,

  // payloads
  LeaderboardTypePayload,

  // queries
  leaderboards,
  leaderboardType,
];
/Users/seeden/Documents/git/quizana-ui/node_modules/babel-core/lib/transformation/file/index.js:590
      throw err;
      ^

TypeError: /Users/seeden/Documents/git/quizana-ui/src/graphql/leaderboard/typeDefs.js: Cannot read property 'type' of undefined
    at Whitespace.getNewlinesAfter (/Users/seeden/Documents/git/quizana-ui/node_modules/babel-generator/lib/whitespace.js:50:19)
    at Generator._printNewline (/Users/seeden/Documents/git/quizana-ui/node_modules/babel-generator/lib/printer.js:459:34)
    at Generator.printJoin (/Users/seeden/Documents/git/quizana-ui/node_modules/babel-generator/lib/printer.js:376:32)
    at Generator.printList (/Users/seeden/Documents/git/quizana-ui/node_modules/babel-generator/lib/printer.js:430:17)
    at Generator.ObjectExpression (/Users/seeden/Documents/git/quizana-ui/node_modules/babel-generator/lib/generators/types.js:57:10)
    at /Users/seeden/Documents/git/quizana-ui/node_modules/babel-generator/lib/printer.js:298:23
    at Buffer.withSource (/Users/seeden/Documents/git/quizana-ui/node_modules/babel-generator/lib/buffer.js:168:5)
    at Generator.withSource (/Users/seeden/Documents/git/quizana-ui/node_modules/babel-generator/lib/printer.js:189:15)
    at Generator.print (/Users/seeden/Documents/git/quizana-ui/node_modules/babel-generator/lib/printer.js:297:10)
    at Generator.ObjectProperty (/Users/seeden/Documents/git/quizana-ui/node_modules/babel-generator/lib/generators/types.js:92:8)

@detrohutt
Copy link
Owner

@seeden I have still failed to reproduce this error. I have tried on Linux and Windows. I'm starting to suspect it is a problem with babel and not with my plugin. What operating system are you using and what version of babel-cli?

@detrohutt detrohutt reopened this Aug 7, 2017
@seeden
Copy link
Author

seeden commented Aug 7, 2017

I have same problem on my Mac and Ubuntu as well.
Here is my versions:

    "babel-cli": "^6.24.1",
    "babel-core": "^6.25.0",

and .babelrc

{
  "presets": [
    ["env", {
      "targets": {
        "node": true,
      },
      "useBuiltIns": true,
      "loose": true,
    }],
    "stage-0",
    "react",
  ],
  "plugins": [
    "babel-plugin-inline-import-graphql-ast",
    "transform-decorators-legacy",
    "transform-class-properties",
    "dynamic-import-node",
    "transform-proto-to-assign",
    "transform-flow-strip-types",
  ],
}

@detrohutt
Copy link
Owner

Ok, good news. I was finally able to reproduce the bug. This was the only way I was able to reproduce it:

  1. Have exactly 3 import statements with blank lines between each
  2. Name the import the same as the imported filename(without extension)
  3. Have export default statement immediately after the last import(and blank line)

For Example:

import { graphql } from 'react-apollo'

import SimplexListContainer from '../gql/SimplexListContainer.gql'

import SimplexList from './SimplexListComponent'

export default graphql(SimplexListContainer)(SimplexList)

In my case there were several different workarounds, any of which got rid of the error:

  1. Have more than 3 import statements
  2. Replace any of the non-gql import statements with a require statement
  3. Replace export default with module.exports =
  4. Get rid of any of the blank lines
  5. Add any statement besides import/ export to the file(even just adding true works)
  6. Name the import anything other than the filename
  7. Strangely, if the filename contains a number, you can name the import the same

I was able to reproduce this in both next.js and an ejected create-react-app project. Anyway now that I have reproduced the error, I can quit bothering you all with questions and get to work on fixing the issue! ;) I'll post here when I think I've got it fixed.

@detrohutt
Copy link
Owner

@seeden @bs1180 @real34 I think I fixed it! Try upgrading to 2.0.1-beta.4 and let me know if it works for you.

@real34
Copy link
Contributor

real34 commented Aug 8, 2017

@detrohutt not for me. Here is an example of file with the issue when removing the comment line https://gist.github.com/real34/851e9c210a90772368424658f9494353#file-gallery-js-L58

I have 3 files in error, and every time adding a comment line before the call to the enhancer (like in the example above) solve the issues.
Below other examples with just this part:


// This comment will fix a weird issue. See https://github.com/detrohutt/babel-plugin-inline-import-graphql-ast/issues/2
const SmartFeaturedProducts = EnhanceFeaturedProducts({
  FeaturedProductsQuery
})(FeaturedProducts);

SmartFeaturedProducts.propTypes = {
  children: PropTypes.node.isRequired
};

export default SmartFeaturedProducts;

and


// This comment will fix a weird issue. See https://github.com/detrohutt/babel-plugin-inline-import-graphql-ast/issues/2
export default EnhanceAddress({
  AddressQuery: AddressQuery,
  CountriesQuery: CountriesQuery
})(injectIntl(Address));

@rngadam
Copy link

rngadam commented Aug 22, 2017

Saw the same error, added a comment before client.query and started working again...

@detrohutt
Copy link
Owner

@rngadam try using the new version I just released and see if that works any better. It's version 2.0.1-rc.1

@danielkcz
Copy link

I am still having issues (#7). Either way perhaps it could help to make the whole process more verbose by adding some debug log messages with some details (eg. what file it's parsing) so it's possible to track when it happens.

@jjant
Copy link

jjant commented Sep 22, 2017

I'm still experiencing this bug, has any solution been found?

@detrohutt
Copy link
Owner

detrohutt commented Sep 23, 2017

@jjant I just published version 2.0.1 to npm ... it's the same as 2.0.1-rc.2 so if you've already tried that, then it won't fix the issue. Hopefully it will though, because I'm pretty much out of ideas at this point. :/

@danielkcz
Copy link

@detrohutt I kinda gave you an idea ... #2 (comment).

@detrohutt
Copy link
Owner

@FredyC Fair enough. For the last month or so I've been focusing exclusively on learning C++ and working on a project in that. I'm not sure I want to "context switch" back to this project at the moment, but as soon as I pick this back up I'll add some debugging. In the meantime if you or anyone else is interested in making a PR to do that, I'll gladly accept it.

@jjant
Copy link

jjant commented Oct 4, 2017

Has anyone tried to fix this? Or can recommend an alternative?

@detrohutt
Copy link
Owner

Hey guys, please try the fix @vstepanyuk made by using 2.0.3-beta.0/@beta from npm and let me know if this fixes the problem for any of you (or breaks anything).

@TAQ2
Copy link

TAQ2 commented Oct 10, 2017

i'm still seeing the same error :/

@xuxucode
Copy link

me too, error still exists.

@vstepanyuk
Copy link

Can you share your examples?

@advance512
Copy link

Happens to me too.

@TAQ2
Copy link

TAQ2 commented Nov 15, 2017

If I put a new line above the .graphql file, it works.

I think I found a weird bug with it. When I use auto format with prettier using an atom plugin it seems to fail. I also wonder if its a problem with not using semi colons?

@advance512
Copy link

advance512 commented Nov 15, 2017

I just added newlines between the import statements and it works.

This does not work:

import PropTypes from 'prop-types';

import { Mutation } from '../../hoc/withMutation/index';

import InviteMarketerToCampaignMutationQuery from '../../graphql/company/inviteCampaignMarketer.graphql';
import CompanyAllCampaignInvitationsQuery from '../../graphql/company/companyAllCampaignInvitations.graphql';
import CompanyCampaignInvitationsQuery from '../../graphql/company/companyCampaignInvitations.graphql';
import CompanyCampaignContestantsQuery from '../../graphql/company/companyCampaignContestants.graphql';

This works:



import PropTypes from 'prop-types';





import { Mutation } from '../../hoc/withMutation/index';





import InviteMarketerToCampaignMutationQuery from '../../graphql/company/inviteCampaignMarketer.graphql';




import CompanyAllCampaignInvitationsQuery from '../../graphql/company/companyAllCampaignInvitations.graphql';




import CompanyCampaignInvitationsQuery from '../../graphql/company/companyCampaignInvitations.graphql';



import CompanyCampaignContestantsQuery from '../../graphql/company/companyCampaignContestants.graphql';


if there are no newlines, it doesn't. :) Of course, eslint is now happy about this.

Any chance we can get a fix?

@detrohutt
Copy link
Owner

@advance512 you can try the new beta version I just published. Please let me know whether or not that fixes it for you.

babel-plugin-inline-import-graphql-ast@2.0.3-beta.1

@detrohutt
Copy link
Owner

did 2.0.3-beta.1 resolve this issue for anyone?

@svsool
Copy link

svsool commented Nov 20, 2017

Hi, for me it still doesn't work with 2.0.3-beta.1.

This doesn't work:

import { graphql } from 'react-apollo';
import transactionsGql from './transactions.gql';
import deleteTransactionsGql from './deleteTransactions.gql';

It works:

import { graphql } from 'react-apollo';
import transactionsGql from './transactions.gql';

import deleteTransactionsGql from './deleteTransactions.gql';

@real34
Copy link
Contributor

real34 commented Nov 23, 2017

Hi, we managed to create a repository with a reproduction of this bug.

To reproduce:

We noticed the bug is related to the number of chars... See video below:

https://www.dropbox.com/s/dwcce8skh5eafus/screencast_00000.mp4?dl=0

We hope it will help!

@advance512
Copy link

Good job, @real34 !

@detrohutt
Copy link
Owner

Thanks @real34 ! I’m not sure if I’ll get to it today since it’s thanksgiving here in the U.S. but I’ll get to work on it ASAP!

@detrohutt
Copy link
Owner

@real34 I just published babel-plugin-inline-import-graphql-ast@2.0.3-beta.2 which seems to fix it (on my machine at least).

@xuxucode
Copy link

xuxucode commented Nov 26, 2017

2.0.3-beta.2 works for me, thanks 👍

Update (2017/11/27)

Sorry, it still happens, I have to add a blank line above the import to fix it.

@detrohutt
Copy link
Owner

@EdwardAA I tried yet another approach with babel-plugin-inline-import-graphql-ast@2.0.3-beta.3. Please let me know if that fixes the issue.

@xuxucode
Copy link

xuxucode commented Dec 3, 2017

2.0.3-beta.3 does not fix the issue.

@bishalshr
Copy link

import * as TestQuery from '../queries/test.graphql';
This worked for me

@MatthieuLemoine
Copy link

I made a PR to babel-generator to fix this issue babel/babel#7205.

I need some thumbups to make sure that a new version of Babel 6 will be released with this fix.

@detrohutt
Copy link
Owner

Thank you very much @MatthieuLemoine I regret I’ve not had the time to deal with this myself but I’ve been dealing with this bug pretty much every day in my current project and my codebase is littered with empty comments to accommodate it. I hope they will merge your pr and release for babel 6. I added a few reactions.

@detrohutt
Copy link
Owner

Unfortunately, it doesn't look like there's much movement on the babel PR to fix this issue. I really appreciate everyone here being so patient about this bug. I know first-hand how annoying it is.

As a "thank you" to you guys, I just released a new beta that has a pretty cool feature. It adds support for multiple operations just like the graphql-tag webpack loader got last month.

Try it out and let me know what you think! It's @beta or "2.2.0-beta.0"

@detrohutt
Copy link
Owner

detrohutt commented Feb 3, 2018

The babel team got this sorted out. See this comment for more info. The gist is you need to remove your node_modules folder and package-lock.json or yarn.lock, make sure your babel version string(s) allows 6.26.1, and run npm install or yarn. I'm going to go ahead and close this issue. Let me know if anyone still has issues after following these steps.

@m14253647g
Copy link

MIT License Copyright (c) [year] [fullname] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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 a pull request may close this issue.