Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

fix(test runner): align with typescript behaviour for resolving index.js and package.json through path mapping #32078

Merged
merged 4 commits into from
Aug 12, 2024

Conversation

Skn0tt
Copy link
Member

@Skn0tt Skn0tt commented Aug 8, 2024

Supercedes #31915, closes #31811.

When TypeScript resolves a specifier via path mapping, it does not interpret package.json. If path mapping resolves to a directory, it only looks at the index.js file in that directory if it's in CommonJS mode.

We need to mirror this in our esmLoader.ts.

…x.js` and `package.json` through path mapping

Co-Authored-By: Dmitry Gozman <dgozman@gmail.com>

This comment has been minimized.

This comment has been minimized.

// TypeScript does not interpret package.json for path mappings: https://www.typescriptlang.org/docs/handbook/modules/reference.html#paths-should-not-point-to-monorepo-packages-or-node_modules-packages
const shouldNotResolveDirectory = isPathMapping && isESM;

if (!shouldNotResolveDirectory && dirExists(resolved)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like TypeScript:

The docs above match my manual testing as well. I think we should follow TS and implement the same logic.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added a test to ensure we follow your first observation in 9a80386.

For the second one, isn't that exactly the behaviour we have right now? If there's a package.json file, then we let Node.js deal with resolving to either index.js or main, depending on the ESM mode.

Copy link
Contributor

Choose a reason for hiding this comment

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

  • For the exports field, it looks like the code will defer to Node.js (when not in ESM), and Node will honor the exports. Is that not the case?
  • For the main field, TypeScript also checks various extensions (e.g. ts) after following it, and we currently do not, because we leave it to Node.js. That's a minor thing, but it would be ideal to mirror TypeScript there as well.
  • Updating comments to explain these things would be great as well!

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. I believe the test https://github.com/Skn0tt/playwright/blob/9a80386c7d057027499609aa4d648549482474fa/tests/playwright-test/resolver.spec.ts#L609-L644 asserts just that behaviour. Maybe i'm looking at it wrong though.
  2. The .ts extension is covered by https://github.com/Skn0tt/playwright/blob/9a80386c7d057027499609aa4d648549482474fa/tests/playwright-test/resolver.spec.ts#L572-L607. What other extensions should we cover?

Both of these are about the path mapping case. Are you referring to the case where there's no path mapping?

In 62ff3fd, i've added a test to ensure that main is ignored in ESM mode, and updated the comments.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, the tests look like they cover all the cases. I guess for the item 1, Node.js does not look at exports either, which works for us. Thank you for following through!

This comment has been minimized.

// TypeScript does not interpret package.json for path mappings: https://www.typescriptlang.org/docs/handbook/modules/reference.html#paths-should-not-point-to-monorepo-packages-or-node_modules-packages
const shouldNotResolveDirectory = isPathMapping && isESM;

if (!shouldNotResolveDirectory && dirExists(resolved)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yep, the tests look like they cover all the cases. I guess for the item 1, Node.js does not look at exports either, which works for us. Thank you for following through!

@Skn0tt Skn0tt merged commit effb1ae into microsoft:main Aug 12, 2024
29 checks passed
Copy link
Contributor

Test results for "tests 1"

3 flaky ⚠️ [playwright-test] › ui-mode-test-progress.spec.ts:22:5 › should update trace live
⚠️ [webkit-library] › library/video.spec.ts:165:5 › screencast › should work with old options
⚠️ [webkit-page] › page/page-goto.spec.ts:182:3 › should properly cancel Cross-Origin-Opener-Policy navigation

29693 passed, 711 skipped
✔️✔️✔️

Merge workflow run.

dgozman added a commit to dgozman/playwright that referenced this pull request Sep 6, 2024
dgozman added a commit to dgozman/playwright that referenced this pull request Sep 6, 2024
…ng `index.js` and `package.json` through path mapping (microsoft#32078)"

This reverts commit effb1ae.
dgozman added a commit that referenced this pull request Sep 6, 2024
…ng `index.js` and `package.json` through path mapping (#32078)" (#32492)

This reverts commit effb1ae.

This broke path mapping into directories in ESM mode. References #32480.
dgozman added a commit to dgozman/playwright that referenced this pull request Sep 6, 2024
dgozman added a commit to dgozman/playwright that referenced this pull request Sep 9, 2024
dgozman added a commit to dgozman/playwright that referenced this pull request Sep 9, 2024
dgozman added a commit that referenced this pull request Sep 9, 2024
We now hopefully align with `moduleResolution: bundler` tsconfig option,
allowing directory imports in every scenario, and allowing proper module
imports when not going through the type mapping.

This regressed in #32078. Fixes #32480, fixes #31811.
dgozman added a commit to dgozman/playwright that referenced this pull request Sep 11, 2024
…pescript behaviour for resolving `index.js` and `package.json` through path mapping (microsoft#32078)"

This reverts commit effb1ae.

This broke path mapping into directories in ESM mode. References microsoft#32480.
dgozman added a commit that referenced this pull request Sep 11, 2024
#32560)

…behaviour for resolving `index.js` and `package.json` through path
mapping (#32078)"

This reverts commit effb1ae.

This broke path mapping into directories in ESM mode. References #32480.
dgozman added a commit to dgozman/playwright that referenced this pull request Sep 11, 2024
…ts with path mapping

We now hopefully align with `moduleResolution: bundler` tsconfig option,
allowing directory imports in every scenario, and allowing proper module
imports when not going through the type mapping.

This regressed in microsoft#32078. Fixes microsoft#32480, fixes microsoft#31811.
dgozman added a commit that referenced this pull request Sep 12, 2024
…ath mapping (#32571)

We now hopefully align with `moduleResolution: bundler` tsconfig option,
allowing directory imports in every scenario, and allowing proper module
imports when not going through the type mapping.

This regressed in #32078. Fixes #32480, fixes #31811.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Regression]: ESM Directory Import is not working anymore with Playwright's ESM Loader activated
2 participants