Skip to content

fix: call resolveId('vitest') after buildStart #5646

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

Merged
merged 8 commits into from
May 2, 2024

Conversation

hi-ogawa
Copy link
Contributor

@hi-ogawa hi-ogawa commented May 1, 2024

Description

ctx.distPath is only used by pool runTests, so we can move resolveId from ctx.setServer to ctx.start.

todo

  • test with vscode extension
    • verified locally with pnpm.overrides

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. If the feature is substantial or introduces breaking changes without a discussion, PR might be closed.
  • Ideally, include a test that fails without this PR but passes with it.
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Tests

  • Run the tests with pnpm test:ci.

Documentation

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs command.

Changesets

  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.

Copy link

netlify bot commented May 1, 2024

Deploy Preview for fastidious-cascaron-4ded94 ready!

Name Link
🔨 Latest commit 74a9636
🔍 Latest deploy log https://app.netlify.com/sites/fastidious-cascaron-4ded94/deploys/6632d17a16139c000844d7fe
😎 Deploy Preview https://deploy-preview-5646--fastidious-cascaron-4ded94.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@hi-ogawa hi-ogawa changed the title fix: use require.resolve('vitest') instead of resolveId during init fix: call resolveId('vitest') after buildStart May 1, 2024
@hi-ogawa hi-ogawa marked this pull request as ready for review May 1, 2024 01:51
@@ -386,6 +381,11 @@ export class Vitest {
async start(filters?: string[]) {
this._onClose = []

// if Vitest is running globally, then we should still import local vitest if possible
const projectVitestPath = await this.vitenode.resolveId('vitest')
const vitestDir = projectVitestPath ? resolve(projectVitestPath.id, '../..') : rootDir
Copy link
Member

Choose a reason for hiding this comment

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

vs code extension doesn't call start at all

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, really...
I had createRequire approach before 1037511. Would this work better?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or we could delay it further to ctx.runFiles. Let me think...

Copy link
Member

Choose a reason for hiding this comment

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

The createRequire approach is wrong because we need Vite to process it. Having it in runFiles is fine. We can have a guard to not check this path if it is resolved, and reset it in configureServer

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we need Vite's resolution? If the purpose is only to find local Vitest, would createRequire with require.resolve('vitest', { paths: [config.root] }) do the same?

This was the approach used for package installer:

if (process.versions.pnp) {
const targetRequire = createRequire(__dirname)
try {
targetRequire.resolve(dependency, { paths: [root, __dirname] })

Copy link
Member

Choose a reason for hiding this comment

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

We need Vite's resolution because files importing vitest are processed by Vite. It's possible that Vitest is called globally and resolved id is not correct, or there is an alias or resolveId interception

@sheremet-va sheremet-va merged commit f5faf42 into vitest-dev:main May 2, 2024
19 checks passed
@hi-ogawa hi-ogawa mentioned this pull request May 2, 2024
6 tasks
@hi-ogawa hi-ogawa deleted the fix-avoid-early-resolveId branch May 2, 2024 09:30
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.

resolveId is called before buildStart with enforce: 'pre'
2 participants