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(build): fix resolution algorithm when build.ssr is true #9989

Merged
merged 5 commits into from Sep 21, 2022

Conversation

mrm007
Copy link
Contributor

@mrm007 mrm007 commented Sep 5, 2022

Description

This PR fixes the resolution algorithm when build.ssr is true i.e. when tryNodeResolve is called with ssr: true and externalize: true.

I couldn't find any tests covering this edge case, so I added a fixture in playground/ssr-resolve.
Related issues/PRs: #8420 #8421.

Additional context

The current resolution produces an invalid path here

if (!pkg?.data.exports && path.extname(id) !== resolvedExt) {
resolvedId += resolvedExt
}

In the case of autosuggest-highlight, autosuggest-highlight/match should resolve to autosuggest-highlight/match/index.js, not autosuggest-highlight/match + .js which doesn't exist.

with fix
➜ DEBUG='vite:resolve-details' pnpm build                            

> ssr-resolve@0.0.0 build /__/vite/playground/ssr-resolve
> vite build

vite v3.1.0-beta.2 building SSR bundle for production...
  vite:resolve-details [fs] /__/vite/playground/ssr-resolve/main.js -> /__/vite/playground/ssr-resolve/main.js +0ms
transforming (1) main.js
  vite:resolve-details [node/deep-import] ./match -> /__/vite/node_modules/.pnpm/autosuggest-highlight@3.3.4/node_modules/autosuggest-highlight/match/index.js +24ms
  vite:resolve-details [processResult] autosuggest-highlight/match -> autosuggest-highlight/match/index.js +0ms
  vite:resolve-details [node/deep-import] ./match -> /__/vite/node_modules/.pnpm/autosuggest-highlight@3.3.4/node_modules/autosuggest-highlight/match/index.js +3ms
  vite:resolve-details [processResult] autosuggest-highlight/match -> autosuggest-highlight/match/index.js +0ms
  vite:resolve-details [node/deep-import] ./isInteger -> /__/vite/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isInteger.js +2ms
  vite:resolve-details [processResult] lodash/isInteger -> lodash/isInteger.js +0ms
  vite:resolve-details [node/deep-import] ./isInteger -> /__/vite/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isInteger.js +2ms
  vite:resolve-details [processResult] lodash/isInteger -> lodash/isInteger.js +0ms
  vite:resolve-details [node/deep-import] ./server -> /__/vite/node_modules/.pnpm/react-dom@18.2.0_react@18.2.0/node_modules/react-dom/server.node.js +2ms
  vite:resolve-details [node/deep-import] ./server -> /__/vite/node_modules/.pnpm/react-dom@18.2.0_react@18.2.0/node_modules/react-dom/server.node.js +1ms
✓ 1 modules transformed.
dist/main.mjs   0.27 KiB
without fix (main)

⚠️ Notice the paths in the lines tagged with [processResult] (autosuggest-highlight/match.js doesn't exist).

➜ DEBUG='vite:resolve-details' pnpm build

> ssr-resolve@0.0.0 build /__/vite/playground/ssr-resolve
> vite build

vite v3.1.0-beta.2 building SSR bundle for production...
  vite:resolve-details [fs] /__/vite/playground/ssr-resolve/main.js -> /__/vite/playground/ssr-resolve/main.js +0ms
transforming (1) main.js
  vite:resolve-details [node/deep-import] ./match -> /__/vite/node_modules/.pnpm/autosuggest-highlight@3.3.4/node_modules/autosuggest-highlight/match/index.js +13ms
  vite:resolve-details [processResult] autosuggest-highlight/match -> autosuggest-highlight/match.js +0ms
  vite:resolve-details [node/deep-import] ./match -> /__/vite/node_modules/.pnpm/autosuggest-highlight@3.3.4/node_modules/autosuggest-highlight/match/index.js +2ms
  vite:resolve-details [processResult] autosuggest-highlight/match -> autosuggest-highlight/match.js +0ms
  vite:resolve-details [node/deep-import] ./isInteger -> /__/vite/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isInteger.js +1ms
  vite:resolve-details [processResult] lodash/isInteger -> lodash/isInteger.js +0ms
  vite:resolve-details [node/deep-import] ./isInteger -> /__/vite/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isInteger.js +0ms
  vite:resolve-details [processResult] lodash/isInteger -> lodash/isInteger.js +0ms
  vite:resolve-details [node/deep-import] ./server -> /__/vite/node_modules/.pnpm/react-dom@18.2.0_react@18.2.0/node_modules/react-dom/server.node.js +1ms
  vite:resolve-details [node/deep-import] ./server -> /__/vite/node_modules/.pnpm/react-dom@18.2.0_react@18.2.0/node_modules/react-dom/server.node.js +1ms
✓ 1 modules transformed.
dist/main.mjs   0.27 KiB
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  playground/ssr-resolve/__tests__/ssr-resolve.spec.ts > correctly resolve entrypoints
Error: [vite-node] Failed to load autosuggest-highlight/match.js
 ❯ async /__/oss/vite/playground-temp/ssr-resolve/dist/main.mjs:1:256
 ❯ async /__/oss/vite/playground/ssr-resolve/__tests__/ssr-resolve.spec.ts:6:31

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯

What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@mrm007 mrm007 changed the title Fix resolution algorithm when build.ssr is true fix(build): fix resolution algorithm when build.ssr is true Sep 5, 2022
patak-dev
patak-dev previously approved these changes Sep 5, 2022
Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks a lot for the fix!

@patak-dev patak-dev added p3-minor-bug An edge case that only affects very specific usage (priority) feat: ssr labels Sep 5, 2022
@patak-dev
Copy link
Member

One improvement we could do before merging is to move the new tests to an existing playground. Our e2e CI is taking quite a bit of time already so we are trying to keep the number of playgrounds at check. Instead of importing real packages in the ssr-resolve you created, we could add new local packages in the ssr-deps repo with these edge cases distilled (check pkg-exports for example).

@mrm007
Copy link
Contributor Author

mrm007 commented Sep 6, 2022

Hi @patak-dev, thanks for looking at this!

I had gone that route initially and created packages in ssr-deps, but then a weird thing happened. The issue seems to not be reproducible inside that setup. Whether it's because it uses ssrLoadModule or something else, the tests pass with or without the fix. Here's the branch without the fix and the tests still pass.

Log output from that branch
➜ DEBUG='vite:resolve-details' pnpm -w test-build ssr-deps

> vite-monorepo@ test-build /__/oss/vite
> cross-env VITE_TEST_BUILD=1 vitest run -c vitest.config.e2e.ts "ssr-deps"

✂ ✂ ✂
vite:resolve-details [node/deep-import] ./dir -> /__/oss/vite/node_modules/.pnpm/file+playground+ssr-deps+entries/node_modules/entries/dir/index.js
vite:resolve-details [processResult] entries/dir -> entries/dir.js
✂ ✂ ✂
vite:resolve-details [node/deep-import] ./dir -> /__/oss/vite/node_modules/.pnpm/file+playground+ssr-deps+entries/node_modules/entries/dir/index.js
✂ ✂ ✂

 ✓ playground/ssr-deps/__tests__/ssr-deps.spec.ts  (17 tests) 2172ms

Test Files  1 passed (1)
     Tests  17 passed (17)
  Start at  12:02:55
  Duration  3.51s (transform 449ms, setup 169ms, collect 37ms, tests 2.17s)

So, I've removed the real packages (react, lodash, autosuggest-highlight) and created simple ones with the edge cases distilled. Thanks for the feedback!

patak-dev
patak-dev previously approved these changes Sep 6, 2022
Copy link
Member

@patak-dev patak-dev left a comment

Choose a reason for hiding this comment

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

Makes sense. Thanks a lot for reworking the test cases and for the clear exposition of your steps on this PR.

@patak-dev patak-dev requested a review from bluwy September 6, 2022 20:26
@mrm007
Copy link
Contributor Author

mrm007 commented Sep 20, 2022

@patak-dev @bluwy can we please get this merged?

@patak-dev
Copy link
Member

@mrm007 would you address @ydcjeff comments first? Thanks!

Copy link
Member

@bluwy bluwy left a comment

Choose a reason for hiding this comment

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

Didn't realized a requested review from me. This looks good 👍 I second @ydcjeff's changes too before merging.

@patak-dev patak-dev merged commit 7229251 into vitejs:main Sep 21, 2022
@benjervis
Copy link

@patak-dev Do we have any timeline on when this would be released? It doesn't look like it made it into v3.1.4, but we're very keen to be able to move forward with our project.

@patak-dev
Copy link
Member

@benjervis would you send a PR cherry-picking this one to the v3.1 branch?

@benjervis
Copy link

@patak-dev I've opened #10354

cuuc20072008 added a commit to cuuc20072008/vite that referenced this pull request Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat: ssr p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants