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

CLI plugin breaks paths when used with webpack alias (windows) #2661

Closed
3 of 4 tasks
enricobuehler opened this issue Feb 19, 2023 · 8 comments
Closed
3 of 4 tasks

CLI plugin breaks paths when used with webpack alias (windows) #2661

enricobuehler opened this issue Feb 19, 2023 · 8 comments

Comments

@enricobuehler
Copy link

enricobuehler commented Feb 19, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

The plugin breaks import paths when used in combination with webpack aliases.
It seems to happen because webpack transforms the import paths to absolute paths, which start with the drive letter, e.g. D:\... and then the plugin suffixes this path with ./. If really needed i can provide a minimal reproduction but since i know which code is responsible for the bug i thought it wouldn't help.

Minimum reproduction code

Steps to reproduce

  1. Create nestjs project
  2. Setup plugin and webpack with alias imports
  3. Run the dev server
    -> Import fails only for the files modified by the cli

Expected behavior

Obviously the paths shouldn't break, however im not sure how to achieve this. In my case just removing the ./ suffix fixed it, could also provide a PR for this, but don't know about possible side effects. The suffix is added here: https://github.com/nestjs/graphql/blob/master/packages/graphql/lib/plugin/utils/plugin-utils.ts#L112

Package version

10.1.6

Graphql version

graphql: 16.6.0
apollo-server-express: 3.11.1

NestJS version

9.2.1

Node.js version

19.6.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

@kamilmysliwiec
Copy link
Member

We need a reproduction repository to fix this issue

@enricobuehler
Copy link
Author

@enricobuehler
Copy link
Author

Just tested it on macOS, there the issue doesn't occur.

@enricobuehler
Copy link
Author

With the newest version compiling still fails on windows, can i create a pull request with the suggested fix or help otherwise to get this fixed?

@all2pie
Copy link

all2pie commented Apr 12, 2023

@enricobuehler yes please it would be appreciated.

You can also look into #2734 it might be related.

enricobuehler added a commit to enricobuehler/graphql that referenced this issue Apr 12, 2023
removed the addition of a '.' suffix so paths
are not broken on windows anymore

fixes nestjs#2661
@mikejpeters
Copy link
Contributor

mikejpeters commented Apr 18, 2023

I'm running into the same issue on Windows (but it works fine on Linux).

The linked pull request fixes the compilation errors on Windows but then breaks things on Linux:

ERROR in ***/example.input.ts 6:23-47
Module not found: Error: Can't resolve 'other.input' in '/home/***/example/dto'
Did you mean './other.input'?

I tried to investigate what's causing the original issue, in order to fix it for Windows without breaking it on Linux, and it seems like the issue is that relativePath is actually an absolute path on Windows.

For example, when this code runs on Windows:

let relativePath = posix.relative(posix.dirname(fileName), importPath);
relativePath = relativePath[0] !== '.' ? './' + relativePath : relativePath;

  1. importPath is 'C:/Users/***/example.model'
  2. posix.dirname(fileName) evaluates to '.'
  3. path.posix.relative('.', 'C:/Users/***/example.model') evaluates to 'C:/Users/***/example.model'

Maybe the problem here is using path.posix.relative instead of path.relative? I'm only guessing and kinda out of my depth.

My hacky fix for now is to only prefix the path if it actually is relative:

  relativePath = !path.isAbsolute(relativePath) && relativePath[0] !== '.' ? './' + relativePath : relativePath;

@kamilmysliwiec
Copy link
Member

Perhaps we should include your hacky fix as a temporary solution to this issue, WDYT? @mikejpeters Would you like to create a PR for this?

@mikejpeters
Copy link
Contributor

Yeah I can probably give it a shot this weekend if no-one else can do it in the meantime. I would like to try maybe adding some tests for Linux and Windows filepaths as well since it seems easy to fix one but break the other

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

Successfully merging a pull request may close this issue.

4 participants