Graphql-code-generator: Codegen fails when base folder name ends with something looking like a file extension

Created on 5 Oct 2020  Â·  5Comments  Â·  Source: dotansimha/graphql-code-generator

Describe the bug

This one really got me stumbling :D I kept getting an error saying Unable to find template plugin matching 'typescript' even though everything had been installed properly. And I have used this project before, without issues.

But after some searching and messing with the code I found the culprit – createRequire.

Before the require is created it runs path.extname(from) to determine if from is pointing towards a file or a folder. But this check gives a false positive if the folder the project is located within ends with something similar to an extension name, e.g. mysite.com.

To Reproduce
Steps to reproduce the behavior:

  1. Setup any project or revisit and earlier
  2. Change the root folder name (the folder where package.json is located) to end with e.g. .com or .whatever
  3. Get into the now renamed folder and run your codegen script (e.g. yarn generate or npm run generate)

The last step should fail complaining that the first plugin in the plugins array can't be found. Remove the extension name from the folder to once again make it work.

Expected behavior
The expected behavior is that this should not fail. Any kind of folder name should be accepted.

Environment:

  • OS: MacOS
  • @graphql-codegen/cli: 1.17.10
  • NodeJS: 12.18.3

Additional context
I have found a solution that might work and it requires minimal changes to the makeDefaultLoader function:

const makeDefaultLoader = (from: string) => {
-  if (!path.extname(from)) {
+  if (fs.statSync(from).isDirectory()) {
    from = path.join(from, '__fake.js');
  }

  const relativeRequire = (createRequire || createRequireFromPath)(from);

  return (mod: string) => {
    return import(relativeRequire.resolve(mod));
  };
};

I would be happy to supply a PR for this if you like!

bug core waiting-for-release

All 5 comments

I took the liberty of submitting a PR #4860

Heres a minimal repro: https://github.com/mastorm/graphql-code-generator-error-repro

do yarn install && yarn gen in both folders

Describe the bug

This one really got me stumbling :D I kept getting an error saying Unable to find template plugin matching 'typescript' even though everything had been installed properly. And I have used this project before, without issues.

But after some searching and messing with the code I found the culprit – createRequire.

Before the require is created it runs path.extname(from) to determine if from is pointing towards a file or a folder. But this check gives a false positive if the folder the project is located within ends with something similar to an extension name, e.g. mysite.com.

To Reproduce
Steps to reproduce the behavior:

1. Setup any project or revisit and earlier

2. Change the root folder name (the folder where `package.json` is located) to end with e.g. `.com` or `.whatever`

3. Get into the now renamed folder and run your codegen script (e.g. `yarn generate` or `npm run generate`)

The last step should fail complaining that the first plugin in the plugins array can't be found. Remove the extension name from the folder to once again make it work.

Expected behavior
The expected behavior is that this should not fail. Any kind of folder name should be accepted.

Environment:

* OS: MacOS

* `@graphql-codegen/cli`: 1.17.10

* NodeJS: 12.18.3

Additional context
I have found a solution that might work and it requires minimal changes to the makeDefaultLoader function:

const makeDefaultLoader = (from: string) => {
-  if (!path.extname(from)) {
+  if (fs.statSync(from).isDirectory()) {
    from = path.join(from, '__fake.js');
  }

  const relativeRequire = (createRequire || createRequireFromPath)(from);

  return (mod: string) => {
    return import(relativeRequire.resolve(mod));
  };
};

I would be happy to supply a PR for this if you like!

This was the only thing that works for me, but I don't understand why this is still happening on @graphql-codegen/cli version 1.17.10.

Describe the bug

This one really got me stumbling :D I kept getting an error saying Unable to find template plugin matching 'typescript' even though everything had been installed properly. And I have used this project before, without issues.

But after some searching and messing with the code I found the culprit – createRequire.

Before the require is created it runs path.extname(from) to determine if from is pointing towards a file or a folder. But this check gives a false positive if the folder the project is located within ends with something similar to an extension name, e.g. mysite.com.

To Reproduce

Steps to reproduce the behavior:

1. Setup any project or revisit and earlier

2. Change the root folder name (the folder where `package.json` is located) to end with e.g. `.com` or `.whatever`

3. Get into the now renamed folder and run your codegen script (e.g. `yarn generate` or `npm run generate`)

The last step should fail complaining that the first plugin in the plugins array can't be found. Remove the extension name from the folder to once again make it work.

Expected behavior

The expected behavior is that this should not fail. Any kind of folder name should be accepted.

Environment:

* OS: MacOS

* `@graphql-codegen/cli`: 1.17.10

* NodeJS: 12.18.3

Additional context

I have found a solution that might work and it requires minimal changes to the makeDefaultLoader function:

```diff

const makeDefaultLoader = (from: string) => {

  • if (!path.extname(from)) {
  • if (fs.statSync(from).isDirectory()) {
from = path.join(from, '__fake.js');

}

const relativeRequire = (createRequire || createRequireFromPath)(from);

return (mod: string) => {

return import(relativeRequire.resolve(mod));

};

};

```

I would be happy to supply a PR for this if you like!

This was the only thing that works for me, but I don't understand why this is still happening on @graphql-codegen/cli version 1.17.10.

This has not been released yet. It's merged into master but awaits release. Keep an eye on #4824 for what it will contain.

Fixed in @graphql-codegen/[email protected]

Was this page helpful?
0 / 5 - 0 ratings