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

fix(typescript): allow directory imports #23254

Merged
merged 9 commits into from
Jun 5, 2023

Conversation

kristojorg
Copy link
Contributor

This updates previous work in #22887 to align more fully with --moduleResolution=bundler, allowing index files to be imported with the /index extension

@kristojorg
Copy link
Contributor Author

@microsoft-github-policy-service agree

Copy link
Contributor

@dgozman dgozman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR! Just a few comments.

packages/playwright-test/src/util.ts Outdated Show resolved Hide resolved
packages/playwright-test/src/util.ts Outdated Show resolved Hide resolved

function dirExists(resolved: string) {
return fs.existsSync(resolved) && fs.lstatSync(resolved).isDirectory();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a newline at the end of the file.

packages/playwright-test/src/util.ts Outdated Show resolved Hide resolved
tests/playwright-test/esm.spec.ts Outdated Show resolved Hide resolved
tests/playwright-test/esm.spec.ts Outdated Show resolved Hide resolved
@kristojorg kristojorg requested a review from dgozman May 25, 2023 07:19
@kristojorg
Copy link
Contributor Author

@dgozman Good suggestions, thanks. I've updated

@kristojorg
Copy link
Contributor Author

not totally sure what happened to these checks, maybe we should re run them?

@kristojorg
Copy link
Contributor Author

@dgozman, wondering if there is something I can do to unblock this? The test failures look unrelated to the work I did

@dgozman
Copy link
Contributor

dgozman commented May 30, 2023

@dgozman, wondering if there is something I can do to unblock this? The test failures look unrelated to the work I did

Sorry for delay, holidays here. The failures are indeed related - fs.lstatSync() throws, see my comments.

@kristojorg
Copy link
Contributor Author

The failures are indeed related - fs.lstatSync() throws, see my comments.

I've added a try/catch and replaced lstatSync with statSync.

@kristojorg
Copy link
Contributor Author

kristojorg commented May 31, 2023

@dgozman I'm just struggling to understand the error the failing test is pointing at. It appears to me that the expect block is being incorrectly interpreted as an array of strings when in fact it is an array of regexs.

I also can't get it to fail locally

@dgozman
Copy link
Contributor

dgozman commented May 31, 2023

This looks very good. Let's avoid the try/catch (see below), and should be good to go.

I've added a try/catch

Why don't we pass throwIfNoEntry: false option to avoid exceptions altogether? This should be much faster.

I'm just struggling to understand the error the failing test is pointing at.

This seems unrelated to your change, we'll take a look on our own. Sorry for the inconvenience.

@kristojorg
Copy link
Contributor Author

Why don't we pass throwIfNoEntry: false option to avoid exceptions altogether? This should be much faster.

I tried this actually and Typescript was giving me an error that it was an invalid option. I figured it must have been added in a newer version of Node than we had installed, but it appears to just be missing from the 14.x types. I've added it with a comment and ts-ignore. Lmk if you'd like me to change that

@mxschmitt
Copy link
Member

Why don't we pass throwIfNoEntry: false option to avoid exceptions altogether? This should be much faster.

I tried this actually and Typescript was giving me an error that it was an invalid option. I figured it must have been added in a newer version of Node than we had installed, but it appears to just be missing from the 14.x types. I've added it with a comment and ts-ignore. Lmk if you'd like me to change that

ts-ignore is unfortunately not an option for us. Let's upgrade @types/node to 16.x since we dropped support for Node.js 14? (as a separate PR). If you don't want to do it, I can do it later. Thanks!

@kristojorg
Copy link
Contributor Author

Made a PR here: #23429

Copy link
Contributor

@dgozman dgozman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kristojorg The code looks great, but linter is unhappy. With #23429 landed, this probably just needs a rebase?

}

function fileExists(resolved: string) {
return fs.lstatSync(resolved)?.isFile();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Why lstat and not stat? It will return "false" for a symbolic link, so we'd better check the resolved file instead.
  • We should pass throwIfNoEntry: false option to avoid exceptions. Same for dirExists.

Copy link
Contributor Author

@kristojorg kristojorg Jun 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fixed!

kristojorg and others added 9 commits June 5, 2023 09:45
This updates previous work in microsoft#22887 to align more fully with `--moduleResolution=bundler`, allowing index files to be imported with the /index extension
Co-authored-by: Dmitry Gozman <dgozman@gmail.com>
Signed-off-by: Kristo Jorgenson <kristojorg@users.noreply.github.com>
Co-authored-by: Dmitry Gozman <dgozman@gmail.com>
Signed-off-by: Kristo Jorgenson <kristojorg@users.noreply.github.com>
@github-actions
Copy link
Contributor

github-actions bot commented Jun 5, 2023

"tests 1" report.

Copy link
Contributor

@dgozman dgozman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, merging in. Thank you for the PR!

@dgozman dgozman merged commit d5d155d into microsoft:main Jun 5, 2023
25 of 26 checks passed
@github-actions
Copy link
Contributor

github-actions bot commented Jun 5, 2023

"tests 1" report.

@kristojorg kristojorg deleted the resolve-dir-imports branch June 6, 2023 12:39
@kristojorg
Copy link
Contributor Author

Hey there, I'm wondering if you know when this will be released?

@mxschmitt
Copy link
Member

mxschmitt commented Jun 27, 2023

This should be already included in v1.35.0.

@kristojorg
Copy link
Contributor Author

I see, yes it is in that release. It seems this isn't working for directory imports from node_modules. For example if I have import pkg from "@org/package", I get a ENOENT: no such file or directory error. The path it is trying to stat however, is /my/source/code/@org/package. I have two questions:

  1. Is there a different way to handle node_module imports?
  2. Should I make this function throw a better error if neither a file or directory exists at the given location, even with the various extensions and index we try adding?

@mxschmitt
Copy link
Member

Can you file an issue and we can continue there? Easier to track, thanks!

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.

None yet

3 participants